-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add tapad id submodule #6167
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
storage.setCookie(key, typeof value === 'object' ? JSON.stringify(value) : value, expiration); | ||
storage.setDataInLocalStorage(key, JSON.stringify(value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you checking that |
||
} | ||
|
||
function deleteFromAllStorages(key) { | ||
storage.setCookie(key, undefined, pastDateString); | ||
storage.removeDataFromLocalStorage(key); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); |
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() | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
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?