-
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
Adquery ID System: add new ID module #7852
Merged
smenzer
merged 2 commits into
prebid:master
from
adquery:adquery-userId-module-adqueryId
Dec 22, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* This module adds AdqueryID to the User ID module | ||
* The {@link module:modules/userId} module is required | ||
* @module modules/adqueryIdSystem | ||
* @requires module:modules/userId | ||
*/ | ||
|
||
import {ajax} from '../src/ajax.js'; | ||
import {getStorageManager} from '../src/storageManager.js'; | ||
import {submodule} from '../src/hook.js'; | ||
import * as utils from '../src/utils.js'; | ||
|
||
const MODULE_NAME = 'adqueryId'; | ||
const AU_GVLID = 902; | ||
|
||
export const storage = getStorageManager(AU_GVLID, 'adquery'); | ||
|
||
/** | ||
* Param or default. | ||
* @param {String} param | ||
* @param {String} defaultVal | ||
*/ | ||
function paramOrDefault(param, defaultVal, arg) { | ||
if (utils.isFn(param)) { | ||
return param(arg); | ||
} else if (utils.isStr(param)) { | ||
return param; | ||
} | ||
return defaultVal; | ||
} | ||
|
||
/** @type {Submodule} */ | ||
export const adqueryIdSubmodule = { | ||
/** | ||
* used to link submodule with config | ||
* @type {string} | ||
*/ | ||
name: MODULE_NAME, | ||
/** | ||
* decode the stored id value for passing to bid requests | ||
* @function | ||
* @param {{value:string}} value | ||
* @returns {{qid:Object}} | ||
*/ | ||
decode(value) { | ||
let qid = storage.getDataFromLocalStorage('qid'); | ||
if (utils.isStr(qid)) { | ||
return {qid: qid}; | ||
} | ||
return (value && typeof value['qid'] === 'string') ? { 'qid': value['qid'] } : undefined; | ||
}, | ||
/** | ||
* performs action to obtain id and return a value in the callback's response argument | ||
* @function | ||
* @param {SubmoduleConfig} [config] | ||
* @returns {IdResponse|undefined} | ||
*/ | ||
getId(config) { | ||
if (!utils.isPlainObject(config.params)) { | ||
config.params = {}; | ||
} | ||
const url = paramOrDefault(config.params.url, | ||
`https://bidder.adquery.io/prebid/qid`, | ||
config.params.urlArg); | ||
|
||
const resp = function (callback) { | ||
let qid = storage.getDataFromLocalStorage('qid'); | ||
if (utils.isStr(qid)) { | ||
const responseObj = {qid: qid}; | ||
callback(responseObj); | ||
} else { | ||
const callbacks = { | ||
success: response => { | ||
let responseObj; | ||
if (response) { | ||
try { | ||
responseObj = JSON.parse(response); | ||
} catch (error) { | ||
utils.logError(error); | ||
} | ||
} | ||
callback(responseObj); | ||
}, | ||
error: error => { | ||
utils.logError(`${MODULE_NAME}: ID fetch encountered an error`, error); | ||
callback(); | ||
} | ||
}; | ||
ajax(url, callbacks, undefined, {method: 'GET'}); | ||
} | ||
}; | ||
return {callback: resp}; | ||
} | ||
}; | ||
|
||
submodule('userId', adqueryIdSubmodule); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { adqueryIdSubmodule, storage } from 'modules/adqueryIdSystem.js'; | ||
import { server } from 'test/mocks/xhr.js'; | ||
|
||
describe('AdqueryIdSystem', function () { | ||
describe('getId', function() { | ||
let getDataFromLocalStorageStub; | ||
|
||
beforeEach(function() { | ||
getDataFromLocalStorageStub = sinon.stub(storage, 'getDataFromLocalStorage'); | ||
}); | ||
|
||
afterEach(function () { | ||
getDataFromLocalStorageStub.restore(); | ||
}); | ||
|
||
it('gets a adqueryId', function() { | ||
const config = { | ||
params: {} | ||
}; | ||
const callbackSpy = sinon.spy(); | ||
const callback = adqueryIdSubmodule.getId(config).callback; | ||
callback(callbackSpy); | ||
const request = server.requests[0]; | ||
expect(request.url).to.eq(`https://bidder.adquery.io/prebid/qid`); | ||
request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ qid: 'qid' })); | ||
expect(callbackSpy.lastCall.lastArg).to.deep.equal({qid: 'qid'}); | ||
}); | ||
|
||
it('gets a cached adqueryId', function() { | ||
const config = { | ||
params: {} | ||
}; | ||
getDataFromLocalStorageStub.withArgs('qid').returns('qid'); | ||
|
||
const callbackSpy = sinon.spy(); | ||
const callback = adqueryIdSubmodule.getId(config).callback; | ||
callback(callbackSpy); | ||
expect(callbackSpy.lastCall.lastArg).to.deep.equal({qid: 'qid'}); | ||
}); | ||
|
||
it('allows configurable id url', function() { | ||
const config = { | ||
params: { | ||
url: 'https://bidder.adquery.io/prebid/qid' | ||
} | ||
}; | ||
const callbackSpy = sinon.spy(); | ||
const callback = adqueryIdSubmodule.getId(config).callback; | ||
callback(callbackSpy); | ||
const request = server.requests[0]; | ||
expect(request.url).to.eq('https://bidder.adquery.io/prebid/qid'); | ||
request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ qid: 'qid' })); | ||
expect(callbackSpy.lastCall.lastArg).to.deep.equal({qid: 'qid'}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
you should probably test with a value that isn't the default to prove that you can properly override
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.
I added missing files and fixed test