Skip to content

Commit

Permalink
Discovery Bid Adapter : synchronize mguid from third party cookie to …
Browse files Browse the repository at this point in the history
…first party cookie (prebid#10927)

* Discovery Bid Adapter : add title, desc, keywords, hLen, nbw, hc, dm  add unit test resolve conflict

* Discovery Bid Adapter : add title, desc, keywords, hLen, nbw, hc, dm  add unit test

* Discovery Bid Adapter : synchronize mguid from third party cookie to first party cookie

---------

Co-authored-by: lvhuixin <lvhuixin@baidu.com>
  • Loading branch information
2 people authored and Kevin Siow committed Mar 1, 2024
1 parent 968f82b commit 2ac71f2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
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 @@ -642,6 +651,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 @@ -523,5 +534,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;
});
});
});
});

0 comments on commit 2ac71f2

Please sign in to comment.