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

Add tapad id submodule #6167

Merged
merged 2 commits into from
Jan 18, 2021
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
3 changes: 2 additions & 1 deletion modules/.submodules.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"idxIdSystem",
"fabrickIdSystem",
"verizonMediaIdSystem",
"pubProvidedIdSystem"
"pubProvidedIdSystem",
"tapadIdSystem"
],
"adpod": [
"freeWheelAdserverVideo",
Expand Down
78 changes: 78 additions & 0 deletions modules/tapadIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { uspDataHandler } from '../src/adapterManager.js';
import { submodule } from '../src/hook.js';
import { getStorageManager } from '../src/storageManager.js';
import * as ajax from '../src/ajax.js'
import * as utils from '../src/utils.js';

export const storage = getStorageManager();
const cookiesMaxAge = 7 * 24 * 60 * 60 * 1000; // 60 days
export const defaultExpirationString = new Date(utils.timestamp() + cookiesMaxAge).toString();
export const pastDateString = new Date(0).toString();
export const tapadCookieKey = 'tapad_id';
export const graphUrl = 'https://realtime-graph-access-zxvhwknfeq-uc.a.run.app/v1/graph';

function saveOnAllStorages(key, value, expiration = defaultExpirationString) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a reason to set a default expiration string, since it's never being used?

storage.setCookie(key, typeof value === 'object' ? JSON.stringify(value) : value, expiration);
storage.setDataInLocalStorage(key, JSON.stringify(value));
Copy link
Collaborator

Choose a reason for hiding this comment

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

why are you checking that value is an object before JSON.stringify for cookies but not local storage?

}

function deleteFromAllStorages(key) {
storage.setCookie(key, undefined, pastDateString);
storage.removeDataFromLocalStorage(key);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In your documentation example (https://github.com/prebid/prebid.github.io/pull/2606/files) you have the publisher set the storage object, yet you are manually storing data here. If storage is set, then the user id module will handle storing and retrieving the id; if you want to do it manually then publishers cannot set storage or you will end up with inconsistencies.

Also, if you are going to manage storage on your own, you need to set an expiration item in the local storage and manually handle throwing it out when reading the data if it's expired.

i would suggest just using the built-in functionality of the user id module to read/write the cached id value.


export const tapadIdSubmodule = {
name: 'tapadId',
/**
* decode the stored id value for passing to bid requests
* @function
* @returns {{tapadId: string} | undefined}
*/
decode(id) {
return { tapadId: id };
},
/*
* @function
* @summary initiate Real Time Graph
* @param {SubmoduleParams} [configParams]
* @param {ConsentData} [consentData]
* @returns {IdResponse }}
*/
getId(config) {
const uspData = uspDataHandler.getConsentData();
if (uspData && uspData !== '1---') {
return { id: undefined };
}
const configParams = config.params || {};
const expiration = config.storage && config.storage.expires
? new Date(utils.timestamp() + config.storage.expires * 24 * 60 * 60 * 1000).toString()
: undefined;

return {
callback: (complete) => {
ajax.ajaxBuilder(10000)(
`${graphUrl}?company_id=${configParams.companyId}&tapad_id_type=TAPAD_ID`,
smenzer marked this conversation as resolved.
Show resolved Hide resolved
{
success: (response) => {
const responseJson = JSON.parse(response);
if (responseJson.hasOwnProperty('tapadId')) {
saveOnAllStorages(tapadCookieKey, responseJson.tapadId, expiration)
complete(responseJson.tapadId)
}
},
error: (_, e) => {
if (e.status === 404) {
deleteFromAllStorages(tapadCookieKey);
complete(undefined);
}
if (e.status === 403) {
utils.logMessage('Invalid Company Id. Contact [email protected] for assistance.')
}
}
}
)
}
}
}
}
submodule('userId', tapadIdSubmodule);
55 changes: 55 additions & 0 deletions test/spec/modules/tapadIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { tapadIdSubmodule, graphUrl, storage, tapadCookieKey, pastDateString, defaultExpirationString } from 'modules/tapadIdSystem.js';
import * as utils from 'src/utils.js';

import { server } from 'test/mocks/xhr.js';

describe('TapadIdSystem', function () {
describe('getId', function() {
let setCookieSpy;
beforeEach(() => {
setCookieSpy = sinon.spy(storage, 'setCookie');
})
afterEach(() => {
setCookieSpy.restore()
})

it('should call to real time graph endpoint and handle valid response', function() {
const callbackSpy = sinon.spy()
const callback = tapadIdSubmodule.getId({
params: { companyId: 12345 }
}).callback
callback(callbackSpy);

const request = server.requests[0]
expect(request.url).to.eq(`${graphUrl}?company_id=12345&tapad_id_type=TAPAD_ID`)

request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ tapadId: 'your-tapad-id' }))
expect(callbackSpy.lastCall.lastArg).to.eq('your-tapad-id');
expect(setCookieSpy.lastCall.calledWith(tapadCookieKey, 'your-tapad-id', defaultExpirationString)).to.be.true
})

it('should remove stored tapadId if not found', function() {
const callbackSpy = sinon.spy()
const callback = tapadIdSubmodule.getId({}).callback
callback(callbackSpy);

const request = server.requests[0]

request.respond(404)
expect(callbackSpy.lastCall.lastArg).to.be.undefined;
expect(setCookieSpy.lastCall.calledWith(tapadCookieKey, undefined, pastDateString)).to.be.true
})
it('should log message with invalid company id', function() {
smenzer marked this conversation as resolved.
Show resolved Hide resolved
const logMessageSpy = sinon.spy(utils, 'logMessage');
const callbackSpy = sinon.spy()
const callback = tapadIdSubmodule.getId({}).callback
callback(callbackSpy);

const request = server.requests[0]

request.respond(403)
expect(logMessageSpy.lastCall.lastArg).to.eq('Invalid Company Id. Contact [email protected] for assistance.')
logMessageSpy.restore()
})
})
})