Skip to content

Commit

Permalink
add publisher ids functionality to Tapad RTD submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
moeroach94 committed Aug 16, 2023
1 parent 9cf41dd commit 71b8e3b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
48 changes: 31 additions & 17 deletions modules/tapadRtdProvider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { submodule } from '../src/hook.js';
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
import { mergeDeep, safeJSONParse, timestamp } from '../src/utils.js';
import { isArray, isPlainObject, mergeDeep, safeJSONParse, timestamp } from '../src/utils.js';
import { ajax } from '../src/ajax.js';

export const SUBMODULE_NAME = 'tapad_rtd';
Expand Down Expand Up @@ -61,28 +61,42 @@ export const tapadRtdObj = {
storage.setDataInLocalStorage(TAPAD_RTD_EXPIRATION_KEY, responseJson.expiresAt, null);
}
}
const queryString = tapadRtdObj.extractConsentQueryString(userConsent)
const queryString = tapadRtdObj.extractConsentQueryString(config, userConsent)
const fullUrl = queryString == null ? `${TAPAD_RTD_URL}/acc/${config.accountId}/ids` : `${TAPAD_RTD_URL}/acc/${config.accountId}/ids${queryString}`
ajax(fullUrl, storeDataEnvelopeResponse, null, { withCredentials: true, contentType: 'application/json' })
},
extractConsentQueryString(userConsent) {
extractConsentQueryString(config, userConsent) {
const queryObj = {};
if (userConsent == null) {
return undefined;
}
if (userConsent.gdpr != null) {
const { gdprApplies, consentString } = userConsent.gdpr;
mergeDeep(queryObj, {gdpr: gdprApplies, gdpr_consent: consentString})
}
if (userConsent.uspConsent != null) {
mergeDeep(queryObj, {us_privacy: userConsent.uspConsent})

if (userConsent != null) {
if (userConsent.gdpr != null) {
const { gdprApplies, consentString } = userConsent.gdpr;
mergeDeep(queryObj, {gdpr: gdprApplies, gdpr_consent: consentString})
}
if (userConsent.uspConsent != null) {
mergeDeep(queryObj, {us_privacy: userConsent.uspConsent})
}
}
if (Object.keys(queryObj).length > 0) {
return Object.entries(queryObj).reduce((queryString, [key, val], i) => {
return `${queryString}${i === 0 ? '' : '&'}${key}=${val}`
}, '?')
const consentQueryString = Object.entries(queryObj).map(([key, val]) => `${key}=${val}`).join('&');

let idsString = '';
if (config.ids != null && isPlainObject(config.ids)) {
idsString = Object.entries(config.ids).map(([idType, val]) => {
if (isArray(val)) {
return val.map((singleVal) => `id.${idType}=${singleVal}`).join('&')
} else {
return `id.${idType}=${val}`
}
}).join('&')
}
return undefined;

return consentQueryString !== '' && idsString !== ''
? `?${consentQueryString}&${idsString}`
: consentQueryString !== ''
? `?${consentQueryString}`
: idsString !== ''
? `?${idsString}`
: undefined;
},
/**
* @function
Expand Down
17 changes: 13 additions & 4 deletions test/spec/modules/tapadRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,23 @@ describe('Tapad realtime module', () => {
describe('extractConsentQueryString', () => {
describe('when userConsent is empty', () => {
it('returns undefined', () => {
expect(tapadRtdObj.extractConsentQueryString()).to.be.undefined
expect(tapadRtdObj.extractConsentQueryString({})).to.be.undefined
})
})

describe('when userConsent exists', () => {
expect(
tapadRtdObj.extractConsentQueryString({ gdpr: { gdprApplies: 1, consentString: 'this-is-something' }, uspConsent: '1YYY' })
).to.equal('?gdpr=1&gdpr_consent=this-is-something&us_privacy=1YYY')
it('builds query string', () => {
expect(
tapadRtdObj.extractConsentQueryString({}, { gdpr: { gdprApplies: 1, consentString: 'this-is-something' }, uspConsent: '1YYY' })
).to.equal('?gdpr=1&gdpr_consent=this-is-something&us_privacy=1YYY')
})
})

describe('when config.ids exists', () => {
it('builds query string', () => {
expect(tapadRtdObj.extractConsentQueryString({ ids: { maid: ['424', '2982'], hem: 'my-hem' } }, { gdpr: { gdprApplies: 1, consentString: 'this-is-something' }, uspConsent: '1YYY' }))
.to.equal('?gdpr=1&gdpr_consent=this-is-something&us_privacy=1YYY&id.maid=424&id.maid=2982&id.hem=my-hem')
})
})
})
})

0 comments on commit 71b8e3b

Please sign in to comment.