Skip to content

Rubicon analytics v2 #5698

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

Merged
merged 15 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions modules/rubiconAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const cache = {
gpt: {},
};

const BID_REJECTED_IPF = 'rejected-ipf';

export function getHostNameFromReferer(referer) {
try {
rubiconAdapter.referrerHostname = utils.parseUrl(referer, {noDecodeWholeURL: true}).hostname;
Expand Down Expand Up @@ -368,7 +370,7 @@ function setRubiconAliases(aliasRegistry) {
}

function getRpaCookie() {
let encodedCookie = storage.getCookie(COOKIE_NAME);
let encodedCookie = storage.getDataFromLocalStorage(COOKIE_NAME);
if (encodedCookie) {
try {
return JSON.parse(window.atob(encodedCookie));
Expand All @@ -381,7 +383,7 @@ function getRpaCookie() {

function setRpaCookie(decodedCookie) {
try {
storage.setCookie(COOKIE_NAME, window.btoa(JSON.stringify(decodedCookie)));
storage.setDataInLocalStorage(COOKIE_NAME, window.btoa(JSON.stringify(decodedCookie)));
} catch (e) {
utils.logError(`Rubicon Analytics: Unable to encode ${COOKIE_NAME} value: `, e);
}
Expand Down Expand Up @@ -480,6 +482,7 @@ let rubiconAdapter = Object.assign({}, baseAdapter, {
disableAnalytics() {
this.getUrl = baseAdapter.getUrl;
accountId = null;
cache.gpt.registered = false;
baseAdapter.disableAnalytics.apply(this, arguments);
},
track({eventType, args}) {
Expand All @@ -499,11 +502,12 @@ let rubiconAdapter = Object.assign({}, baseAdapter, {
cacheEntry.floorData = {...floorData};
}
cacheEntry.gdprConsent = utils.deepAccess(args, 'bidderRequests.0.gdprConsent');
cacheEntry.session = storage.cookiesAreEnabled() && updateRpaCookie();
cacheEntry.session = storage.localStorageIsEnabled() && updateRpaCookie();
cache.auctions[args.auctionId] = cacheEntry;
// register to listen to gpt events if not done yet
if (!cache.gpt.registered && utils.isGptPubadsDefined()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

where is cache.gpt.registered set to true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it is not, must have left it out or removed it accidentally! Thanks, good catch!

subscribeToGamSlots();
cache.gpt.registered = true;
}
break;
case BID_REQUESTED:
Expand Down Expand Up @@ -608,7 +612,7 @@ let rubiconAdapter = Object.assign({}, baseAdapter, {
delete bid.error; // it's possible for this to be set by a previous timeout
break;
case NO_BID:
bid.status = args.status === BID_REJECTED ? 'rejected-ipf' : 'no-bid';
bid.status = args.status === BID_REJECTED ? BID_REJECTED_IPF : 'no-bid';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking args.status === BID_REJECTED_IPF as the constant. Cause what if new code starts rejecting bids for a reason other than ipf? But this would require updates in other places.

Copy link
Collaborator

Choose a reason for hiding this comment

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

but we can fix this later. for now this is ok

delete bid.error;
break;
default:
Expand Down
52 changes: 26 additions & 26 deletions test/spec/modules/rubiconAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,15 @@ function performStandardAuction(gptEvents) {
describe('rubicon analytics adapter', function () {
let sandbox;
let clock;
let getCookieStub, setCookieStub, cookiesAreEnabledStub;
let getDataFromLocalStorageStub, setDataInLocalStorageStub, localStorageIsEnabledStub;
beforeEach(function () {
getCookieStub = sinon.stub(storage, 'getCookie');
setCookieStub = sinon.stub(storage, 'setCookie');
cookiesAreEnabledStub = sinon.stub(storage, 'cookiesAreEnabled');
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage');
setDataInLocalStorageStub = sinon.stub(storage, 'setDataInLocalStorage');
localStorageIsEnabledStub = sinon.stub(storage, 'localStorageIsEnabled');
mockGpt.disable();
sandbox = sinon.sandbox.create();

cookiesAreEnabledStub.returns(true);
localStorageIsEnabledStub.returns(true);

sandbox.stub(events, 'getEvents').returns([]);

Expand All @@ -530,9 +530,9 @@ describe('rubicon analytics adapter', function () {
sandbox.restore();
config.resetConfig();
mockGpt.enable();
getCookieStub.restore();
setCookieStub.restore();
cookiesAreEnabledStub.restore();
getDataFromLocalStorageStub.restore();
setDataInLocalStorageStub.restore();
localStorageIsEnabledStub.restore();
});

it('should require accountId', function () {
Expand Down Expand Up @@ -843,8 +843,8 @@ describe('rubicon analytics adapter', function () {
prebidGlobal.rp = pvid = kvps = undefined;
});

it('shouldnot log any session data if cookies are not enabled', function () {
cookiesAreEnabledStub.returns(false);
it('should not log any session data if local storage is not enabled', function () {
localStorageIsEnabledStub.returns(false);

let expectedMessage = utils.deepClone(ANALYTICS_MESSAGE);
delete expectedMessage.session;
Expand Down Expand Up @@ -885,20 +885,20 @@ describe('rubicon analytics adapter', function () {
expect(message).to.deep.equal(expectedMessage);
});

it('should pick up existing cookie and use its values', function () {
// set some cookie
let inputCookie = {
it('should pick up existing localStorage and use its values', function () {
// set some localStorage
let inputlocalStorage = {
id: '987654',
start: 1519766113781, // 15 mins before "now"
expires: 1519787713781, // six hours later
lastSeen: 1519766113781,
fpkvs: { source: 'tw' }
};
getCookieStub.withArgs('rpaSession').returns(btoa(JSON.stringify(inputCookie)));
getDataFromLocalStorageStub.withArgs('rpaSession').returns(btoa(JSON.stringify(inputlocalStorage)));

pvid = '1a2b3c';
kvps = {
link: 'email' // should merge this with what is in the cookie!
link: 'email' // should merge this with what is in the localStorage!
};

performStandardAuction();
Expand All @@ -922,7 +922,7 @@ describe('rubicon analytics adapter', function () {

let calledWith;
try {
calledWith = JSON.parse(atob(setCookieStub.getCall(0).args[1]));
calledWith = JSON.parse(atob(setDataInLocalStorageStub.getCall(0).args[1]));
} catch (e) {
calledWith = {};
}
Expand All @@ -938,19 +938,19 @@ describe('rubicon analytics adapter', function () {
});

it('should throw out session if lastSeen > 30 mins ago and create new one', function () {
// set some cookie
let inputCookie = {
// set some localStorage
let inputlocalStorage = {
id: '987654',
start: 1519764313781, // 45 mins before "now"
expires: 1519785913781, // six hours later
lastSeen: 1519764313781, // 45 mins before "now"
fpkvs: { source: 'tw' }
};
getCookieStub.withArgs('rpaSession').returns(btoa(JSON.stringify(inputCookie)));
getDataFromLocalStorageStub.withArgs('rpaSession').returns(btoa(JSON.stringify(inputlocalStorage)));

pvid = '1a2b3c';
kvps = {
link: 'email' // should merge this with what is in the cookie!
link: 'email' // should merge this with what is in the localStorage!
};

performStandardAuction();
Expand All @@ -971,7 +971,7 @@ describe('rubicon analytics adapter', function () {

let calledWith;
try {
calledWith = JSON.parse(atob(setCookieStub.getCall(0).args[1]));
calledWith = JSON.parse(atob(setDataInLocalStorageStub.getCall(0).args[1]));
} catch (e) {
calledWith = {};
}
Expand All @@ -987,19 +987,19 @@ describe('rubicon analytics adapter', function () {
});

it('should throw out session if past expires time and create new one', function () {
// set some cookie
let inputCookie = {
// set some localStorage
let inputlocalStorage = {
id: '987654',
start: 1519745353781, // 6 hours before "expires"
expires: 1519766953781, // little more than six hours ago
lastSeen: 1519767008781, // 5 seconds ago
fpkvs: { source: 'tw' }
};
getCookieStub.withArgs('rpaSession').returns(btoa(JSON.stringify(inputCookie)));
getDataFromLocalStorageStub.withArgs('rpaSession').returns(btoa(JSON.stringify(inputlocalStorage)));

pvid = '1a2b3c';
kvps = {
link: 'email' // should merge this with what is in the cookie!
link: 'email' // should merge this with what is in the localStorage!
};

performStandardAuction();
Expand All @@ -1020,7 +1020,7 @@ describe('rubicon analytics adapter', function () {

let calledWith;
try {
calledWith = JSON.parse(atob(setCookieStub.getCall(0).args[1]));
calledWith = JSON.parse(atob(setDataInLocalStorageStub.getCall(0).args[1]));
} catch (e) {
calledWith = {};
}
Expand Down