Skip to content
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

Prebid manager analytics utm tags #4998

Merged
merged 4 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 34 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;
let utmKeys = {utm_source: '', utm_medium: '', utm_campaign: '', utm_term: '', utm_content: ''};
bx2 marked this conversation as resolved.
Show resolved Hide resolved

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

export function collectUtmTagData() {
bx2 marked this conversation as resolved.
Show resolved Hide resolved
let newUtm = false;
let pbUtmTags = {};
try {
for (let prop in utmKeys) {
utmKeys[prop] = utils.getParameterByName(prop);
if (utmKeys[prop] != '') {
newUtm = true;
}
pbUtmTags[prop] = utmKeys[prop];
}

if (newUtm === false) {
for (let prop in utmKeys) {
let itemValue = localStorage.getItem(`pm_${prop}`);
if (itemValue.length !== 0) {
pbUtmTags[prop] = itemValue;
}
}
} else {
for (let prop in utmKeys) {
localStorage.setItem(`pm_${prop}`, utmKeys[prop]);
}
}
} catch (e) {
utils.logInfo(`${analyticsName}Error`, e);
bx2 marked this conversation as resolved.
Show resolved Hide resolved
pbUtmTags['error_utm'] = 1;
}
return pbUtmTags;
}

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

ajax(
Expand Down
26 changes: 26 additions & 0 deletions test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prebidmanagerAnalytics from 'modules/prebidmanagerAnalyticsAdapter.js';
import {expect} from 'chai';
import {server} from 'test/mocks/xhr.js';
import {collectUtmTagData} from '../../../modules/prebidmanagerAnalyticsAdapter.js';
bx2 marked this conversation as resolved.
Show resolved Hide resolved
let events = require('src/events');
let constants = require('src/constants.json');

Expand Down Expand Up @@ -101,4 +102,29 @@ 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');
});
it('should build utm data from local storage', function () {
let utmTagData = collectUtmTagData();
expect(utmTagData.utm_source).to.equal('utm_source');
expect(utmTagData.utm_medium).to.equal('utm_medium');
expect(utmTagData.utm_campaign).to.equal('utm_camp');
expect(utmTagData.utm_term).to.equal('');
expect(utmTagData.utm_content).to.equal('');
});
});
});