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

Campaign support from utm and configuration properties #23

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
85 changes: 44 additions & 41 deletions modules/openxAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import adapterManager from '../src/adapterManager.js';

//* ******* V2 Code
import { ajax } from '../src/ajax.js';
import {getWindowLocation, parseQS} from '../src/utils';

// temp dependency on zlib to minimize payload
const zlib = require('zlib'); // eslint-disable-line
Expand Down Expand Up @@ -38,11 +39,11 @@ const SLOT_LOADED = 'slotOnload';
* @property {number} sampling
* @property {boolean} enableV2
* @property {boolean} testPipeline
* @property {Object} utmTagData
* @property {Object} campaign
* @property {string} adIdKey
* @property {number} payloadWaitTime
* @property {number} payloadWaitTimePadding
* @property {Array<string>}adUnits
* @property {Array<string>} adUnits
*/

/**
Expand All @@ -56,10 +57,10 @@ const DEFAULT_ANALYTICS_CONFIG = {
enableV2: false,
testPipeline: false,
adIdKey: 'hb_adid',
utmTagData: {},
campaign: {},
adUnits: [],
payloadWaitTime: AUCTION_END_WAIT_TIME,
payloadWaitTimePadding: 100
payloadWaitTimePadding: 2000
};

let googletag = window.googletag || {};
Expand All @@ -75,12 +76,20 @@ let loadedAdSlots = {};

let localStoragePrefix = 'openx_analytics_';
let utmTags = [
'utm_campaign',
'utm_source',
'utm_medium',
'utm_campaign',
'utm_term',
'utm_content'
];

const UTM_TO_CAMPAIGN_PROPERTIES = {
'utm_campaign': 'name',
'utm_source': 'source',
'utm_medium': 'medium',
'utm_term': 'term',
'utm_content': 'content'
};
let utmTimeoutKey = 'utm_timeout';
let utmTimeout = 60 * 60 * 1000;
let sessionTimeout = 60 * 60 * 1000;
Expand Down Expand Up @@ -384,7 +393,10 @@ openxAdapter.enableAnalytics = function(adapterConfig = {options: {}}) {

if (isValidConfig(adapterConfig)) {
analyticsConfig = {...DEFAULT_ANALYTICS_CONFIG, ...adapterConfig.options};
analyticsConfig.utmTagData = this.buildUtmTagData();

// campaign properties defined by config will override utm query parameters
analyticsConfig.campaign = {...buildCampaignFromUtmCodes(), ...analyticsConfig.campaign};

utils.logInfo('OpenX Analytics enabled with config', analyticsConfig);

if (analyticsConfig.testPipeline) {
Expand Down Expand Up @@ -433,6 +445,7 @@ openxAdapter.enableAnalytics = function(adapterConfig = {options: {}}) {
['testPipeline', 'boolean', false],
['adIdKey', 'string', false],
['payloadWaitTime', 'number', false],
['payloadWaitTimePadding', 'number', false],
];

let failedValidation = fieldValidations.find(([property, type, required]) => {
Expand All @@ -441,7 +454,6 @@ openxAdapter.enableAnalytics = function(adapterConfig = {options: {}}) {
return (required && !analyticsOptions.hasOwnProperty(property)) ||
(analyticsOptions.hasOwnProperty(property) && typeof analyticsOptions[property] !== type);
});

if (failedValidation) {
let [property, type, required] = failedValidation;

Expand All @@ -456,36 +468,19 @@ openxAdapter.enableAnalytics = function(adapterConfig = {options: {}}) {
}
};

openxAdapter.buildUtmTagData = function() {
let utmTagData = {};
let utmTagsDetected = false;
utmTags.forEach(function(utmTagKey) {
let utmTagValue = getParameterByName(utmTagKey);
if (utmTagValue !== '') {
utmTagsDetected = true;
}
utmTagData[utmTagKey] = utmTagValue;
});
utmTags.forEach(function(utmTagKey) {
if (utmTagsDetected) {
localStorage.setItem(
buildUtmLocalStorageKey(utmTagKey),
utmTagData[utmTagKey]
);
updateUtmTimeout();
} else {
if (!isUtmTimeoutExpired()) {
utmTagData[utmTagKey] = localStorage.getItem(
buildUtmLocalStorageKey(utmTagKey)
)
? localStorage.getItem(buildUtmLocalStorageKey(utmTagKey))
: '';
updateUtmTimeout();
}
function buildCampaignFromUtmCodes() {
let campaign = {};
let queryParams = utils.parseQS(utils.getWindowLocation() && utils.getWindowLocation().search);

utmTags.forEach(function(utmKey) {
let utmValue = queryParams[utmKey];
if(utmValue){
let key = UTM_TO_CAMPAIGN_PROPERTIES[utmKey];
campaign[key] = utmValue;
}
});
return utmTagData;
};
return campaign;
}

function buildPayload(
data,
Expand All @@ -494,7 +489,8 @@ function buildPayload(
publisherAccountId,
auctionId,
testCode,
sourceUrl
sourceUrl,
campaign
) {
return {
adapterVersion: ADAPTER_VERSION,
Expand All @@ -505,7 +501,8 @@ function buildPayload(
publisherAccountId: publisherAccountId,
auctionId: auctionId,
testCode: testCode,
sourceUrl: sourceUrl
sourceUrl: sourceUrl,
campaign
};
}

Expand Down Expand Up @@ -637,7 +634,8 @@ function send(eventType, eventStack, auctionId) {
publisherAccountId,
auctionId,
testCode,
sourceUrl
sourceUrl,
analyticsConfig.campaign
);
apiCall(urlGenerated, MAX_RETRIES, payload);
} else {
Expand Down Expand Up @@ -1014,10 +1012,13 @@ function onSlotLoadedV2({ slot }) {
adUnit.renderTime = renderTime;
}

if (auction.adunitCodesRenderedCount === auction.adUnitCodesCount) {
auction.state = AUCTION_STATES.COMPLETED;
}

// prepare to send regardless if auction is complete or not as a failsafe in case not all events are tracked
// add additional padding when not all slots are rendered
delayedSend(auction);
auction.state = AUCTION_STATES.COMPLETED;
}

function delayedSend(auction) {
Expand Down Expand Up @@ -1060,10 +1061,12 @@ function getAuctionByGoogleTagSLot(slot) {

function buildAuctionPayload(auction) {
let {startTime, endTime, state, timeout, auctionOrder, adUnitCodeToBidderRequestMap} = auction;
let {publisherPlatformId, publisherAccountId, campaign} = analyticsConfig;

return {
publisherPlatformId: analyticsConfig.publisherPlatformId,
publisherAccountId: analyticsConfig.publisherAccountId,
publisherPlatformId,
publisherAccountId,
campaign,
state,
startTime,
endTime,
Expand Down
Loading