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

Prebidmanager analytics adapter: fix console error when utm is null and collect page info #6002

Merged
Show file tree
Hide file tree
Changes from all 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
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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to add a unit test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a test in test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js


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);
});
});
});