Skip to content

Commit aeec1f4

Browse files
authored
Fix GA4 set bug (#575)
* Fix GA4 set bug * Fix ugly comment spacing * Use deep equal for test
1 parent f3acb12 commit aeec1f4

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
0.0.2 / 2021-03-24
2+
==================
3+
4+
* Fix `set` command call.
5+
6+
0.0.1 / 2021-03-24
7+
==================
8+
9+
* Initial commit :rocket:

integrations/google-analytics-4/lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ GA4.prototype.initialize = function() {
153153
* https://developers.google.com/gtagjs/reference/api#set
154154
*/
155155
for (var i = 0; i < sets.length; i++) {
156-
window.gtag.apply(null, sets[i]);
156+
// Copy the set args and append the command before calling gtag.js.
157+
var args = sets[i].slice(0)
158+
args.unshift('set')
159+
window.gtag.apply(null, args);
157160
}
158161

159162
self.ready();

integrations/google-analytics-4/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@segment/analytics.js-integration-google-analytics-4",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "",
55
"main": "lib/index.js",
66
"directories": {

integrations/google-analytics-4/test/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,19 @@ describe('Google Analytics 4', function () {
145145
analytics.equal(window.ga4DataLayer[2][2]['cookie_expires'], 21)
146146

147147
// cookie_flags uses the `set` command
148-
analytics.equal(window.ga4DataLayer[3][0]['cookie_flags'], 'SameSite=None;Secure')
149-
148+
analytics.deepEqual(toArray(window.ga4DataLayer[3]), ['set', { cookie_flags: 'SameSite=None;Secure' }])
150149
});
151150

152151
it('should disable all advertising features', function () {
153152
ga4.options.allowAllAdvertisingFeatures = false;
154153
analytics.initialize();
155-
analytics.deepEqual(toArray(window.ga4DataLayer[4]), ['allow_google_signals', false])
154+
analytics.deepEqual(toArray(window.ga4DataLayer[4]), ['set', 'allow_google_signals', false])
156155
});
157156

158157
it('should disable all advertising features', function () {
159158
ga4.options.allowAdvertisingPersonalization = false;
160159
analytics.initialize();
161-
analytics.deepEqual(toArray(window.ga4DataLayer[5]), ['allow_ad_personalization_signals', false])
160+
analytics.deepEqual(toArray(window.ga4DataLayer[5]), ['set', 'allow_ad_personalization_signals', false])
162161
});
163162
});
164163

0 commit comments

Comments
 (0)