Skip to content

Commit

Permalink
Google Analytics noConflict integration possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstantineYurevich committed Feb 13, 2016
1 parent 51cfa72 commit 87e274a
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 127 deletions.
91 changes: 50 additions & 41 deletions dist/dd-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6640,7 +6640,7 @@ var ddManager = {

if ((0, _size2['default'])(_integrations) > 0) {
(0, _each2['default'])(_integrations, function (name, integration) {
if (!integration.isLoaded()) {
if (!integration.isLoaded() || integration.getOption('noConflict')) {
integration.once('ready', ready);
integration.initialize();
} else {
Expand Down Expand Up @@ -7714,7 +7714,8 @@ var GoogleAnalytics = (function (_Integration) {
metrics: {},
dimensions: {},
contentGroupings: {},
namespace: undefined
namespace: undefined,
noConflict: false
}, options);

var _this = _possibleConstructorReturn(this, _Integration.call(this, digitalData, optionsWithDefaults));
Expand All @@ -7737,54 +7738,60 @@ var GoogleAnalytics = (function (_Integration) {
this.pageCalled = false;

// setup the tracker globals
if (!window.ga) {
window.GoogleAnalyticsObject = 'ga';
window.ga = window.ga || function ga() {
window.ga.q = window.ga.q || [];
window.ga.q.push(arguments);
};
window.ga.l = new Date().getTime();
}
window.GoogleAnalyticsObject = 'ga';
window.ga = window.ga || function gaPlaceholder() {
window.ga.q = window.ga.q || [];
window.ga.q.push(arguments);
};
window.ga.l = new Date().getTime();

if (window.location.hostname === 'localhost') {
this.setOption('domain', 'none');
}

window.ga('create', this.getOption('trackingId'), {
// Fall back on default to protect against empty string
cookieDomain: this.getOption('domain'),
siteSpeedSampleRate: this.getOption('siteSpeedSampleRate'),
allowLinker: true,
name: this.getOption('namespace')
});

// display advertising
if (this.getOption('doubleClick')) {
this.ga('require', 'displayfeatures');
}
// https://support.google.com/analytics/answer/2558867?hl=en
if (this.getOption('enhancedLinkAttribution')) {
this.ga('require', 'linkid', 'linkid.js');
}
this.initializeTracker();

// send global id
var userId = this.get('user.id');
if (this.getOption('sendUserId') && userId) {
this.ga('set', 'userId', userId);
if (this.getOption('noConflict')) {
this.ready();
} else {
this.load(this.ready);
}
} else {
this.ready();
}
};

// anonymize after initializing, otherwise a warning is shown
// in google analytics debugger
if (this.getOption('anonymizeIp')) this.ga('set', 'anonymizeIp', true);
GoogleAnalytics.prototype.initializeTracker = function initializeTracker() {
window.ga('create', this.getOption('trackingId'), {
// Fall back on default to protect against empty string
cookieDomain: this.getOption('domain'),
siteSpeedSampleRate: this.getOption('siteSpeedSampleRate'),
allowLinker: true,
name: this.getOption('namespace')
});

// custom dimensions & metrics
var custom = this.getCustomDimensions();
if ((0, _size2['default'])(custom)) this.ga('set', custom);
// display advertising
if (this.getOption('doubleClick')) {
this.ga('require', 'displayfeatures');
}
// https://support.google.com/analytics/answer/2558867?hl=en
if (this.getOption('enhancedLinkAttribution')) {
this.ga('require', 'linkid', 'linkid.js');
}

this.load(this.ready);
} else {
this.ready();
// send global id
var userId = this.get('user.id');
if (this.getOption('sendUserId') && userId) {
this.ga('set', 'userId', userId);
}

// anonymize after initializing, otherwise a warning is shown
// in google analytics debugger
if (this.getOption('anonymizeIp')) this.ga('set', 'anonymizeIp', true);

// custom dimensions & metrics
var custom = this.getCustomDimensions();
if ((0, _size2['default'])(custom)) this.ga('set', custom);
};

GoogleAnalytics.prototype.ga = function ga() {
Expand Down Expand Up @@ -7864,8 +7871,10 @@ var GoogleAnalytics = (function (_Integration) {
};

GoogleAnalytics.prototype.trackEvent = function trackEvent(event) {
if (this.getOption('trackOnlyCustomEvents') && ['Viewed Page', 'Viewed Product', 'Clicked Product', 'Viewed Product Detail', 'Added Product', 'Removed Product', 'Completed Transaction', 'Refunded Transaction', 'Viewed Product Category', 'Viewed Checkout Step', 'Completed Checkout Step'].indexOf(event.name) < 0) {
this.onCustomEvent(event);
if (this.getOption('trackOnlyCustomEvents')) {
if (['Viewed Page', 'Viewed Product', 'Clicked Product', 'Viewed Product Detail', 'Added Product', 'Removed Product', 'Completed Transaction', 'Refunded Transaction', 'Viewed Product Category', 'Viewed Checkout Step', 'Completed Checkout Step'].indexOf(event.name) < 0) {
this.onCustomEvent(event);
}
} else {
if (event.name === 'Viewed Page') {
this.onViewedPage(event);
Expand Down
8 changes: 4 additions & 4 deletions dist/dd-manager.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ddManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const ddManager = {

if (size(_integrations) > 0) {
each(_integrations, (name, integration) => {
if (!integration.isLoaded()) {
if (!integration.isLoaded() || integration.getOption('noConflict')) {
integration.once('ready', ready);
integration.initialize();
} else {
Expand Down
111 changes: 60 additions & 51 deletions src/integrations/GoogleAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class GoogleAnalytics extends Integration {
dimensions: {},
contentGroupings: {},
namespace: undefined,
noConflict: false,
}, options);

super(digitalData, optionsWithDefaults);
Expand All @@ -74,54 +75,60 @@ class GoogleAnalytics extends Integration {
this.pageCalled = false;

// setup the tracker globals
if (!window.ga) {
window.GoogleAnalyticsObject = 'ga';
window.ga = window.ga || function ga() {
window.ga.q = window.ga.q || [];
window.ga.q.push(arguments);
};
window.ga.l = new Date().getTime();
}
window.GoogleAnalyticsObject = 'ga';
window.ga = window.ga || function gaPlaceholder() {
window.ga.q = window.ga.q || [];
window.ga.q.push(arguments);
};
window.ga.l = new Date().getTime();

if (window.location.hostname === 'localhost') {
this.setOption('domain', 'none');
}

window.ga('create', this.getOption('trackingId'), {
// Fall back on default to protect against empty string
cookieDomain: this.getOption('domain'),
siteSpeedSampleRate: this.getOption('siteSpeedSampleRate'),
allowLinker: true,
name: this.getOption('namespace'),
});

// display advertising
if (this.getOption('doubleClick')) {
this.ga('require', 'displayfeatures');
}
// https://support.google.com/analytics/answer/2558867?hl=en
if (this.getOption('enhancedLinkAttribution')) {
this.ga('require', 'linkid', 'linkid.js');
}
this.initializeTracker();

// send global id
const userId = this.get('user.id');
if (this.getOption('sendUserId') && userId) {
this.ga('set', 'userId', userId);
if (this.getOption('noConflict')) {
this.ready();
} else {
this.load(this.ready);
}
} else {
this.ready();
}
}

// anonymize after initializing, otherwise a warning is shown
// in google analytics debugger
if (this.getOption('anonymizeIp')) this.ga('set', 'anonymizeIp', true);
initializeTracker() {
window.ga('create', this.getOption('trackingId'), {
// Fall back on default to protect against empty string
cookieDomain: this.getOption('domain'),
siteSpeedSampleRate: this.getOption('siteSpeedSampleRate'),
allowLinker: true,
name: this.getOption('namespace'),
});

// custom dimensions & metrics
const custom = this.getCustomDimensions();
if (size(custom)) this.ga('set', custom);
// display advertising
if (this.getOption('doubleClick')) {
this.ga('require', 'displayfeatures');
}
// https://support.google.com/analytics/answer/2558867?hl=en
if (this.getOption('enhancedLinkAttribution')) {
this.ga('require', 'linkid', 'linkid.js');
}

this.load(this.ready);
} else {
this.ready();
// send global id
const userId = this.get('user.id');
if (this.getOption('sendUserId') && userId) {
this.ga('set', 'userId', userId);
}

// anonymize after initializing, otherwise a warning is shown
// in google analytics debugger
if (this.getOption('anonymizeIp')) this.ga('set', 'anonymizeIp', true);

// custom dimensions & metrics
const custom = this.getCustomDimensions();
if (size(custom)) this.ga('set', custom);
}

ga() {
Expand Down Expand Up @@ -201,20 +208,22 @@ class GoogleAnalytics extends Integration {
}

trackEvent(event) {
if (this.getOption('trackOnlyCustomEvents') && [
'Viewed Page',
'Viewed Product',
'Clicked Product',
'Viewed Product Detail',
'Added Product',
'Removed Product',
'Completed Transaction',
'Refunded Transaction',
'Viewed Product Category',
'Viewed Checkout Step',
'Completed Checkout Step',
].indexOf(event.name) < 0) {
this.onCustomEvent(event);
if (this.getOption('trackOnlyCustomEvents')) {
if ([
'Viewed Page',
'Viewed Product',
'Clicked Product',
'Viewed Product Detail',
'Added Product',
'Removed Product',
'Completed Transaction',
'Refunded Transaction',
'Viewed Product Category',
'Viewed Checkout Step',
'Completed Checkout Step',
].indexOf(event.name) < 0) {
this.onCustomEvent(event);
}
} else {
if (event.name === 'Viewed Page') {
this.onViewedPage(event);
Expand Down
66 changes: 36 additions & 30 deletions test/integrations/GoogleAnalyticsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1732,19 +1732,39 @@ describe('Integrations: GoogleAnalytics', () => {
});
});

describe('Universal with custom namespace', function() {
describe('Universal with noConflict', function() {

let ga;
let options = {
enhancedEcommerce: true,
trackingId: 'UA-51485228-7',
anonymizeIp: true,
domain: 'none',
defaultCurrency: 'USD',
siteSpeedSampleRate: 42,
namespace: 'ddl',
trackOnlyCustomEvents: true
trackOnlyCustomEvents: true,
noConflict: true
};

function loadGA(callback) {
//load GA
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

window.ga('create', 'UA-51485228-7', {
// Fall back on default to protect against empty string
cookieDomain: 'auto',
name: 'gtm.123',
});
window.ga('send', 'pageview');

window.ga(() => {
callback();
});
}

beforeEach(() => {
window.digitalData = {
events: []
Expand All @@ -1761,18 +1781,21 @@ describe('Integrations: GoogleAnalytics', () => {

describe('after loading', function () {
beforeEach((done) => {
ddManager.once('ready', done);
ddManager.initialize({
autoEvents: false
loadGA(() => {
ddManager.once('ready', done);
ddManager.initialize({
autoEvents: false
});
});
});

describe('enhanced ecommerce', function () {
beforeEach(function () {
sinon.stub(window, 'ga');

beforeEach(() => {
sinon.spy(window, 'ga');
});

afterEach(function () {
afterEach(() => {
window.ga.restore();
});

Expand All @@ -1781,13 +1804,8 @@ describe('Integrations: GoogleAnalytics', () => {
name: 'Test',
category: 'Test',
callback: () => {
assert.ok(window.ga.calledWith('ddl.send', 'event', {
eventCategory: 'Test',
eventAction: 'Test',
eventLabel: undefined,
eventValue: 0,
nonInteraction: false
}));
assert.equal(2, window.ga.getAll().length);
assert.ok(window.ga.calledOnce);
done();
}
});
Expand All @@ -1798,13 +1816,7 @@ describe('Integrations: GoogleAnalytics', () => {
name: 'Viewed Page',
category: 'Content',
callback: () => {
assert.ok(!window.ga.calledWith('ddl.send', 'event', {
eventCategory: 'Content',
eventAction: 'Viewed Page',
eventLabel: undefined,
eventValue: 0,
nonInteraction: false
}));
assert.ok(!window.ga.called);
done();
}
});
Expand All @@ -1815,13 +1827,7 @@ describe('Integrations: GoogleAnalytics', () => {
name: 'Viewed Product',
category: 'Ecommerce',
callback: () => {
assert.ok(!window.ga.calledWith('ddl.send', 'event', {
eventCategory: 'Ecommerce',
eventAction: 'Viewed Product',
eventLabel: undefined,
eventValue: 0,
nonInteraction: false
}));
assert.ok(!window.ga.called);
done();
}
});
Expand Down

0 comments on commit 87e274a

Please sign in to comment.