Skip to content

Commit

Permalink
AdomikAnalyticsAdapter: add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
liabas-b committed Mar 3, 2022
1 parent 2293a34 commit 8e05d27
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 67 deletions.
61 changes: 27 additions & 34 deletions modules/adomikAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let adomikAdapter = Object.assign(adapter({}),
break;

case bidResponse:
adomikAdapter.saveBidResponse(args, 'bidResponse');
adomikAdapter.saveBidResponse(args);
break;

case bidWon:
Expand Down Expand Up @@ -66,15 +66,14 @@ adomikAdapter.initializeBucketEvents = function() {
adomikAdapter.bucketEvents = [];
}

adomikAdapter.saveBidResponse = function(args, from) {
adomikAdapter.saveBidResponse = function(args) {
let responseSaved = adomikAdapter.bucketEvents.find((bucketEvent) =>
bucketEvent.type == 'response' && bucketEvent.event.id == args.id
);
if (responseSaved)
return true;
if (responseSaved) { return true; }
adomikAdapter.bucketEvents.push({
type: 'response',
event: { ...adomikAdapter.buildBidResponse(args), ...{ from: from } }
event: adomikAdapter.buildBidResponse(args)
});
}

Expand Down Expand Up @@ -124,10 +123,8 @@ adomikAdapter.sendTypedEvent = function() {
const stringBulkEvents = JSON.stringify(bulkEvents)
logInfo('Events sent to adomik prebid analytic ' + stringBulkEvents);

// Encode object in base64
const encodedBuf = window.btoa(stringBulkEvents);

// Create final url and split it (+endpoint length)
const encodedUri = encodeURIComponent(encodedBuf);
const maxLength = adomikAdapter.maxPartLength();
const splittedUrl = encodedUri.match(new RegExp(`.{1,${maxLength}}`, 'g'));
Expand All @@ -140,24 +137,18 @@ adomikAdapter.sendTypedEvent = function() {
};

adomikAdapter.sendWonEvent = function (wonEvent) {
<<<<<<< HEAD
let keyValues = { testId: adomikAdapter.currentContext.testId, testValue: adomikAdapter.currentContext.testValue }
let samplingInfo = { sampling: adomikAdapter.currentContext.sampling }
wonEvent = { ...wonEvent, ...keyValues, ...samplingInfo }

=======
let [testId, testValue] = adomikAdapter.getKeyValues();
keyValues = { testId: testId, testValue: testValue }
wonEvent = {...wonEvent, ...keyValues}
>>>>>>> adomikAnayticsAdapter/receive KV from sessionStorage
const stringWonEvent = JSON.stringify(wonEvent)
let keyValues = { testId: testId, testValue: testValue };
let samplingInfo = { sampling: adomikAdapter.currentContext.sampling };
wonEvent = { ...adomikAdapter.buildBidResponse(wonEvent), ...keyValues, ...samplingInfo };

const stringWonEvent = JSON.stringify(wonEvent);
logInfo('Won event sent to adomik prebid analytic ' + stringWonEvent);

// Encode object in base64
const encodedBuf = window.btoa(stringWonEvent);
const encodedUri = encodeURIComponent(encodedBuf);
const img = new Image(1, 1);
img.src = `https://${adomikAdapter.currentContext.url}/?q=${encodedUri}&id=${adomikAdapter.currentContext.id}&won=true`
img.src = `https://${adomikAdapter.currentContext.url}/?q=${encodedUri}&id=${adomikAdapter.currentContext.id}&won=true`;
}

adomikAdapter.buildBidResponse = function (bid) {
Expand Down Expand Up @@ -219,12 +210,23 @@ adomikAdapter.buildTypedEvents = function () {
return groupedTypedEvents;
}

adomikAdapter.getKeyValues = function () {
let preventTest = sessionStorage.getItem(window.location.hostname + '_NoAdomikTest')
let inScope = sessionStorage.getItem(window.location.hostname + '_AdomikTestInScope')
let keyValues = JSON.parse(sessionStorage.getItem(window.location.hostname + '_AdomikTest'))
let testId;
let testValue;
if (typeof (keyValues) === 'object' && keyValues != undefined && !preventTest && inScope) {
testId = keyValues.testId
testValue = keyValues.testOptionLabel
}
return [testId, testValue]
}

adomikAdapter.enable = function(options) {
adomikAdapter.currentContext = {
uid: options.id,
url: options.url,
testId: options.testId,
testValue: options.testValue,
id: '',
timeouted: false,
sampling: options.sampling
Expand All @@ -235,24 +237,15 @@ adomikAdapter.enable = function(options) {

adomikAdapter.checkOptions = function(options) {
if (typeof options !== 'undefined') {
if (options.id && options.url)
adomikAdapter.enable(options);
else
logInfo('Adomik Analytics disabled because id and/or url is missing from config', options);
}
else
logInfo('Adomik Analytics disabled because config is missing');
if (options.id && options.url) { adomikAdapter.enable(options); } else { logInfo('Adomik Analytics disabled because id and/or url is missing from config', options); }
} else { logInfo('Adomik Analytics disabled because config is missing'); }
};

adomikAdapter.checkSampling = function(options) {
_sampled = typeof options === 'undefined' ||
typeof options.sampling === 'undefined' ||
Math.random() < parseFloat(options.sampling);

if (_sampled)
adomikAdapter.checkOptions(options)
else
logInfo('Adomik Analytics ignored for sampling', options.sampling);
(options.sampling > 0 && Math.random() < parseFloat(options.sampling));
if (_sampled) { adomikAdapter.checkOptions(options) } else { logInfo('Adomik Analytics ignored for sampling', options.sampling); }
};

adomikAdapter.adapterEnableAnalytics = adomikAdapter.enableAnalytics;
Expand Down
166 changes: 133 additions & 33 deletions test/spec/modules/adomikAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,34 @@ describe('Adomik Prebid Analytic', function () {
let sendWonEventStub;
let clock;

before(function () {
beforeEach(function () {
clock = sinon.useFakeTimers();
sinon.spy(adomikAnalytics, 'track');
sendEventStub = sinon.stub(adomikAnalytics, 'sendTypedEvent');
sendWonEventStub = sinon.stub(adomikAnalytics, 'sendWonEvent');
sinon.stub(events, 'getEvents').returns([]);
adomikAnalytics.currentContext = undefined;

adapterManager.registerAnalyticsAdapter({
code: 'adomik',
adapter: adomikAnalytics
});
});
after(function () {

afterEach(function () {
adomikAnalytics.disableAnalytics();
clock.restore();
adomikAnalytics.track.restore();
sendEventStub.restore();
sendWonEventStub.restore();
events.getEvents.restore();
});

describe('enableAnalytics', function () {
beforeEach(function () {
sinon.spy(adomikAnalytics, 'track');
sendEventStub = sinon.stub(adomikAnalytics, 'sendTypedEvent');
sendWonEventStub = sinon.stub(adomikAnalytics, 'sendWonEvent');
sinon.stub(events, 'getEvents').returns([]);
});

afterEach(function () {
adomikAnalytics.track.restore();
sendEventStub.restore();
sendWonEventStub.restore();
events.getEvents.restore();
});

after(function () {
adomikAnalytics.disableAnalytics();
});

describe('adomikAnalytics.enableAnalytics', function () {
it('should catch all events', function (done) {
adapterManager.registerAnalyticsAdapter({
code: 'adomik',
adapter: adomikAnalytics
});

const initOptions = {
id: '123456',
url: 'testurl',
testId: '12345',
testValue: '1000'
url: 'testurl'
};

const bid = {
Expand Down Expand Up @@ -74,8 +65,6 @@ describe('Adomik Prebid Analytic', function () {
uid: '123456',
url: 'testurl',
sampling: undefined,
testId: '12345',
testValue: '1000',
id: '',
timeouted: false
});
Expand All @@ -87,8 +76,6 @@ describe('Adomik Prebid Analytic', function () {
uid: '123456',
url: 'testurl',
sampling: undefined,
testId: '12345',
testValue: '1000',
id: 'test-test-test',
timeouted: false
});
Expand Down Expand Up @@ -149,5 +136,118 @@ describe('Adomik Prebid Analytic', function () {

sinon.assert.callCount(adomikAnalytics.track, 6);
});

describe('when sampling is undefined', function () {
beforeEach(function() {
adapterManager.enableAnalytics({
provider: 'adomik',
options: { id: '123456', url: 'testurl' }
});
});

it('is enabled', function () {
expect(adomikAnalytics.currentContext).is.not.null;
});
});

describe('when sampling is 0', function () {
beforeEach(function() {
adapterManager.enableAnalytics({
provider: 'adomik',
options: { id: '123456', url: 'testurl', sampling: 0 }
});
});

it('is disabled', function () {
expect(adomikAnalytics.currentContext).to.equal(undefined);
});
});

describe('when sampling is 1', function () {
beforeEach(function() {
adapterManager.enableAnalytics({
provider: 'adomik',
options: { id: '123456', url: 'testurl', sampling: 1 }
});
});

it('is enabled', function () {
expect(adomikAnalytics.currentContext).is.not.null;
});
});

describe('when options is not defined', function () {
beforeEach(function() {
adapterManager.enableAnalytics({ provider: 'adomik' });
});

it('is disabled', function () {
expect(adomikAnalytics.currentContext).to.equal(undefined);
});
});

describe('when id is not defined in options', function () {
beforeEach(function() {
adapterManager.enableAnalytics({ provider: 'adomik', url: 'xxx' });
});

it('is disabled', function () {
expect(adomikAnalytics.currentContext).to.equal(undefined);
});
});

describe('when url is not defined in options', function () {
beforeEach(function() {
adapterManager.enableAnalytics({ provider: 'adomik', id: 'xxx' });
});

it('is disabled', function () {
expect(adomikAnalytics.currentContext).to.equal(undefined);
});
});
});

describe('adomikAnalytics.getKeyValues', function () {
it('returns [undefined, undefined]', function () {
let [testId, testValue] = adomikAnalytics.getKeyValues()
expect(testId).to.equal(undefined);
expect(testValue).to.equal(undefined);
});

describe('when test is in scope', function () {
beforeEach(function () {
sessionStorage.setItem(window.location.hostname + '_AdomikTestInScope', true);
});

it('returns [undefined, undefined]', function () {
let [testId, testValue] = adomikAnalytics.getKeyValues()
expect(testId).to.equal(undefined);
expect(testValue).to.equal(undefined);
});

describe('when key values are defined', function () {
beforeEach(function () {
sessionStorage.setItem(window.location.hostname + '_AdomikTest', '{"testId":"12345","testOptionLabel":"1000"}');
});

it('returns key values', function () {
let [testId, testValue] = adomikAnalytics.getKeyValues()
expect(testId).to.equal('12345');
expect(testValue).to.equal('1000');
});

describe('when preventTest is on', function () {
beforeEach(function () {
sessionStorage.setItem(window.location.hostname + '_NoAdomikTest', true);
});

it('returns [undefined, undefined]', function () {
let [testId, testValue] = adomikAnalytics.getKeyValues()
expect(testId).to.equal(undefined);
expect(testValue).to.equal(undefined);
});
});
});
});
});
});

0 comments on commit 8e05d27

Please sign in to comment.