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

Updates to PubWise Analytics - Enable config, UTM & Debug/Logging Upd… #1409

Merged
merged 13 commits into from
Aug 2, 2017
Merged
124 changes: 99 additions & 25 deletions modules/pubwiseAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,127 @@
import {ajax} from 'src/ajax';
import adapter from 'src/AnalyticsAdapter';
import adaptermanager from 'src/adaptermanager';
import CONSTANTS from 'src/constants.json';
const utils = require('src/utils');

/****
* PubWise.io Analytics
* Contact: [email protected]
* Developer: Stephen Johnston
*
* For testing:
*
pbjs.enableAnalytics({
provider: 'pubwise',
options: {
site: 'test-test-test-test',
endpoint: 'https://api.pubwise.io/api/v4/event/add/',
}
});
*/

const analyticsType = 'endpoint';
let target_site = 'unknown';
let target_url = 'https://staging.api.pubwise.io';
let pw_version = '2.1.3';
const analyticsName = 'PubWise Analytics: ';
let defaultUrl = 'https://api.pubwise.io/api/v4/event/default/';
let pubwiseVersion = '2.2';
let configOptions = {site: '', endpoint: 'https://api.pubwise.io/api/v4/event/default/', debug: ''};
let pwAnalyticsEnabled = false;
let utmKeys = {utm_source: '', utm_medium: '', utm_campaign: '', utm_term: '', utm_content: ''};

function markEnabled() {
utils.logInfo(`${analyticsName}Enabled`, configOptions);
pwAnalyticsEnabled = true;
}

function enrichWithMetrics(dataBag) {
try {
dataBag['pw_version'] = pubwiseVersion;
dataBag['pbjs_version'] = $$PREBID_GLOBAL$$.version;
dataBag['debug'] = configOptions.debug;
} catch (e) {
dataBag['error_metric'] = 1;
}

return dataBag;
}

function enrichWithUTM(dataBag) {
let newUtm = false;
try {
for (var prop in utmKeys) {
let urlValue = utils.getParameterByName(prop);
utmKeys[prop] = urlValue;
if (utmKeys[prop] != '') {
newUtm = true;
dataBag[prop] = utmKeys[prop];
}
}

if (newUtm === false) {
for (var prop in utmKeys) {
let itemValue = localStorage.getItem(`pw-${prop}`);
if (itemValue.length !== 0) {
dataBag[prop] = itemValue;
}
}
} else {
for (var prop in utmKeys) {
localStorage.setItem(`pw-${prop}`, utmKeys[prop]);
}
}
} catch (e) {
utils.logInfo(`${analyticsName}Error`, e);
dataBag['error_utm'] = 1;
}
return dataBag;
}

function sendEvent(eventType, data) {
utils.logInfo(`${analyticsName}Event ${eventType} ${pwAnalyticsEnabled}`, data);

// put the typical items in the data bag
let dataBag = {
eventType: eventType,
args: data,
target_site: configOptions.site,
debug: configOptions.debug ? 1 : 0,
};

// for certain events, track additional info
if (eventType == CONSTANTS.EVENTS.AUCTION_INIT) {
dataBag = enrichWithMetrics(dataBag);
dataBag = enrichWithUTM(dataBag);
}

ajax(configOptions.endpoint, (result) => utils.logInfo(`${analyticsName}Result`, result), JSON.stringify(dataBag));
}

let pubwiseAnalytics = Object.assign(adapter(
{
target_url,
defaultUrl,
analyticsType
}
),
}),
{
// Override AnalyticsAdapter functions by supplying custom methods
track({eventType, args}) {
/*
The args object is not always available, in addition neither is the config object
it is available on the first call and we can setup our config. Potential additional
PR for later, but this solves this for now.
*/
if (args !== undefined && args.config !== undefined && args.config.site !== undefined && args.config.endpoint !== undefined) {
target_site = args.config.site;
target_url = args.config.endpoint;
}
utils.logInfo('Sending PubWise Analytics Event ' + eventType, args);
ajax(target_url,
(result) => utils.logInfo('PubWise Analytics Result', result), JSON.stringify({
eventType,
args,
target_site,
pw_version
})
);
sendEvent(eventType, args);
}
});

pubwiseAnalytics.adapterEnableAnalytics = pubwiseAnalytics.enableAnalytics;

pubwiseAnalytics.enableAnalytics = function (config) {
if (config.options.debug === undefined) {
config.options.debug = utils.debugTurnedOn();
}
configOptions = config.options;
markEnabled();
pubwiseAnalytics.adapterEnableAnalytics(config);
};

adaptermanager.registerAnalyticsAdapter({
adapter: pubwiseAnalytics,
code: 'pubwise'
});

export default pubwiseAnalytics;

2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ var getParameterByName = function (name) {
return decodeURIComponent(results[1].replace(/\+/g, ' '));
};

exports.getParameterByName = getParameterByName;

/**
* This function validates paramaters.
* @param {object[string]} paramObj [description]
Expand Down