Skip to content

Commit

Permalink
Prebidmanager analytics adapter: fix console error when utm is null a…
Browse files Browse the repository at this point in the history
…nd collect page info (#6002)

* Fix PrebidManager analytics console error when utm data is null

* collect pageInfo in PrebidManager analytics adapter

* minor edit + add test for pageInfo in PrebidManager analytics adapter

Co-authored-by: apuzanova <apuzanova@asteriosoft.com>
  • Loading branch information
Prebid-Manager and apuzanova authored Dec 1, 2020
1 parent 4638dff commit 6f147c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
13 changes: 12 additions & 1 deletion modules/prebidmanagerAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function collectUtmTagData() {
if (newUtm === false) {
utmTags.forEach(function (utmKey) {
let itemValue = localStorage.getItem(`pm_${utmKey}`);
if (itemValue.length !== 0) {
if (itemValue && itemValue.length !== 0) {
pmUtmTags[utmKey] = itemValue;
}
});
Expand All @@ -99,6 +99,16 @@ function collectUtmTagData() {
return pmUtmTags;
}

function collectPageInfo() {
const pageInfo = {
domain: window.location.hostname,
}
if (document.referrer) {
pageInfo.referrerDomain = utils.parseUrl(document.referrer).hostname;
}
return pageInfo;
}

function flush() {
if (!pmAnalyticsEnabled) {
return;
Expand All @@ -111,6 +121,7 @@ function flush() {
bundleId: initOptions.bundleId,
events: _eventQueue,
utmTags: collectUtmTagData(),
pageInfo: collectPageInfo(),
};

ajax(
Expand Down
23 changes: 22 additions & 1 deletion test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import prebidmanagerAnalytics from 'modules/prebidmanagerAnalyticsAdapter.js';
import {expect} from 'chai';
import {server} from 'test/mocks/xhr.js';
import * as utils from 'src/utils.js';

let events = require('src/events');
let constants = require('src/constants.json');

Expand Down Expand Up @@ -98,7 +100,7 @@ describe('Prebid Manager Analytics Adapter', function () {
events.emit(constants.EVENTS.AUCTION_END, {});
events.emit(constants.EVENTS.BID_TIMEOUT, {});

sinon.assert.callCount(prebidmanagerAnalytics.track, 7);
sinon.assert.callCount(prebidmanagerAnalytics.track, 6);
});
});

Expand Down Expand Up @@ -135,4 +137,23 @@ describe('Prebid Manager Analytics Adapter', function () {
expect(pmEvents.utmTags.utm_content).to.equal('');
});
});

describe('build page info', function () {
afterEach(function () {
prebidmanagerAnalytics.disableAnalytics()
});
it('should build page info', function () {
prebidmanagerAnalytics.enableAnalytics({
provider: 'prebidmanager',
options: {
bundleId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}
});

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

expect(pmEvents.pageInfo.domain).to.equal(window.location.hostname);
expect(pmEvents.pageInfo.referrerDomain).to.equal(utils.parseUrl(document.referrer).hostname);
});
});
});

0 comments on commit 6f147c6

Please sign in to comment.