From a6dd9425045ce2f1b8b85dd7e776505f2d3d1616 Mon Sep 17 00:00:00 2001 From: Gabriel P Samson Date: Wed, 24 Mar 2021 15:56:48 -0700 Subject: [PATCH 1/3] Fix GA4 set bug --- integrations/google-analytics-4/HISTORY.md | 9 +++++++++ integrations/google-analytics-4/lib/index.js | 6 +++++- integrations/google-analytics-4/package.json | 2 +- integrations/google-analytics-4/test/index.js | 7 ++++--- 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 integrations/google-analytics-4/HISTORY.md diff --git a/integrations/google-analytics-4/HISTORY.md b/integrations/google-analytics-4/HISTORY.md new file mode 100644 index 000000000..8b0fa67d0 --- /dev/null +++ b/integrations/google-analytics-4/HISTORY.md @@ -0,0 +1,9 @@ +0.0.2 / 2021-03-24 +================== + + * Fix `set` command call. + +0.0.1 / 2021-03-24 +================== + + * Initial commit :rocket: diff --git a/integrations/google-analytics-4/lib/index.js b/integrations/google-analytics-4/lib/index.js index 5e5ea705b..bed60ec44 100644 --- a/integrations/google-analytics-4/lib/index.js +++ b/integrations/google-analytics-4/lib/index.js @@ -153,7 +153,11 @@ GA4.prototype.initialize = function() { * https://developers.google.com/gtagjs/reference/api#set */ for (var i = 0; i < sets.length; i++) { - window.gtag.apply(null, sets[i]); + // Copy the set args and append the command before + // calling gtag.js. + var args = sets[i].slice(0) + args.unshift('set') + window.gtag.apply(null, args); } self.ready(); diff --git a/integrations/google-analytics-4/package.json b/integrations/google-analytics-4/package.json index 9e2368316..d6a83c323 100644 --- a/integrations/google-analytics-4/package.json +++ b/integrations/google-analytics-4/package.json @@ -1,6 +1,6 @@ { "name": "@segment/analytics.js-integration-google-analytics-4", - "version": "0.0.1", + "version": "0.0.2", "description": "", "main": "lib/index.js", "directories": { diff --git a/integrations/google-analytics-4/test/index.js b/integrations/google-analytics-4/test/index.js index d873c8b0c..5e5f8c74e 100644 --- a/integrations/google-analytics-4/test/index.js +++ b/integrations/google-analytics-4/test/index.js @@ -145,20 +145,21 @@ describe('Google Analytics 4', function () { analytics.equal(window.ga4DataLayer[2][2]['cookie_expires'], 21) // cookie_flags uses the `set` command - analytics.equal(window.ga4DataLayer[3][0]['cookie_flags'], 'SameSite=None;Secure') + analytics.equal(window.ga4DataLayer[3][0], 'set') + analytics.equal(window.ga4DataLayer[3][1]['cookie_flags'], 'SameSite=None;Secure') }); it('should disable all advertising features', function () { ga4.options.allowAllAdvertisingFeatures = false; analytics.initialize(); - analytics.deepEqual(toArray(window.ga4DataLayer[4]), ['allow_google_signals', false]) + analytics.deepEqual(toArray(window.ga4DataLayer[4]), ['set', 'allow_google_signals', false]) }); it('should disable all advertising features', function () { ga4.options.allowAdvertisingPersonalization = false; analytics.initialize(); - analytics.deepEqual(toArray(window.ga4DataLayer[5]), ['allow_ad_personalization_signals', false]) + analytics.deepEqual(toArray(window.ga4DataLayer[5]), ['set', 'allow_ad_personalization_signals', false]) }); }); From 5048ece6f1acc342d38cdb9275048b2e889a154c Mon Sep 17 00:00:00 2001 From: Gabriel P Samson Date: Wed, 24 Mar 2021 16:00:51 -0700 Subject: [PATCH 2/3] Fix ugly comment spacing --- integrations/google-analytics-4/lib/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/integrations/google-analytics-4/lib/index.js b/integrations/google-analytics-4/lib/index.js index bed60ec44..b5daacfe0 100644 --- a/integrations/google-analytics-4/lib/index.js +++ b/integrations/google-analytics-4/lib/index.js @@ -153,8 +153,7 @@ GA4.prototype.initialize = function() { * https://developers.google.com/gtagjs/reference/api#set */ for (var i = 0; i < sets.length; i++) { - // Copy the set args and append the command before - // calling gtag.js. + // Copy the set args and append the command before calling gtag.js. var args = sets[i].slice(0) args.unshift('set') window.gtag.apply(null, args); From 28c346453a4e4ce6868c6e7b2eabf15426b1bd9f Mon Sep 17 00:00:00 2001 From: Gabriel P Samson Date: Wed, 24 Mar 2021 16:18:59 -0700 Subject: [PATCH 3/3] Use deep equal for test --- integrations/google-analytics-4/test/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/integrations/google-analytics-4/test/index.js b/integrations/google-analytics-4/test/index.js index 5e5f8c74e..3dfe3de80 100644 --- a/integrations/google-analytics-4/test/index.js +++ b/integrations/google-analytics-4/test/index.js @@ -145,9 +145,7 @@ describe('Google Analytics 4', function () { analytics.equal(window.ga4DataLayer[2][2]['cookie_expires'], 21) // cookie_flags uses the `set` command - analytics.equal(window.ga4DataLayer[3][0], 'set') - analytics.equal(window.ga4DataLayer[3][1]['cookie_flags'], 'SameSite=None;Secure') - + analytics.deepEqual(toArray(window.ga4DataLayer[3]), ['set', { cookie_flags: 'SameSite=None;Secure' }]) }); it('should disable all advertising features', function () {