Skip to content

Commit 4d5e39a

Browse files
Analytics: Add user id tracking to google analytics (grafana#42763)
1 parent 8318fab commit 4d5e39a

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"@types/enzyme": "3.10.5",
108108
"@types/enzyme-adapter-react-16": "1.0.6",
109109
"@types/file-saver": "2.0.1",
110+
"@types/google.analytics": "^0.0.42",
110111
"@types/grafana__slate-react": "npm:@types/[email protected]",
111112
"@types/history": "^4.7.8",
112113
"@types/hoist-non-react-statics": "3.3.1",

public/app/core/services/echo/backends/analytics/GABackend.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface GAEchoBackendOptions {
88

99
export class GAEchoBackend implements EchoBackend<PageviewEchoEvent, GAEchoBackendOptions> {
1010
supportedEvents = [EchoEventType.Pageview];
11+
trackedUserId: number | null = null;
1112

1213
constructor(public options: GAEchoBackendOptions) {
1314
const url = `https://www.google-analytics.com/analytics${options.debug ? '_debug' : ''}.js`;
@@ -18,8 +19,8 @@ export class GAEchoBackend implements EchoBackend<PageviewEchoEvent, GAEchoBacke
1819
cache: true,
1920
});
2021

21-
const ga = ((window as any).ga =
22-
(window as any).ga ||
22+
const ga = (window.ga =
23+
window.ga ||
2324
// this had the equivalent of `eslint-disable-next-line prefer-arrow/prefer-arrow-functions`
2425
function () {
2526
(ga.q = ga.q || []).push(arguments);
@@ -30,12 +31,18 @@ export class GAEchoBackend implements EchoBackend<PageviewEchoEvent, GAEchoBacke
3031
}
3132

3233
addEvent = (e: PageviewEchoEvent) => {
33-
if (!(window as any).ga) {
34+
if (!window.ga) {
3435
return;
3536
}
3637

37-
(window as any).ga('set', { page: e.payload.page });
38-
(window as any).ga('send', 'pageview');
38+
window.ga('set', { page: e.payload.page });
39+
window.ga('send', 'pageview');
40+
41+
const { userSignedIn, userId } = e.meta;
42+
if (userSignedIn && userId !== this.trackedUserId) {
43+
this.trackedUserId = userId;
44+
window.ga('set', 'userId', userId);
45+
}
3946
};
4047

4148
// Not using Echo buffering, addEvent above sends events to GA as soon as they appear

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9015,6 +9015,13 @@ __metadata:
90159015
languageName: node
90169016
linkType: hard
90179017

9018+
"@types/google.analytics@npm:^0.0.42":
9019+
version: 0.0.42
9020+
resolution: "@types/google.analytics@npm:0.0.42"
9021+
checksum: c64b3f7991c4bcb1dfe8db902a2e2ebe821ce53a1ca36e6628b4d08d0c24213aa0d4f49492b3144dd6bfa661f53fb1ecf16e83cca522cddd6d9f8419a4b73fe8
9022+
languageName: node
9023+
linkType: hard
9024+
90189025
"@types/graceful-fs@npm:^4.1.2":
90199026
version: 4.1.5
90209027
resolution: "@types/graceful-fs@npm:4.1.5"
@@ -19263,6 +19270,7 @@ __metadata:
1926319270
"@types/enzyme": 3.10.5
1926419271
"@types/enzyme-adapter-react-16": 1.0.6
1926519272
"@types/file-saver": 2.0.1
19273+
"@types/google.analytics": ^0.0.42
1926619274
"@types/grafana__slate-react": "npm:@types/[email protected]"
1926719275
"@types/history": ^4.7.8
1926819276
"@types/hoist-non-react-statics": 3.3.1

0 commit comments

Comments
 (0)