Skip to content

Instantly share code, notes, and snippets.

@brs14ku
Created October 12, 2016 16:18
Show Gist options
  • Save brs14ku/5e30c8ced30b89b8b4858be79037466d to your computer and use it in GitHub Desktop.
Save brs14ku/5e30c8ced30b89b8b4858be79037466d to your computer and use it in GitHub Desktop.
Sample GA adapter
/**
* ga.js - analytics adapter for google analytics
*/
var events = require('./../../events');
var utils = require('./../../utils');
var CONSTANTS = require('./../../constants.json');
var BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT;
var BID_WON = CONSTANTS.EVENTS.BID_WON;
var _disableInteraction = { nonInteraction: true };
var _analyticsQueue = [];
var _gaGlobal = null;
// var _enableCheck = true;
var _category = 'Prebid.js Bids';
var _eventCount = 0;
var _enableDistribution = false;
var _trackerSend = null;
/**
* This will enable sending data to google analytics. Only call once, or duplicate data will be sent!
* @param {object} provider use to set GA global (if renamed);
* @param {object} options use to configure adapter;
* @return {[type]} [description]
*/
exports.enableAnalytics = function ({ provider, options }) {
utils.logMessage('im running the ga file');
_gaGlobal = provider || 'ga';
_trackerSend = options && options.trackerName ? options.trackerName + '.send' : 'send';
if (options && typeof options.enableDistribution !== 'undefined') {
_enableDistribution = options.enableDistribution;
}
// var bid = null;
//first send all events fired before enableAnalytics called
// var existingEvents = events.getEvents();
// utils._each(existingEvents, function (eventObj) {
// var args = eventObj.args;
// if (!eventObj) {
// return;
// }
// if (eventObj.eventType === BID_TIMEOUT) {
// const bidderArray = args;
// sendBidTimeouts(bidderArray);
// } else if (eventObj.eventType === BID_WON) {
// bid = args;
// sendBidWonToGa(bid);
// }
// console.log( 'existing events', existingEvents );
// });
//Next register event listeners to send data immediately
// //bidRequests
// events.on(BID_REQUESTED, function (bidRequestObj) {
// sendBidRequestToGa(bidRequestObj);
// });
// //bidResponses
// events.on(BID_RESPONSE, function (bid) {
// sendBidResponseToGa(bid);
// });
//bidTimeouts
events.on(BID_TIMEOUT, function (bidderArray) {
sendBidTimeouts(bidderArray);
});
//wins
events.on(BID_WON, function (bid) {
sendBidWonToGa(bid);
});
// finally set this function to return log message, prevents multiple adapter listeners
this.enableAnalytics = function _enable() {
return utils.logMessage(`Analytics adapter already enabled, unnecessary call to \`enableAnalytics\`.`);
};
};
exports.getTrackerSend = function getTrackerSend() {
return _trackerSend;
};
function convertToCents(dollars) {
if (dollars) {
return Math.floor(dollars * 100);
}
return 0;
}
function sendBidTimeouts(timedOutBidders) {
_analyticsQueue.push(function () {
utils._each(timedOutBidders, function (bidderCode) {
_eventCount++;
window[_gaGlobal](_trackerSend, 'event', _category, 'Timeouts', bidderCode, _disableInteraction);
});
});
}
function sendBidWonToGa(bid) {
var cpmCents = convertToCents(bid.cpm);
_analyticsQueue.push(function () {
_eventCount++;
window[_gaGlobal](_trackerSend, 'event', _category, 'Wins', bid.bidderCode, cpmCents, _disableInteraction);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment