Skip to content

Commit

Permalink
Add cross domain tracking to Tracker object
Browse files Browse the repository at this point in the history
So that pages that link to known partners can allow those partners to
track full user journeys, this commit adds the necessary UA incantations
to link domains and separate accounts together.

This is for Universal Analytics only.
  • Loading branch information
bradwright committed May 5, 2015
1 parent 6a1c3bc commit d4997be
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions javascripts/govuk/analytics/google-analytics-universal-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@
});
};

/*
https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain
trackerId - the UA account code to track the domain against
name - name for the tracker
domain - the domain to track
*/
GoogleAnalyticsUniversalTracker.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
sendToGa('create',
trackerId,
'auto',
{'name': name});
// Load the plugin.
sendToGa('require', 'linker');
sendToGa(name + '.require', 'linker');

// Define which domains to autoLink.
sendToGa('linker:autoLink', [domain]);
sendToGa(name + '.linker:autoLink', [domain]);

sendToGa(name + '.set', 'anonymizeIp', true);
sendToGa(name + '.send', 'pageview');
};

// https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
GoogleAnalyticsUniversalTracker.prototype.setDimension = function(index, value) {
sendToGa('set', 'dimension' + index, String(value));
Expand Down
7 changes: 7 additions & 0 deletions javascripts/govuk/analytics/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,12 @@
this.classic.setCustomVariable(index, value, name, scope);
};

/*
Add a beacon to track a page in another GA account on another domain.
*/
Tracker.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
this.universal.addLinkedTrackerDomain(trackerId, name, domain);
};

GOVUK.Tracker = Tracker;
})();
21 changes: 21 additions & 0 deletions spec/unit/analytics/TrackerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ describe("GOVUK.Tracker", function() {
});
});

describe('when adding a linked domain', function() {
beforeEach(function() {
tracker = new GOVUK.Tracker(this.config);
});

it('adds a linked domain to universal only', function() {
window._gaq = [];
tracker.addLinkedTrackerDomain('1234', 'test', 'www.example.com');

expect(window._gaq).toEqual([]);
var allArgs = window.ga.calls.allArgs()
expect(allArgs).toContain(['create', '1234', 'auto', {'name': 'test'}]);
expect(allArgs).toContain(['require', 'linker']);
expect(allArgs).toContain(['test.require', 'linker']);
expect(allArgs).toContain(['linker:autoLink', ['www.example.com']]);
expect(allArgs).toContain(['test.linker:autoLink', ['www.example.com']]);
expect(allArgs).toContain(['test.set', 'anonymizeIp', true]);
expect(allArgs).toContain(['test.send', 'pageview']);
});
});

});

0 comments on commit d4997be

Please sign in to comment.