Skip to content

Commit 107d3e7

Browse files
authored
Add analytics on Run button and stackblitz button, and SPA transitions (#338)
1 parent 8845f94 commit 107d3e7

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

index.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,6 @@
4747
</head>
4848
<body>
4949
<div id="app"></div>
50-
<!-- Google tag (gtag.js) -->
51-
<script async src="https://www.googletagmanager.com/gtag/js?id=G-QSPTT7WK79"></script>
52-
<script>
53-
window.dataLayer = window.dataLayer || [];
54-
function gtag(){dataLayer.push(arguments);}
55-
gtag('js', new Date());
56-
57-
gtag('config', 'G-QSPTT7WK79');
58-
</script>
5950
<script type="module" src="/src/app.js"></script>
6051
</body>
6152
</html>

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"monaco-editor": "^0.52.2",
5858
"react": "^18.3.1",
5959
"react-dom": "^18.3.1",
60+
"react-ga4": "^2.1.0",
6061
"react-helmet": "^6.1.0",
6162
"react-monaco-editor": "^0.58.0",
6263
"react-router": "^7.6.2",

src/components/Shared/StackBlitzLink.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ReactNode } from 'react';
22
import stackblitzSdk, { ProjectDependencies } from '@stackblitz/sdk';
33
import { TargetBlankLink } from './TargetBlankLink.tsx';
4+
import { sendEvent } from '../analytics.ts';
45

56
type StackBlitzLinkProps = {
67
/**
@@ -51,6 +52,11 @@ export function StackBlitzLink({ code, title, children }: StackBlitzLinkProps) {
5152
href="#stackblitz-open-project"
5253
onClick={(e) => {
5354
e.preventDefault();
55+
sendEvent({
56+
category: 'StackBlitz',
57+
action: 'Open Project',
58+
label: title,
59+
});
5460
/*
5561
* https://developer.stackblitz.com/guides/integration/create-with-sdk#creating-a-new-project
5662
*/

src/components/analytics.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ReactGA from 'react-ga4';
2+
import { useLocation } from 'react-router';
3+
import { useEffect } from 'react';
4+
5+
ReactGA.initialize('G-QSPTT7WK79');
6+
7+
export function PageTracker() {
8+
const location = useLocation();
9+
10+
useEffect(() => {
11+
ReactGA.send({
12+
hitType: 'pageview',
13+
page: location.pathname + location.search,
14+
});
15+
}, [location.pathname, location.search]);
16+
17+
return null;
18+
}
19+
20+
export function sendEvent(ev: { category: string; action: string; label?: string; value?: number }) {
21+
ReactGA.event(ev);
22+
}

src/containers/Root.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { PureComponent } from 'react';
22
import { BrowserRouter } from 'react-router';
33
import Routes from '../routes';
4+
import { PageTracker } from '../components/analytics.ts';
45

56
class Root extends PureComponent {
67
render() {
78
return (
89
<BrowserRouter>
10+
<PageTracker />
911
<Routes />
1012
</BrowserRouter>
1113
);

src/views/ExamplesView.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import fetchFile from '../utils/fetchUtils.ts';
1515
import 'simple-line-icons/scss/simple-line-icons.scss';
1616
import { RouteComponentProps, withRouter } from '../routes/withRouter.tsx';
1717
import { StackBlitzLink } from '../components/Shared/StackBlitzLink.tsx';
18+
import { sendEvent } from '../components/analytics.ts';
1819

1920
// @ts-ignore
2021
const cates = Object.keys(Examples).sort((a, b) => Examples[a].order - Examples[b].order);
@@ -156,6 +157,11 @@ class ExamplesView extends PureComponent<ExamplesViewProps, ExamplesViewState> {
156157

157158
handleRunCode = () => {
158159
if (this.editorRef.current) {
160+
sendEvent({
161+
category: 'Examples',
162+
action: 'Run Code',
163+
label: this.getPage(),
164+
});
159165
this.setState({
160166
// @ts-ignore
161167
exampleCode: this.editorRef.current.getValue(),

0 commit comments

Comments
 (0)