Skip to content

Add support to GTM and GA4 for First-Party mode #818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions integrations/google-analytics-4/HISTORY.md
Original file line number Diff line number Diff line change
@@ -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
==================

9 changes: 8 additions & 1 deletion integrations/google-analytics-4/lib/index.js
Original file line number Diff line number Diff line change
@@ -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(
'<script src="//www.googletagmanager.com/gtag/js?id={{ measurementId }}&l=ga4DataLayer">'
'<script src="//{{{ domain }}}/gtag/js?id={{ measurementId }}&l=ga4DataLayer">'
));

/**
@@ -90,6 +91,12 @@ GA4.prototype.initialize = function() {
*/
send_page_view: opts.sendAutomaticPageViewEvent,

/**
* Server Container URL
* https://developers.google.com/tag-platform/tag-manager/server-side/dependency-serving
*/
server_container_url: '//' + opts.domain,

/**
* Cookie Update
* https://developers.google.com/analytics/devguides/collection/ga4/cookies-user-id#cookie_update_parameter
4 changes: 2 additions & 2 deletions integrations/google-analytics-4/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@segment/analytics.js-integration-google-analytics-4",
"version": "0.0.3",
"version": "0.1.0",
"description": "",
"main": "lib/index.js",
"directories": {
@@ -52,4 +52,4 @@
"to-array": "^0.1.4",
"watchify": "^3.7.0"
}
}
}
15 changes: 15 additions & 0 deletions integrations/google-analytics-4/test/index.js
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ describe('Google Analytics 4', function () {
.global('gtag')
.global('ga4DataLayer')
.option('measurementIds', [])
.option('domain', 'www.googletagmanager.com')
.option('cookieDomainName', 'auto')
.option('cookiePrefix', '_ga')
.option('cookieExpiration', 63072000)
@@ -96,6 +97,13 @@ describe('Google Analytics 4', function () {
analytics.called(ga4.load)
analytics.loaded('<script src="http://www.googletagmanager.com/gtag/js?id=G-100&l=ga4DataLayer"></script>')
});

it('should load gtag.js with the custom domain', function () {
ga4.options.domain = 'custom.example.com';
analytics.initialize();
analytics.called(ga4.load)
analytics.loaded('<script src="http://custom.example.com/gtag/js?id=G-100&l=ga4DataLayer"></script>')
});
});
});

@@ -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'
34 changes: 34 additions & 0 deletions integrations/google-analytics-4/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var Analytics = require('@segment/analytics.js-core').constructor;
var integration = require('@segment/analytics.js-integration');
var sandbox = require('@segment/clear-env');
var tester = require('@segment/analytics.js-integration-tester');
var GA4 = require('../lib/');

describe('Google Analytics 4', function() {
var analytics;
var ga4;
var options = {
containerId: 'GTM-XXXX',
environment: '',
domain: 'www.googletagmanager.com',
trackNamedPages: true,
trackCategorizedPages: true
};

beforeEach(function() {
analytics = new Analytics();
ga4 = new GA4(options);
analytics.use(GA4);
analytics.use(tester);
analytics.add(ga4);
});

afterEach(function() {
analytics.restore();
analytics.reset();
ga4.reset();
sandbox();
});
});
5 changes: 5 additions & 0 deletions integrations/google-tag-manager/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.6.0 / 2025-03-19
==================

* Add support for custom domain configuration for GTM script loading

2.5.0 / 2017-04-27
==================

5 changes: 3 additions & 2 deletions integrations/google-tag-manager/lib/index.js
Original file line number Diff line number Diff line change
@@ -16,15 +16,16 @@ var GTM = (module.exports = integration('Google Tag Manager')
.global('google_tag_manager')
.option('containerId', '')
.option('environment', '')
.option('domain', 'www.googletagmanager.com')
.option('trackNamedPages', true)
.option('trackCategorizedPages', true)
.tag(
'no-env',
'<script src="//www.googletagmanager.com/gtm.js?id={{ containerId }}&l=dataLayer">'
'<script src="//{{{ domain }}}/gtm.js?id={{ containerId }}&l=dataLayer">'
)
.tag(
'with-env',
'<script src="//www.googletagmanager.com/gtm.js?id={{ containerId }}&l=dataLayer&gtm_preview={{ environment }}">'
'<script src="//{{{ domain }}}/gtm.js?id={{ containerId }}&l=dataLayer&gtm_preview={{ environment }}">'
));

/**
4 changes: 2 additions & 2 deletions integrations/google-tag-manager/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@segment/analytics.js-integration-google-tag-manager",
"description": "The Google Tag Manager analytics.js integration.",
"version": "2.5.1",
"version": "2.6.0",
"keywords": [
"analytics.js",
"analytics.js-integration",
@@ -44,4 +44,4 @@
"mocha": "^6.1.4",
"watchify": "^3.11.1"
}
}
}
17 changes: 17 additions & 0 deletions integrations/google-tag-manager/test/index.test.js
Original file line number Diff line number Diff line change
@@ -45,6 +45,23 @@ describe('Google Tag Manager', function() {
it('should load', function(done) {
analytics.load(gtm, done);
});

it('should load default domain', function() {
analytics.spy(gtm, 'load');
analytics.initialize();
analytics.page();
analytics.called(gtm.load);
analytics.assert(gtm.options.domain === 'www.googletagmanager.com');
});

it('should load custom domain if specified', function() {
gtm.options.domain = 'custom.example.com';
analytics.spy(gtm, 'load');
analytics.initialize();
analytics.page();
analytics.called(gtm.load);
analytics.assert(gtm.options.domain === 'custom.example.com');
});
});

describe('after loading', function() {