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 : synchronize mguid from third party cookie to first party cookie #10927

Merged
merged 4 commits into from
Jan 26, 2024
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
31 changes: 27 additions & 4 deletions modules/discoveryBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ const MEDIATYPE = [BANNER, NATIVE];

/* ----- _ss_pp_id:start ------ */
const COOKIE_KEY_SSPPID = '_ss_pp_id';
const COOKIE_KEY_MGUID = '__mguid_';
export const COOKIE_KEY_MGUID = '__mguid_';
const COOKIE_KEY_PMGUID = '__pmguid_';
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 NATIVERET = {
id: 'id',
Expand Down Expand Up @@ -126,10 +127,8 @@ export const getPmgUID = () => {
let pmgUid = storage.getCookie(COOKIE_KEY_PMGUID);
if (!pmgUid) {
pmgUid = utils.generateUUID();
const date = new Date();
date.setTime(date.getTime() + COOKIE_RETENTION_TIME);
try {
storage.setCookie(COOKIE_KEY_PMGUID, pmgUid, date.toUTCString());
storage.setCookie(COOKIE_KEY_PMGUID, pmgUid, getCurrentTimeToUTCString());
} catch (e) {}
}
return pmgUid;
Expand Down Expand Up @@ -295,6 +294,16 @@ function getReferrer(bidRequest = {}, bidderRequest = {}) {
return pageUrl;
}

/**
* get current time to UTC string
* @returns utc string
*/
export function getCurrentTimeToUTCString() {
const date = new Date();
date.setTime(date.getTime() + COOKIE_RETENTION_TIME);
return date.toUTCString();
}

/**
* format imp ad test ext params
*
Expand Down Expand Up @@ -640,6 +649,20 @@ export const spec = {
}

if (syncOptions.iframeEnabled) {
window.addEventListener('message', function handler(event) {
if (!event.data || event.origin != THIRD_PARTY_COOKIE_ORIGIN) {
return;
}

this.removeEventListener('message', handler);

event.stopImmediatePropagation();

const response = event.data;
if (!response.optout && response.mguid) {
storage.setCookie(COOKIE_KEY_MGUID, response.mguid, getCurrentTimeToUTCString());
}
}, true);
return [
{
type: 'iframe',
Expand Down
68 changes: 67 additions & 1 deletion test/spec/modules/discoveryBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { expect } from 'chai';
import { spec, getPmgUID, storage, getPageTitle, getPageDescription, getPageKeywords, getConnectionDownLink } from 'modules/discoveryBidAdapter.js';
import {
spec,
getPmgUID,
storage,
getPageTitle,
getPageDescription,
getPageKeywords,
getConnectionDownLink,
THIRD_PARTY_COOKIE_ORIGIN,
COOKIE_KEY_MGUID,
getCurrentTimeToUTCString
} from 'modules/discoveryBidAdapter.js';
import * as utils from 'src/utils.js';

describe('discovery:BidAdapterTests', function () {
Expand Down Expand Up @@ -443,5 +454,60 @@ describe('discovery Bid Adapter Tests', function () {
expect(result).to.be.undefined;
});
});

describe('getUserSyncs with message event listener', function() {
function messageHandler(event) {
if (!event.data || event.origin !== THIRD_PARTY_COOKIE_ORIGIN) {
return;
}

window.removeEventListener('message', messageHandler, true);
event.stopImmediatePropagation();

const response = event.data;
if (!response.optout && response.mguid) {
storage.setCookie(COOKIE_KEY_MGUID, response.mguid, getCurrentTimeToUTCString());
}
}

let sandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox.stub(storage, 'setCookie');
sandbox.stub(window, 'removeEventListener');
});

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

it('should set a cookie when a valid message is received', () => {
const fakeEvent = {
data: { optout: '', mguid: '12345' },
origin: THIRD_PARTY_COOKIE_ORIGIN,
stopImmediatePropagation: sinon.spy()
};

messageHandler(fakeEvent);

expect(fakeEvent.stopImmediatePropagation.calledOnce).to.be.true;
expect(window.removeEventListener.calledWith('message', messageHandler, true)).to.be.true;
expect(storage.setCookie.calledWith(COOKIE_KEY_MGUID, '12345', sinon.match.string)).to.be.true;
});
it('should not do anything when an invalid message is received', () => {
const fakeEvent = {
data: null,
origin: 'http://invalid-origin.com',
stopImmediatePropagation: sinon.spy()
};

messageHandler(fakeEvent);

expect(fakeEvent.stopImmediatePropagation.notCalled).to.be.true;
expect(window.removeEventListener.notCalled).to.be.true;
expect(storage.setCookie.notCalled).to.be.true;
});
});
});
});