Skip to content
This repository was archived by the owner on Nov 27, 2019. It is now read-only.

Commit 2636bda

Browse files
Release 2.4.0
1 parent 28b2517 commit 2636bda

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.4.0 / 2017-03-29
2+
==================
3+
4+
* Add support for environment option
15

26
2.3.0 / 2016-11-07
37
==================

lib/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ var GTM = module.exports = integration('Google Tag Manager')
1616
.global('dataLayer')
1717
.global('google_tag_manager')
1818
.option('containerId', '')
19+
.option('environment', '')
1920
.option('trackNamedPages', true)
2021
.option('trackCategorizedPages', true)
21-
.tag('<script src="//www.googletagmanager.com/gtm.js?id={{ containerId }}&l=dataLayer">');
22+
.tag('no-env', '<script src="//www.googletagmanager.com/gtm.js?id={{ containerId }}&l=dataLayer">')
23+
.tag('with-env', '<script src="//www.googletagmanager.com/gtm.js?id={{ containerId }}&l=dataLayer&gtm_preview={{ environment }}">');
2224

2325
/**
2426
* Initialize.
@@ -30,7 +32,12 @@ var GTM = module.exports = integration('Google Tag Manager')
3032

3133
GTM.prototype.initialize = function() {
3234
push({ 'gtm.start': Number(new Date()), event: 'gtm.js' });
33-
this.load(this.ready);
35+
36+
if (this.options.environment.length) {
37+
this.load('with-env', this.options, this.ready);
38+
} else {
39+
this.load('no-env', this.options, this.ready);
40+
}
3441
};
3542

3643
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-google-tag-manager",
33
"description": "The Google Tag Manager analytics.js integration.",
4-
"version": "2.3.0",
4+
"version": "2.4.0",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

test/index.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ describe('Google Tag Manager', function() {
1010
var analytics;
1111
var gtm;
1212
var options = {
13-
containerId: 'GTM-M8M29T'
13+
containerId: 'GTM-M8M29T',
14+
environment: ''
1415
};
1516

1617
beforeEach(function() {
@@ -33,6 +34,7 @@ describe('Google Tag Manager', function() {
3334
.assumesPageview()
3435
.global('dataLayer')
3536
.option('containerId', '')
37+
.option('environment', '')
3638
.option('trackNamedPages', true)
3739
.option('trackCategorizedPages', true));
3840
});
@@ -45,6 +47,10 @@ describe('Google Tag Manager', function() {
4547

4648
describe('after loading', function() {
4749
beforeEach(function(done) {
50+
options = {
51+
containerId: 'GTM-M8M29T',
52+
environment: ''
53+
};
4854
analytics.once('ready', done);
4955
analytics.initialize();
5056
analytics.page();
@@ -169,4 +175,19 @@ describe('Google Tag Manager', function() {
169175
});
170176
});
171177
});
178+
179+
describe('environment options', function() {
180+
it('should use the right tag if the environment option is set', function() {
181+
gtm.options = {
182+
containerId: 'GTM-M8M29T',
183+
environment: 'test'
184+
};
185+
186+
var tag = '<script src="http://www.googletagmanager.com/gtm.js?id=' + gtm.options.containerId + '&l=dataLayer&gtm_preview=' + gtm.options.environment + '">';
187+
analytics.spy(gtm, 'load');
188+
analytics.initialize();
189+
analytics.page();
190+
analytics.loaded(tag);
191+
});
192+
});
172193
});

0 commit comments

Comments
 (0)