Skip to content

Commit

Permalink
Staq Adapter: update with meta envelope (#4372)
Browse files Browse the repository at this point in the history
* initial dev

* fix staq adapter name

* fix hello world staq call

* get hello world working again

* add user agent collection

* fix some unite tests

* Add STAQ Analytics Adapter doc

* clean up hello world

* fix tests to play nice with browserstack

* fix around issues with browserstack and deep equals of objects

* dump variable env testing since we can't mod user agent stuff in browserstack

* Update STAQ adapter to stop using deprecated utils for referrer

* remove package-lock.json changes via master rebase

* improve call frequency for ref util

* change ajax content type

* adjust ajax request to not expect whitelisting

* remove superflous commented-out code

* update event package to use meta information in envelope rather than per event basis

* fix formatting

* more formatting fixes

* more formatting!
  • Loading branch information
mquirion authored and sumit116 committed Nov 15, 2019
1 parent ac2f8e5 commit 58f839c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 84 deletions.
103 changes: 51 additions & 52 deletions modules/staqAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import adapter from '../src/AnalyticsAdapter';
import CONSTANTS from '../src/constants.json';
import adapterManager from '../src/adapterManager';
import {getRefererInfo} from '../src/refererDetection';
import {parse} from '../src/url';
import { getRefererInfo } from '../src/refererDetection';
import { parse } from '../src/url';
import * as utils from '../src/utils';
import {ajax} from '../src/ajax';
import { ajax } from '../src/ajax';

const ANALYTICS_VERSION = '1.0.0';
const DEFAULT_QUEUE_TIMEOUT = 4000;
Expand Down Expand Up @@ -43,50 +43,49 @@ function buildRequestTemplate(connId) {
}
}

let analyticsAdapter = Object.assign(adapter({analyticsType: 'endpoint'}),
{
track({ eventType, args }) {
if (!analyticsAdapter.context) {
return;
}
let handler = null;
switch (eventType) {
case CONSTANTS.EVENTS.AUCTION_INIT:
if (analyticsAdapter.context.queue) {
analyticsAdapter.context.queue.init();
}
handler = trackAuctionInit;
break;
case CONSTANTS.EVENTS.BID_REQUESTED:
handler = trackBidRequest;
break;
case CONSTANTS.EVENTS.BID_RESPONSE:
handler = trackBidResponse;
break;
case CONSTANTS.EVENTS.BID_WON:
handler = trackBidWon;
break;
case CONSTANTS.EVENTS.BID_TIMEOUT:
handler = trackBidTimeout;
break;
case CONSTANTS.EVENTS.AUCTION_END:
handler = trackAuctionEnd;
break;
}
if (handler) {
let events = handler(args);
let analyticsAdapter = Object.assign(adapter({ analyticsType: 'endpoint' }), {
track({ eventType, args }) {
if (!analyticsAdapter.context) {
return;
}
let handler = null;
switch (eventType) {
case CONSTANTS.EVENTS.AUCTION_INIT:
if (analyticsAdapter.context.queue) {
analyticsAdapter.context.queue.push(events);
if (eventType === CONSTANTS.EVENTS.BID_WON) {
analyticsAdapter.context.queue.updateWithWins(events);
}
analyticsAdapter.context.queue.init();
}
if (eventType === CONSTANTS.EVENTS.AUCTION_END) {
sendAll();
handler = trackAuctionInit;
break;
case CONSTANTS.EVENTS.BID_REQUESTED:
handler = trackBidRequest;
break;
case CONSTANTS.EVENTS.BID_RESPONSE:
handler = trackBidResponse;
break;
case CONSTANTS.EVENTS.BID_WON:
handler = trackBidWon;
break;
case CONSTANTS.EVENTS.BID_TIMEOUT:
handler = trackBidTimeout;
break;
case CONSTANTS.EVENTS.AUCTION_END:
handler = trackAuctionEnd;
break;
}
if (handler) {
let events = handler(args);
if (analyticsAdapter.context.queue) {
analyticsAdapter.context.queue.push(events);
if (eventType === CONSTANTS.EVENTS.BID_WON) {
analyticsAdapter.context.queue.updateWithWins(events);
}
}
if (eventType === CONSTANTS.EVENTS.AUCTION_END) {
sendAll();
}
}
});
}
});

analyticsAdapter.context = {};

Expand Down Expand Up @@ -123,17 +122,17 @@ export default analyticsAdapter;
function sendAll() {
let events = analyticsAdapter.context.queue.popAll();
if (events.length !== 0) {
let req = events.map(event => {
return Object.assign({}, event, analyticsAdapter.context.requestTemplate)
});
let req = analyticsAdapter.context.requestTemplate;
req.auctionId = analyticsAdapter.context.auctionId;
req.events = events

analyticsAdapter.ajaxCall(JSON.stringify(req));
}
}

analyticsAdapter.ajaxCall = function ajaxCall(data) {
utils.logInfo('SENDING DATA: ' + data);
ajax(`//${analyticsAdapter.context.url}/prebid/${analyticsAdapter.context.connectionId}`, () => {
}, data, {contentType: 'text/plain'});
ajax(`//${analyticsAdapter.context.url}/prebid/${analyticsAdapter.context.connectionId}`, () => {}, data, { contentType: 'text/plain' });
};

function trackAuctionInit(args) {
Expand Down Expand Up @@ -166,9 +165,7 @@ function trackAuctionEnd(args) {
}

function trackBidTimeout(args) {
return args.map(arg =>
createHbEvent(arg.auctionId, arg.bidderCode, STAQ_EVENTS.TIMEOUT)
);
return args.map(arg => createHbEvent(arg.auctionId, arg.bidderCode, STAQ_EVENTS.TIMEOUT));
}

function createHbEvent(auctionId, adapter, event, adUnitCode = undefined, value = 0, time = 0, bidWon = undefined, eventArgs) {
Expand Down Expand Up @@ -206,7 +203,8 @@ function createHbEvent(auctionId, adapter, event, adUnitCode = undefined, value
}

const UTM_TAGS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',
'utm_c1', 'utm_c2', 'utm_c3', 'utm_c4', 'utm_c5'];
'utm_c1', 'utm_c2', 'utm_c3', 'utm_c4', 'utm_c5'
];
const STAQ_PREBID_KEY = 'staq_analytics';
const DIRECT = '(direct)';
const REFERRAL = '(referral)';
Expand Down Expand Up @@ -334,7 +332,8 @@ export function getUmtSource(pageUrl, referrer) {
function chooseActualUtm(prev, curr) {
if (ord(prev) < ord(curr)) {
return [true, curr];
} if (ord(prev) > ord(curr)) {
}
if (ord(prev) > ord(curr)) {
return [false, prev];
} else {
if (prev.campaign === REFERRAL && prev.content !== curr.content) {
Expand Down
65 changes: 33 additions & 32 deletions test/spec/modules/staqAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import analyticsAdapter, {ExpiringQueue, getUmtSource, storage} from 'modules/staqAnalyticsAdapter';
import {expect} from 'chai';
import analyticsAdapter, { ExpiringQueue, getUmtSource, storage } from 'modules/staqAnalyticsAdapter';
import { expect } from 'chai';
import adapterManager from 'src/adapterManager';
import CONSTANTS from 'src/constants.json';

Expand Down Expand Up @@ -32,73 +32,73 @@ const CAMPAIGN = {
c5: '5'

};
describe('', function () {
describe('', function() {
let sandbox;

before(function () {
before(function() {
sandbox = sinon.sandbox.create();
});

after(function () {
after(function() {
sandbox.restore();
analyticsAdapter.disableAnalytics();
});

describe('UTM source parser', function () {
describe('UTM source parser', function() {
let stubSetItem;
let stubGetItem;

before(function () {
before(function() {
stubSetItem = sandbox.stub(storage, 'setItem');
stubGetItem = sandbox.stub(storage, 'getItem');
});

afterEach(function () {
afterEach(function() {
sandbox.reset();
});

it('should parse first direct visit as (direct)', function () {
it('should parse first direct visit as (direct)', function() {
stubGetItem.withArgs('adk_dpt_analytics').returns(undefined);
stubSetItem.returns(undefined);
let source = getUmtSource('http://example.com');
expect(source).to.be.eql(DIRECT);
});

it('should parse visit from google as organic', function () {
it('should parse visit from google as organic', function() {
stubGetItem.withArgs('adk_dpt_analytics').returns(undefined);
stubSetItem.returns(undefined);
let source = getUmtSource('http://example.com', 'https://www.google.com/search?q=pikachu');
expect(source).to.be.eql(GOOGLE_ORGANIC);
});

it('should parse referral visit', function () {
it('should parse referral visit', function() {
stubGetItem.withArgs('adk_dpt_analytics').returns(undefined);
stubSetItem.returns(undefined);
let source = getUmtSource('http://example.com', 'http://lander.com/lander.html');
expect(source).to.be.eql(REFERRER);
});

it('should parse referral visit from same domain as direct', function () {
it('should parse referral visit from same domain as direct', function() {
stubGetItem.withArgs('adk_dpt_analytics').returns(undefined);
stubSetItem.returns(undefined);
let source = getUmtSource('http://lander.com/news.html', 'http://lander.com/lander.html');
expect(source).to.be.eql(DIRECT);
});

it('should parse campaign visit', function () {
it('should parse campaign visit', function() {
stubGetItem.withArgs('adk_dpt_analytics').returns(undefined);
stubSetItem.returns(undefined);
let source = getUmtSource('http://lander.com/index.html?utm_campaign=new_campaign&utm_source=adkernel&utm_medium=email&utm_c1=1&utm_c2=2&utm_c3=3&utm_c4=4&utm_c5=5');
expect(source).to.be.eql(CAMPAIGN);
});
});

describe('ExpiringQueue', function () {
describe('ExpiringQueue', function() {
let timer;
before(function () {
before(function() {
timer = sandbox.useFakeTimers(0);
});
after(function () {
after(function() {
timer.restore();
});

Expand Down Expand Up @@ -135,7 +135,9 @@ describe('', function () {
params: {},
adUnitCode: 'container-1',
transactionId: 'de90df62-7fd0-4fbc-8787-92d133a7dc06',
sizes: [[300, 250]],
sizes: [
[300, 250]
],
bidId: '208750227436c1',
bidderRequestId: '1a6fc81528d0f6',
auctionId: '5018eb39-f900-4370-b71e-3bb5b48d324f'
Expand Down Expand Up @@ -176,26 +178,26 @@ describe('', function () {
auctionId: '66529d4c-8998-47c2-ab3e-5b953490b98f'
}];

describe('Analytics adapter', function () {
describe('Analytics adapter', function() {
let ajaxStub;
let timer;

before(function () {
before(function() {
ajaxStub = sandbox.stub(analyticsAdapter, 'ajaxCall');
timer = sandbox.useFakeTimers(0);
});

beforeEach(function () {
beforeEach(function() {
sandbox.stub(events, 'getEvents').callsFake(() => {
return []
});
});

afterEach(function () {
afterEach(function() {
events.getEvents.restore();
});

it('should be configurable', function () {
it('should be configurable', function() {
adapterManager.registerAnalyticsAdapter({
code: 'staq',
adapter: analyticsAdapter
Expand All @@ -213,14 +215,14 @@ describe('', function () {
expect(analyticsAdapter.context).to.have.property('connectionId', 777);
});

it('should handle auction init event', function () {
events.emit(CONSTANTS.EVENTS.AUCTION_INIT, {config: {}, timeout: 3000});
it('should handle auction init event', function() {
events.emit(CONSTANTS.EVENTS.AUCTION_INIT, { config: {}, timeout: 3000 });
const ev = analyticsAdapter.context.queue.peekAll();
expect(ev).to.have.length(1);
expect(ev[0]).to.be.eql({event: 'auctionInit', auctionId: undefined});
expect(ev[0]).to.be.eql({ event: 'auctionInit', auctionId: undefined });
});

it('should handle bid request event', function () {
it('should handle bid request event', function() {
events.emit(CONSTANTS.EVENTS.BID_REQUESTED, REQUEST);
const ev = analyticsAdapter.context.queue.peekAll();
expect(ev).to.have.length(2);
Expand All @@ -233,7 +235,7 @@ describe('', function () {
});
});

it('should handle bid response event', function () {
it('should handle bid response event', function() {
events.emit(CONSTANTS.EVENTS.BID_RESPONSE, RESPONSE);
const ev = analyticsAdapter.context.queue.peekAll();
expect(ev).to.have.length(3);
Expand Down Expand Up @@ -265,7 +267,7 @@ describe('', function () {
})
});

it('should handle winning bid', function () {
it('should handle winning bid', function() {
events.emit(CONSTANTS.EVENTS.BID_WON, RESPONSE);
const ev = analyticsAdapter.context.queue.peekAll();
expect(ev).to.have.length(6);
Expand All @@ -283,17 +285,16 @@ describe('', function () {
});
});

it('should handle auction end event', function () {
it('should handle auction end event', function() {
timer.tick(447);
events.emit(CONSTANTS.EVENTS.AUCTION_END, RESPONSE);
let ev = analyticsAdapter.context.queue.peekAll();
expect(ev).to.have.length(0);
expect(ajaxStub.calledOnce).to.be.equal(true);
let firstCallArgs0 = ajaxStub.firstCall.args[0];
ev = JSON.parse(firstCallArgs0);
// console.log('AUCTION END EVENT SHAPE ' + JSON.stringify(ev));
const ev6 = ev[6];
expect(ev6.connId).to.be.eql(777);
const ev6 = ev['events'][6];
expect(ev['connId']).to.be.eql(777);
expect(ev6.auctionId).to.be.eql('5018eb39-f900-4370-b71e-3bb5b48d324f');
expect(ev6.event).to.be.eql('auctionEnd');
});
Expand Down

0 comments on commit 58f839c

Please sign in to comment.