From 410e0ff4fd65eef819fc0ea03214c584eb1f0a1e Mon Sep 17 00:00:00 2001 From: Daniel Archer Date: Wed, 19 Mar 2025 14:05:21 -0700 Subject: [PATCH 1/2] Add support to GTM and GA4 for customizing the domain to allow for a first-party domain --- integrations/google-analytics-4/HISTORY.md | 5 ++ integrations/google-analytics-4/lib/index.js | 3 +- integrations/google-analytics-4/package.json | 4 +- .../google-analytics-4/test/index.test.js | 53 +++++++++++++++++++ integrations/google-tag-manager/HISTORY.md | 5 ++ integrations/google-tag-manager/lib/index.js | 5 +- integrations/google-tag-manager/package.json | 4 +- .../google-tag-manager/test/index.test.js | 17 ++++++ 8 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 integrations/google-analytics-4/test/index.test.js diff --git a/integrations/google-analytics-4/HISTORY.md b/integrations/google-analytics-4/HISTORY.md index 8b0fa67d0..7d78e6f8c 100644 --- a/integrations/google-analytics-4/HISTORY.md +++ b/integrations/google-analytics-4/HISTORY.md @@ -1,3 +1,8 @@ +0.1.0 / 2025-03-19 +================== + + * Add support for custom domain configuration for gtag.js script loading + 0.0.2 / 2021-03-24 ================== diff --git a/integrations/google-analytics-4/lib/index.js b/integrations/google-analytics-4/lib/index.js index b5daacfe0..a1cf533a2 100644 --- a/integrations/google-analytics-4/lib/index.js +++ b/integrations/google-analytics-4/lib/index.js @@ -13,6 +13,7 @@ var GA4 = (module.exports = integration('Google Analytics 4') .global('gtag') .global('ga4DataLayer') .option('measurementIds', []) + .option('domain', 'www.googletagmanager.com') .option('cookieDomainName', 'auto') .option('cookiePrefix', '_ga') .option('cookieExpiration', 63072000) @@ -48,7 +49,7 @@ var GA4 = (module.exports = integration('Google Analytics 4') */ .option('customEventsAndParameters', []) .tag( - '') }); + + it('should load gtag.js with the custom domain', function () { + ga4.options.domain = 'custom.example.com'; + analytics.initialize(); + analytics.called(ga4.load) + analytics.loaded('') + }); }); }); @@ -124,6 +132,13 @@ describe('Google Analytics 4', function () { analytics.equal(window.ga4DataLayer[2][2]['send_page_view'], false) }); + it('should set the server_container_url for all measurement IDs', function () { + ga4.options.domain = 'https://custom.example.com'; + analytics.initialize(); + analytics.equal(window.ga4DataLayer[1][2]['server_container_url'], 'https://custom.example.com') + analytics.equal(window.ga4DataLayer[2][2]['server_container_url'], 'https://custom.example.com') + }); + it('should set cookie related setting for all measurement IDs', function () { ga4.options.cookieUpdate = false; ga4.options.cookieDomainName = 'ajs.test' diff --git a/integrations/google-analytics-4/test/index.test.js b/integrations/google-analytics-4/test/index.test.js index 6d3d9ef98..f5f02e0d6 100644 --- a/integrations/google-analytics-4/test/index.test.js +++ b/integrations/google-analytics-4/test/index.test.js @@ -31,23 +31,4 @@ describe('Google Analytics 4', function() { ga4.reset(); sandbox(); }); - - describe('loading', function() { - it('should load default domain', function() { - analytics.spy(ga4, 'load'); - analytics.initialize(); - analytics.page(); - analytics.called(ga4.load); - analytics.assert(ga4.options.domain === 'www.googletagmanager.com'); - }); - - it('should load custom domain if specified', function() { - ga4.options.domain = 'custom.example.com'; - analytics.spy(ga4, 'load'); - analytics.initialize(); - analytics.page(); - analytics.called(ga4.load); - analytics.assert(ga4.options.domain === 'custom.example.com'); - }); - }); }); \ No newline at end of file