Skip to content

Commit

Permalink
Britepool user id module update (#5750)
Browse files Browse the repository at this point in the history
* adding britepool_pubparams dynamic variable lookup and merge into submodule params if exists

* adding support for gdpr consent string in query params

* adding tests for britepool_pubparams

* adding doc block for consentData

* adding pixel on success

* - ensures id resolution pixel only fires when authoritative information is not present
 - adds tests for id resolution pixel
  • Loading branch information
bansawbanchee authored Sep 23, 2020
1 parent 540c7de commit 1b932a0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
17 changes: 13 additions & 4 deletions modules/britepoolIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as utils from '../src/utils.js'
import {ajax} from '../src/ajax.js';
import {submodule} from '../src/hook.js';
const PIXEL = 'https://px.britepool.com/new?partner_id=t';

/** @type {Submodule} */
export const britepoolIdSubmodule = {
Expand All @@ -28,7 +29,8 @@ export const britepoolIdSubmodule = {
/**
* Performs action to obtain id and return a value in the callback's response argument
* @function
* @param {SubmoduleParams} [configParams]
* @param {SubmoduleParams} [submoduleConfigParams]
* @param {ConsentData|undefined} consentData
* @returns {function(callback:function)}
*/
getId(submoduleConfigParams, consentData) {
Expand All @@ -44,6 +46,9 @@ export const britepoolIdSubmodule = {
};
}
}
if (utils.isEmpty(params)) {
utils.triggerPixel(PIXEL);
}
// Return for async operation
return {
callback: function(callback) {
Expand Down Expand Up @@ -79,13 +84,17 @@ export const britepoolIdSubmodule = {
},
/**
* Helper method to create params for our API call
* @param {SubmoduleParams} [configParams]
* @param {SubmoduleParams} [submoduleConfigParams]
* @param {ConsentData|undefined} consentData
* @returns {object} Object with parsed out params
*/
createParams(submoduleConfigParams, consentData) {
const hasGdprData = consentData && typeof consentData.gdprApplies === 'boolean' && consentData.gdprApplies;
const gdprConsentString = hasGdprData ? consentData.consentString : undefined;
let errors = [];
const headers = {};
let params = Object.assign({}, submoduleConfigParams);
const dynamicVars = typeof britepool_pubparams !== 'undefined' ? britepool_pubparams : {}; // eslint-disable-line camelcase, no-undef
let params = Object.assign({}, submoduleConfigParams, dynamicVars);
if (params.getter) {
// Custom getter will not require other params
if (typeof params.getter !== 'function') {
Expand All @@ -98,7 +107,7 @@ export const britepoolIdSubmodule = {
headers['x-api-key'] = params.api_key;
}
}
const url = params.url || 'https://api.britepool.com/v1/britepool/id';
const url = params.url || `https://api.britepool.com/v1/britepool/id${gdprConsentString ? '?gdprString=' + encodeURIComponent(gdprConsentString) : ''}`;
const getter = params.getter;
delete params.api_key;
delete params.url;
Expand Down
64 changes: 63 additions & 1 deletion test/spec/modules/britepoolIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import {britepoolIdSubmodule} from 'modules/britepoolIdSystem.js';
import * as utils from '../../../src/utils.js';

describe('BritePool Submodule', () => {
const api_key = '1111';
Expand All @@ -16,6 +16,32 @@ describe('BritePool Submodule', () => {
};
};

let triggerPixelStub;

beforeEach(function (done) {
triggerPixelStub = sinon.stub(utils, 'triggerPixel');
done();
});

afterEach(function () {
triggerPixelStub.restore();
});

it('trigger id resolution pixel when no identifiers set', () => {
britepoolIdSubmodule.getId({});
expect(triggerPixelStub.called).to.be.true;
});

it('trigger id resolution pixel when no identifiers set with api_key param', () => {
britepoolIdSubmodule.getId({ api_key });
expect(triggerPixelStub.called).to.be.true;
});

it('does not trigger id resolution pixel when identifiers set', () => {
britepoolIdSubmodule.getId({ api_key, aaid });
expect(triggerPixelStub.called).to.be.false;
});

it('sends x-api-key in header and one identifier', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid });
assert(errors.length === 0, errors);
Expand Down Expand Up @@ -43,6 +69,42 @@ describe('BritePool Submodule', () => {
expect(params.url).to.be.undefined;
});

it('test gdpr consent string in url', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid }, { gdprApplies: true, consentString: 'expectedConsentString' });
expect(url).to.equal('https://api.britepool.com/v1/britepool/id?gdprString=expectedConsentString');
});

it('test gdpr consent string not in url if gdprApplies false', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid }, { gdprApplies: false, consentString: 'expectedConsentString' });
expect(url).to.equal('https://api.britepool.com/v1/britepool/id');
});

it('test gdpr consent string not in url if consent string undefined', () => {
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid }, { gdprApplies: true, consentString: undefined });
expect(url).to.equal('https://api.britepool.com/v1/britepool/id');
});

it('dynamic pub params should be added to params', () => {
window.britepool_pubparams = { ppid: '12345' };
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid });
expect(params).to.eql({ aaid, ppid: '12345' });
window.britepool_pubparams = undefined;
});

it('dynamic pub params should override submodule params', () => {
window.britepool_pubparams = { ppid: '67890' };
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, ppid: '12345' });
expect(params).to.eql({ ppid: '67890' });
window.britepool_pubparams = undefined;
});

it('if dynamic pub params undefined do nothing', () => {
window.britepool_pubparams = undefined;
const { params, headers, url, errors } = britepoolIdSubmodule.createParams({ api_key, aaid });
expect(params).to.eql({ aaid });
window.britepool_pubparams = undefined;
});

it('test getter override with value', () => {
const { params, headers, url, getter, errors } = britepoolIdSubmodule.createParams({ api_key, aaid, url: url_override, getter: getter_override });
expect(getter).to.equal(getter_override);
Expand Down

0 comments on commit 1b932a0

Please sign in to comment.