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

Discovery Bid Adapter : build UTM tag data #11317

Merged
merged 11 commits into from
Apr 12, 2024
21 changes: 21 additions & 0 deletions modules/discoveryBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const COOKIE_RETENTION_TIME = 365 * 24 * 60 * 60 * 1000; // 1 year
const COOKY_SYNC_IFRAME_URL = 'https://asset.popin.cc/js/cookieSync.html';
export const THIRD_PARTY_COOKIE_ORIGIN = 'https://asset.popin.cc';

const UTM_KEY = '_ss_pp_utm';
let UTMValue = {};

const NATIVERET = {
id: 'id',
bidfloor: 0,
Expand Down Expand Up @@ -409,6 +412,20 @@ function getItems(validBidRequests, bidderRequest) {
return items;
}

export const buildUTMTagData = (url) => {
if (!storage.cookiesAreEnabled()) return;
const urlParams = utils.parseUrl(url).search;
const UTMParams = {};
Object.keys(urlParams).forEach(key => {
if (/^utm_/.test(key)) {
UTMParams[key] = urlParams[key];
}
});
UTMValue = JSON.parse(storage.getCookie(UTM_KEY) || '{}');
Object.assign(UTMValue, UTMParams);
storage.setCookie(UTM_KEY, JSON.stringify(UTMValue), getCurrentTimeToUTCString());
}

/**
* get rtb qequest params
*
Expand Down Expand Up @@ -443,6 +460,10 @@ function getParam(validBidRequests, bidderRequest) {
const desc = getPageDescription();
const keywords = getPageKeywords();

try {
buildUTMTagData(page);
} catch (error) { }

if (items && items.length) {
let c = {
// TODO: fix auctionId leak: https://github.com/prebid/Prebid.js/issues/9781
Expand Down
36 changes: 35 additions & 1 deletion test/spec/modules/discoveryBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getConnectionDownLink,
THIRD_PARTY_COOKIE_ORIGIN,
COOKIE_KEY_MGUID,
getCurrentTimeToUTCString
getCurrentTimeToUTCString,
buildUTMTagData
} from 'modules/discoveryBidAdapter.js';
import * as utils from 'src/utils.js';

Expand Down Expand Up @@ -252,6 +253,39 @@ describe('discovery:BidAdapterTests', function () {
expect(storage.setCookie.calledOnce).to.be.false;
});
})
describe('buildUTMTagData function', function() {
let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(storage, 'getCookie');
sandbox.stub(storage, 'setCookie');
sandbox.stub(utils, 'parseUrl').returns({
search: {
utm_source: 'example.com'
}
});
sandbox.stub(storage, 'cookiesAreEnabled');
})

afterEach(() => {
sandbox.restore();
});

it('should set UTM cookie', () => {
storage.cookiesAreEnabled.callsFake(() => true);
storage.getCookie.callsFake(() => null);
buildUTMTagData();
expect(storage.setCookie.calledOnce).to.be.true;
});

it('should not set UTM when cookies are not enabled', () => {
storage.cookiesAreEnabled.callsFake(() => false);
storage.getCookie.callsFake(() => null);
buildUTMTagData();
expect(storage.setCookie.calledOnce).to.be.false;
});
})
});

it('discovery:validate_response_params', function () {
Expand Down