Skip to content

Commit

Permalink
add no track logic, and make account id be string
Browse files Browse the repository at this point in the history
  • Loading branch information
moeroach94 committed Aug 31, 2023
1 parent 07cbb9a commit ef69bfc
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 16 deletions.
27 changes: 23 additions & 4 deletions modules/tapadRtdProvider.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { submodule } from '../src/hook.js';
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
import { isArray, isPlainObject, mergeDeep, safeJSONParse, timestamp } from '../src/utils.js';
import {
isArray,
isPlainObject,
isStr,
mergeDeep,
safeJSONParse,
timestamp
} from '../src/utils.js';
import { ajax } from '../src/ajax.js';

export const SUBMODULE_NAME = 'tapad_rtd';
export const TAPAD_RTD_DATA_KEY = 'tapad_rtd_data';
export const TAPAD_RTD_EXPIRATION_KEY = 'tapad_rtd_expiration';
export const TAPAD_RTD_STALE_KEY = 'tapad_rtd_stale';
export const TAPAD_RTD_NO_TRACK_KEY = 'tapad_rtd_no_track';
const TAPAD_RTD_URL = 'https://rtid.tapad.com'
const storage = getStorageManager({ moduleType: MODULE_TYPE_RTD, moduleName: SUBMODULE_NAME });

Expand All @@ -23,8 +31,9 @@ export const tapadRtdObj = {
const dataEnvelope = storage.getDataFromLocalStorage(TAPAD_RTD_DATA_KEY, null);
const stale = storage.getDataFromLocalStorage(TAPAD_RTD_STALE_KEY, null);
const expired = storage.getDataFromLocalStorage(TAPAD_RTD_EXPIRATION_KEY, null);
const noTrack = storage.getDataFromLocalStorage(TAPAD_RTD_NO_TRACK_KEY, null);
const now = timestamp()
if (dataEnvelope == null || now > new Date(expired).getTime()) {
if (now > new Date(expired).getTime() || (noTrack == null && dataEnvelope == null)) {
// request data envelope and don't manipulate bids
tapadRtdObj.requestDataEnvelope(config, userConsent)
done();
Expand All @@ -34,6 +43,10 @@ export const tapadRtdObj = {
// request data envelope and manipulate bids
tapadRtdObj.requestDataEnvelope(config, userConsent);
}
if (noTrack != null) {
done();
return false;
}
tapadRtdObj.alterBids(reqBidsConfigObj, config);
done()
return true;
Expand All @@ -55,9 +68,15 @@ export const tapadRtdObj = {
function storeDataEnvelopeResponse(response) {
const responseJson = safeJSONParse(response);
if (responseJson != null) {
storage.setDataInLocalStorage(TAPAD_RTD_DATA_KEY, JSON.stringify(responseJson.data), null);
storage.setDataInLocalStorage(TAPAD_RTD_STALE_KEY, responseJson.staleAt, null);
storage.setDataInLocalStorage(TAPAD_RTD_EXPIRATION_KEY, responseJson.expiresAt, null);
if (responseJson.status === 'no_track') {
storage.setDataInLocalStorage(TAPAD_RTD_NO_TRACK_KEY, 'no_track', null);
storage.removeDataFromLocalStorage(TAPAD_RTD_DATA_KEY, null);
} else {
storage.setDataInLocalStorage(TAPAD_RTD_DATA_KEY, JSON.stringify(responseJson.data), null);
storage.removeDataFromLocalStorage(TAPAD_RTD_NO_TRACK_KEY, null);
}
}
}
const queryString = tapadRtdObj.extractConsentQueryString(config, userConsent)
Expand Down Expand Up @@ -101,7 +120,7 @@ export const tapadRtdObj = {
* @return {boolean} false to remove sub module
*/
init(config, userConsent) {
return !isNaN(config.accountId);
return isStr(config.accountId);
}
}

Expand Down
17 changes: 9 additions & 8 deletions modules/tapadRtdProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ This module is configured as part of the `realTimeData.dataProviders`
```javascript
pbjs.setConfig({
realTimeData: {
auctionDelay: 300,
dataProviders: [{
name: 'tapad_rtd',
waitForIt: true,
params: {
accountId: 123,
accountId: 'ZylatYg',
bidders: ['sovrn', 'pubmatic'],
ids: { maid: ['424', '2982'], hem: 'my-hem' }
}
Expand All @@ -42,10 +43,10 @@ pbjs.setConfig({
```

### Parameters
| Name | Type | Description | Default |
|:-----------------|:----------------------------------------|:-----------------------------------------------------------------|:-------------------|
| name | String | Real time data module name | Always 'tapad_rtd' |
| waitForIt | Boolean | Should be `true` if there's an `auctionDelay` defined (optional) | `false` |
| params.accountId | String | Your account id issued by Tapad | |
| params.bidders | Array<string> | List of bidders for which you would like data to be set | |
| params.ids | Record<string, Array<string> or string> | Additional identifiers to send to Tapad RTID endpoint | |
| Name | Type | Description | Default |
|:-----------------|:----------------------------------------|:-----------------------------------------------------------------------------|:-------------------|
| name | String | Real time data module name | Always 'tapad_rtd' |
| waitForIt | Boolean | Set to true to maximize chance for bidder enrichment, used with auctionDelay | `false` |
| params.accountId | String | Your account id issued by Tapad | |
| params.bidders | Array<string> | List of bidders for which you would like data to be set | |
| params.ids | Record<string, Array<string> or string> | Additional identifiers to send to Tapad RTID endpoint | |
80 changes: 76 additions & 4 deletions test/spec/modules/tapadRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TAPAD_RTD_STALE_KEY,
SUBMODULE_NAME,
tapadRtdObj,
tapadRtdSubmodule
tapadRtdSubmodule, TAPAD_RTD_NO_TRACK_KEY
} from '../../../modules/tapadRtdProvider.js';
import { getStorageManager } from '../../../src/storageManager.js';
import { MODULE_TYPE_RTD } from '../../../src/activities/modules';
Expand All @@ -23,6 +23,7 @@ describe('Tapad realtime module', () => {
storage.removeDataFromLocalStorage(TAPAD_RTD_DATA_KEY, null)
storage.removeDataFromLocalStorage(TAPAD_RTD_EXPIRATION_KEY, null)
storage.removeDataFromLocalStorage(TAPAD_RTD_STALE_KEY, null)
storage.removeDataFromLocalStorage(TAPAD_RTD_NO_TRACK_KEY, null)
})
afterEach(() => {
sandbox.restore();
Expand All @@ -39,18 +40,18 @@ describe('Tapad realtime module', () => {
};
describe('init', () => {
it('succeeds when params have accountId', () => {
const initResult = tapadRtdSubmodule.init({ accountId: 123 })
const initResult = tapadRtdSubmodule.init({ accountId: 'ZylatYg' })
expect(initResult).to.be.true;
})

it('fails when params don\'t have accountId', () => {
const initResult = tapadRtdSubmodule.init({ accountId: 'invalid' })
const initResult = tapadRtdSubmodule.init({ })
expect(initResult).to.be.false;
})
})

describe('getBidRequestData', () => {
describe('when local storage has data, isn\'t stale and isn\'t expired', () => {
describe('when local storage has data, isn\'t no track, isn\'t stale and isn\'t expired', () => {
beforeEach(() => {
const now = timestamp()
storage.setDataInLocalStorage(TAPAD_RTD_DATA_KEY, JSON.stringify([
Expand Down Expand Up @@ -180,6 +181,77 @@ describe('Tapad realtime module', () => {
expect(alterBidsSpy.called).to.be.false;
})
})
describe('when local storage has no track and is expired', () => {
beforeEach(() => {
const now = timestamp()
storage.setDataInLocalStorage(TAPAD_RTD_NO_TRACK_KEY, 'no_track', null)

storage.setDataInLocalStorage(TAPAD_RTD_EXPIRATION_KEY, new Date(now - 50000).toISOString(), null)
storage.setDataInLocalStorage(TAPAD_RTD_STALE_KEY, new Date(now - 100000).toISOString(), null)
})
it('requests data envelope, and doesn\'t alter bids', () => {
const bidsConfig = {
ortb2Fragments: {
bidder: {}
}
}
const userConsent = {gdpr: {}, uspConsent: {}}
const moduleConfig = { accountId: 123, bidders: ['pubmatic', 'sovrn'] }
const dataEnvelopeSpy = sandbox.spy(tapadRtdObj, 'requestDataEnvelope')
const alterBidsSpy = sandbox.spy(tapadRtdObj, 'alterBids')
tapadRtdSubmodule.getBidRequestData(bidsConfig, sinon.stub, moduleConfig, userConsent)
sandbox.assert.calledWithExactly(dataEnvelopeSpy, moduleConfig, userConsent)
expect(alterBidsSpy.called).to.be.false;
})
})

describe('when local storage has no track and is stale', () => {
beforeEach(() => {
const now = timestamp()
storage.setDataInLocalStorage(TAPAD_RTD_NO_TRACK_KEY, 'no_track', null)

storage.setDataInLocalStorage(TAPAD_RTD_EXPIRATION_KEY, new Date(now + 100000).toISOString(), null)
storage.setDataInLocalStorage(TAPAD_RTD_STALE_KEY, new Date(now - 50000).toISOString(), null)
})
it('requests data envelope, and doesn\'t alter bids', () => {
const bidsConfig = {
ortb2Fragments: {
bidder: {}
}
}
const userConsent = {gdpr: {}, uspConsent: {}}
const moduleConfig = { accountId: 123, bidders: ['pubmatic', 'sovrn'] }
const dataEnvelopeSpy = sandbox.spy(tapadRtdObj, 'requestDataEnvelope')
const alterBidsSpy = sandbox.spy(tapadRtdObj, 'alterBids')
tapadRtdSubmodule.getBidRequestData(bidsConfig, sinon.stub, moduleConfig, userConsent)
sandbox.assert.calledWithExactly(dataEnvelopeSpy, moduleConfig, userConsent)
expect(alterBidsSpy.called).to.be.false;
})
})

describe('when local storage has no track and isn\'t expired or stale', () => {
beforeEach(() => {
const now = timestamp()
storage.setDataInLocalStorage(TAPAD_RTD_NO_TRACK_KEY, 'no_track', null)

storage.setDataInLocalStorage(TAPAD_RTD_EXPIRATION_KEY, new Date(now + 100000).toISOString(), null)
storage.setDataInLocalStorage(TAPAD_RTD_STALE_KEY, new Date(now + 50000).toISOString(), null)
})
it('doesn\'t alter bids and doesn\'t request data envelope', () => {
const bidsConfig = {
ortb2Fragments: {
bidder: {}
}
}
const userConsent = {gdpr: {}, uspConsent: {}}
const moduleConfig = { accountId: 123, bidders: ['pubmatic', 'sovrn'] }
const dataEnvelopeSpy = sandbox.spy(tapadRtdObj, 'requestDataEnvelope')
const alterBidsSpy = sandbox.spy(tapadRtdObj, 'alterBids')
tapadRtdSubmodule.getBidRequestData(bidsConfig, sinon.stub, moduleConfig, userConsent)
expect(alterBidsSpy.called).to.be.false;
expect(dataEnvelopeSpy.called).to.be.false;
})
})
})

describe('alterBids', () => {
Expand Down

0 comments on commit ef69bfc

Please sign in to comment.