Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staq Adapter: update with meta envelope #4372

Merged
merged 26 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2bad754
initial dev
mquirion Apr 9, 2019
ee55125
fix staq adapter name
mquirion Apr 9, 2019
05ea85f
fix hello world staq call
mquirion Apr 9, 2019
d07cc87
get hello world working again
mquirion Apr 9, 2019
d394ee4
add user agent collection
mquirion Apr 17, 2019
a68a198
fix some unite tests
mquirion Apr 19, 2019
f35c925
Add STAQ Analytics Adapter doc
mquirion Apr 22, 2019
1abff32
clean up hello world
mquirion Apr 22, 2019
7bf3c70
Merge branch 'master' into new_staq_adapter
mquirion Apr 22, 2019
a4e1f62
fix tests to play nice with browserstack
mquirion Apr 22, 2019
4da31a1
Merge branch 'new_staq_adapter' of https://github.com/staqapp/Prebid.…
mquirion Apr 22, 2019
b5b032f
fix around issues with browserstack and deep equals of objects
mquirion Apr 22, 2019
5ec6ff2
dump variable env testing since we can't mod user agent stuff in brow…
mquirion Apr 22, 2019
f479411
Update STAQ adapter to stop using deprecated utils for referrer
mquirion May 2, 2019
8eaa16f
remove package-lock.json changes via master rebase
mquirion May 2, 2019
53b52b0
improve call frequency for ref util
mquirion May 2, 2019
ec4a1a1
change ajax content type
mquirion May 14, 2019
0eb0166
adjust ajax request to not expect whitelisting
mquirion May 15, 2019
ef83a4f
remove superflous commented-out code
May 23, 2019
209cf14
update event package to use meta information in envelope rather than …
Oct 23, 2019
b29747b
merging in latest Prebid.js updates
Oct 23, 2019
40a4246
fix formatting
Oct 24, 2019
b8ff144
more formatting fixes
Oct 24, 2019
8b4c5c7
more formatting!
Oct 24, 2019
598c2d5
Merge branch 'master' of https://github.com/prebid/Prebid.js
Oct 25, 2019
11ba456
Merge branch 'master' into staq_update_with_envelope
Oct 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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