Skip to content

Commit

Permalink
Prebid manager analytics utm tags (prebid#4998)
Browse files Browse the repository at this point in the history
* prebidmanager add utm tags support

* prebidmanager add utm tags support tests

* prebidmanager add utm tags support tests

* prebidmanager add utm tags support fixes

Co-authored-by: apuzanova <[email protected]>
  • Loading branch information
2 people authored and iggyfisk committed Jun 22, 2020
1 parent 341497a commit e9e27a6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
34 changes: 33 additions & 1 deletion modules/prebidmanagerAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _startAuction = 0;
var _bidRequestTimeout = 0;
let flushInterval;
var pmAnalyticsEnabled = false;
const utmTags = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];

var w = window;
var d = document;
Expand Down Expand Up @@ -68,6 +69,36 @@ prebidmanagerAnalytics.disableAnalytics = function() {
prebidmanagerAnalytics.originDisableAnalytics();
};

function collectUtmTagData() {
let newUtm = false;
let pmUtmTags = {};
try {
utmTags.forEach(function (utmKey) {
let utmValue = utils.getParameterByName(utmKey);
if (utmValue !== '') {
newUtm = true;
}
pmUtmTags[utmKey] = utmValue;
});
if (newUtm === false) {
utmTags.forEach(function (utmKey) {
let itemValue = localStorage.getItem(`pm_${utmKey}`);
if (itemValue.length !== 0) {
pmUtmTags[utmKey] = itemValue;
}
});
} else {
utmTags.forEach(function (utmKey) {
localStorage.setItem(`pm_${utmKey}`, pmUtmTags[utmKey]);
});
}
} catch (e) {
utils.logError(`${analyticsName}Error`, e);
pmUtmTags['error_utm'] = 1;
}
return pmUtmTags;
}

function flush() {
if (!pmAnalyticsEnabled) {
return;
Expand All @@ -78,7 +109,8 @@ function flush() {
pageViewId: _pageViewId,
ver: _VERSION,
bundleId: initOptions.bundleId,
events: _eventQueue
events: _eventQueue,
utmTags: collectUtmTagData(),
};

ajax(
Expand Down
34 changes: 34 additions & 0 deletions test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,38 @@ describe('Prebid Manager Analytics Adapter', function () {
sinon.assert.callCount(prebidmanagerAnalytics.track, 6);
});
});

describe('build utm tag data', function () {
beforeEach(function () {
localStorage.setItem('pm_utm_source', 'utm_source');
localStorage.setItem('pm_utm_medium', 'utm_medium');
localStorage.setItem('pm_utm_campaign', 'utm_camp');
localStorage.setItem('pm_utm_term', '');
localStorage.setItem('pm_utm_content', '');
});
afterEach(function () {
localStorage.removeItem('pm_utm_source');
localStorage.removeItem('pm_utm_medium');
localStorage.removeItem('pm_utm_campaign');
localStorage.removeItem('pm_utm_term');
localStorage.removeItem('pm_utm_content');
prebidmanagerAnalytics.disableAnalytics()
});
it('should build utm data from local storage', function () {
prebidmanagerAnalytics.enableAnalytics({
provider: 'prebidmanager',
options: {
bundleId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}
});

const pmEvents = JSON.parse(server.requests[0].requestBody.substring(2));

expect(pmEvents.utmTags.utm_source).to.equal('utm_source');
expect(pmEvents.utmTags.utm_medium).to.equal('utm_medium');
expect(pmEvents.utmTags.utm_campaign).to.equal('utm_camp');
expect(pmEvents.utmTags.utm_term).to.equal('');
expect(pmEvents.utmTags.utm_content).to.equal('');
});
});
});

0 comments on commit e9e27a6

Please sign in to comment.