Skip to content

Commit dc830b7

Browse files
committed
Fix GA4 events
1 parent 1e59991 commit dc830b7

28 files changed

+377
-245
lines changed

scripts/gtag-fix/app.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env node
2+
const path = require('path');
3+
const fs = require('fs');
4+
5+
6+
const topDir = path.join(__dirname, '..', '..');
7+
8+
const jsDir = path.join(topDir, 'src/assets/js');
9+
console.log('jsDir ' + jsDir);
10+
for(const file of fs.readdirSync(jsDir)) {
11+
if (!file.endsWith('.js')) {
12+
continue;
13+
}
14+
const origFile = fs.readFileSync(path.join(jsDir, file), 'utf8');
15+
16+
let newFile = '';
17+
for(let line of origFile.split(/\n/)) {
18+
const gtagIndex = line.indexOf('gtag(');
19+
if (gtagIndex >= 0) {
20+
const linePrefix = line.substring(0, gtagIndex);
21+
22+
const paramStartIndex = gtagIndex + 5;
23+
const paramEndIndex = line.lastIndexOf(')');
24+
if (paramEndIndex >= paramStartIndex) {
25+
26+
const paramStr = line.substring(paramStartIndex, paramEndIndex);
27+
28+
const linePrefix = line.substring(0, gtagIndex);
29+
const lineSuffix = line.substring(paramEndIndex + 1);
30+
31+
const params = paramStr.split(',');
32+
for(let ii = 0; ii < params.length; ii++) {
33+
params[ii] = params[ii].trim();
34+
}
35+
if (params.length >= 4) {
36+
if (params[0] == "'send'" && params[1] == "'event'") {
37+
// console.log('params', params);
38+
39+
// ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
40+
// 2 eventCategory -> event_category
41+
// 3 eventAction -> event_action
42+
// 4 eventLabel -> event_label
43+
// 5 eventValue -> event_value
44+
45+
switch(params.length) {
46+
case 4:
47+
line = linePrefix + "gtag('event', " + params[3] + ", {'event_category':" + params[2] + "})" + lineSuffix;
48+
break;
49+
50+
case 5:
51+
line = linePrefix + "gtag('event', " + params[3] + ", {'event_category':" + params[2] + ", 'event_label':" + params[4] + "})" + lineSuffix;
52+
break;
53+
54+
default:
55+
console.log('too many parameters', params);
56+
break;
57+
}
58+
59+
console.log('updated line: ' + line);
60+
}
61+
else {
62+
console.log('not send event params', params);
63+
}
64+
}
65+
else {
66+
console.log('not enough parameters', params);
67+
}
68+
}
69+
else {
70+
console.log('missing end )');
71+
}
72+
}
73+
newFile += line + '\n';
74+
}
75+
76+
if (newFile.trim() != origFile.trim()) {
77+
console.log('changed ' + file);
78+
fs.writeFileSync(path.join(jsDir, file), newFile);
79+
}
80+
}

scripts/gtag-fix/package-lock.json

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/gtag-fix/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "gtag-fix",
3+
"description": "Tool for GA4 migration",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "MIT",
7+
"author": "[email protected]",
8+
"engines": {
9+
"node": ">=16"
10+
},
11+
"scripts": {
12+
"start": "node app.js"
13+
},
14+
"dependencies": {
15+
}
16+
}

src/assets/js/api-helper-auth.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ $(document).ready(function() {
88
let orgInfo;
99

1010
const handleLogin = function() {
11-
gtag('send', 'event', eventCategory, 'Login Started');
11+
gtag('event', 'Login Started', {'event_category':eventCategory});
1212

1313
const origUrl = window.location.href;
1414
window.location.href = 'https://login.particle.io/login?redirect=' + encodeURI(origUrl);
1515
};
1616

1717
const handleEditAccount = function() {
18-
gtag('send', 'event', eventCategory, 'Edit Account');
18+
gtag('event', 'Edit Account', {'event_category':eventCategory});
1919

2020
const origUrl = window.location.href;
2121
window.location.href = 'https://login.particle.io/account-info?redirect=' + encodeURI(origUrl);
@@ -26,7 +26,7 @@ $(document).ready(function() {
2626
localStorage.removeItem('apiHelperOrg')
2727

2828
if (typeof apiHelper != 'undefined' && apiHelper.localLogin && apiHelper.localLogin.access_token ) {
29-
gtag('send', 'event', eventCategory, 'Logged Out Local');
29+
gtag('event', 'Logged Out Local', {'event_category':eventCategory});
3030
localStorage.removeItem('apiHelperLocalLogin');
3131

3232
if (!apiHelper.localLogin.tokenLogin) {
@@ -49,7 +49,7 @@ $(document).ready(function() {
4949
location.reload();
5050
}
5151
else {
52-
gtag('send', 'event', eventCategory, 'Logged Out SSO');
52+
gtag('event', 'Logged Out SSO', {'event_category':eventCategory});
5353
localStorage.removeItem('apiHelperTestLogin');
5454
const origUrl = window.location.href;
5555
window.location.href = 'https://login.particle.io/logout?redirect=' + encodeURI(origUrl);
@@ -558,3 +558,4 @@ $(document).ready(function() {
558558
});
559559
});
560560

561+

0 commit comments

Comments
 (0)