Skip to content

Commit

Permalink
Updates to PubWise Analytics - Enable config, UTM & Debug/Logging Upd… (
Browse files Browse the repository at this point in the history
prebid#1409)

* Updates to Build Process to Support Building DIST and DEV Together

* Updates to PubWise Analytics - Enable config, UTM & Debug/Logging Updates

* Fixing ESLint Errors

* Updates for ESLint which is now properly installed

* Proper ESLint Fix Commit without Erroneous Files

* File Cleanup

* Additional File Cleanup to get Back to Upstream Master

* Updates for Comments from jaiminpanchal27

* Adding by in ParameterByName

* Updates w/ Utils
  • Loading branch information
GLStephen authored and philipwatson committed Sep 18, 2017
1 parent 7d8d98c commit 1177eac
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 24 deletions.
123 changes: 99 additions & 24 deletions modules/pubwiseAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,124 @@
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'
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,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

0 comments on commit 1177eac

Please sign in to comment.