diff --git a/modules/googleAnalyticsAdapter.js b/modules/googleAnalyticsAdapter.js deleted file mode 100644 index 88ce20d07fa..00000000000 --- a/modules/googleAnalyticsAdapter.js +++ /dev/null @@ -1,298 +0,0 @@ -/** - * ga.js - analytics adapter for google analytics - */ - -import { _each, logMessage } from '../src/utils.js'; -import * as events from '../src/events.js'; -import CONSTANTS from '../src/constants.json'; -import adapterManager from '../src/adapterManager.js'; - -var BID_REQUESTED = CONSTANTS.EVENTS.BID_REQUESTED; -var BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT; -var BID_RESPONSE = CONSTANTS.EVENTS.BID_RESPONSE; -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 _cpmDistribution = null; -var _trackerSend = null; -var _sampled = true; -var _sendFloors = false; - -let adapter = {}; - -/** - * 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] - */ -adapter.enableAnalytics = function ({ provider, options }) { - _gaGlobal = provider || 'ga'; - _trackerSend = options && options.trackerName ? options.trackerName + '.send' : 'send'; - _sampled = typeof options === 'undefined' || typeof options.sampling === 'undefined' || - Math.random() < parseFloat(options.sampling); - - if (options && typeof options.global !== 'undefined') { - _gaGlobal = options.global; - } - if (options && typeof options.enableDistribution !== 'undefined') { - _enableDistribution = options.enableDistribution; - } - if (options && typeof options.cpmDistribution === 'function') { - _cpmDistribution = options.cpmDistribution; - } - if (options && typeof options.sendFloors !== 'undefined') { - _sendFloors = options.sendFloors; - } - - var bid = null; - - if (_sampled) { - // first send all events fired before enableAnalytics called - - var existingEvents = events.getEvents(); - - _each(existingEvents, function (eventObj) { - if (typeof eventObj !== 'object') { - return; - } - var args = eventObj.args; - - if (eventObj.eventType === BID_REQUESTED) { - bid = args; - sendBidRequestToGa(bid); - } else if (eventObj.eventType === BID_RESPONSE) { - // bid is 2nd args - bid = args; - sendBidResponseToGa(bid); - } else if (eventObj.eventType === BID_TIMEOUT) { - const bidderArray = args; - sendBidTimeouts(bidderArray); - } else if (eventObj.eventType === BID_WON) { - bid = args; - sendBidWonToGa(bid); - } - }); - - // 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); - }); - } else { - logMessage('Prebid.js google analytics disabled by sampling'); - } - - // finally set this function to return log message, prevents multiple adapter listeners - this.enableAnalytics = function _enable() { - return logMessage(`Analytics adapter already enabled, unnecessary call to \`enableAnalytics\`.`); - }; -}; - -adapter.getTrackerSend = function getTrackerSend() { - return _trackerSend; -}; - -/** - * Check if gaGlobal or window.ga is defined on page. If defined execute all the GA commands - */ -function checkAnalytics() { - if (_enableCheck && typeof window[_gaGlobal] === 'function') { - for (var i = 0; i < _analyticsQueue.length; i++) { - _analyticsQueue[i].call(); - } - - // override push to execute the command immediately from now on - _analyticsQueue.push = function (fn) { - fn.call(); - }; - - // turn check into NOOP - _enableCheck = false; - } - - logMessage('event count sent to GA: ' + _eventCount); -} - -function convertToCents(dollars) { - if (dollars) { - return Math.floor(dollars * 100); - } - - return 0; -} - -function getLoadTimeDistribution(time) { - var distribution; - if (time >= 0 && time < 200) { - distribution = '0-200ms'; - } else if (time >= 200 && time < 300) { - distribution = '0200-300ms'; - } else if (time >= 300 && time < 400) { - distribution = '0300-400ms'; - } else if (time >= 400 && time < 500) { - distribution = '0400-500ms'; - } else if (time >= 500 && time < 600) { - distribution = '0500-600ms'; - } else if (time >= 600 && time < 800) { - distribution = '0600-800ms'; - } else if (time >= 800 && time < 1000) { - distribution = '0800-1000ms'; - } else if (time >= 1000 && time < 1200) { - distribution = '1000-1200ms'; - } else if (time >= 1200 && time < 1500) { - distribution = '1200-1500ms'; - } else if (time >= 1500 && time < 2000) { - distribution = '1500-2000ms'; - } else if (time >= 2000) { - distribution = '2000ms above'; - } - - return distribution; -} - -function getCpmDistribution(cpm) { - if (_cpmDistribution) { - return _cpmDistribution(cpm); - } - var distribution; - if (cpm >= 0 && cpm < 0.5) { - distribution = '$0-0.5'; - } else if (cpm >= 0.5 && cpm < 1) { - distribution = '$0.5-1'; - } else if (cpm >= 1 && cpm < 1.5) { - distribution = '$1-1.5'; - } else if (cpm >= 1.5 && cpm < 2) { - distribution = '$1.5-2'; - } else if (cpm >= 2 && cpm < 2.5) { - distribution = '$2-2.5'; - } else if (cpm >= 2.5 && cpm < 3) { - distribution = '$2.5-3'; - } else if (cpm >= 3 && cpm < 4) { - distribution = '$3-4'; - } else if (cpm >= 4 && cpm < 6) { - distribution = '$4-6'; - } else if (cpm >= 6 && cpm < 8) { - distribution = '$6-8'; - } else if (cpm >= 8) { - distribution = '$8 above'; - } - - return distribution; -} - -function sendBidRequestToGa(bid) { - if (bid && bid.bidderCode) { - _analyticsQueue.push(function () { - _eventCount++; - if (_sendFloors) { - var floor = 'No Floor'; - if (bid.floorData) { - floor = bid.floorData.floorValue; - } else if (bid.bids.length) { - floor = bid.bids[0].getFloor().floor; - } - window[_gaGlobal](_trackerSend, 'event', _category, 'Requests by Floor=' + floor, bid.bidderCode, 1, _disableInteraction); - } else { - window[_gaGlobal](_trackerSend, 'event', _category, 'Requests', bid.bidderCode, 1, _disableInteraction); - } - }); - } - - // check the queue - checkAnalytics(); -} - -function sendBidResponseToGa(bid) { - if (bid && bid.bidderCode) { - _analyticsQueue.push(function () { - var cpmCents = convertToCents(bid.cpm); - var bidder = bid.bidderCode; - if (typeof bid.timeToRespond !== 'undefined' && _enableDistribution) { - _eventCount++; - var dis = getLoadTimeDistribution(bid.timeToRespond); - window[_gaGlobal](_trackerSend, 'event', 'Prebid.js Load Time Distribution', dis, bidder, 1, _disableInteraction); - } - - if (bid.cpm > 0) { - _eventCount = _eventCount + 2; - var cpmDis = getCpmDistribution(bid.cpm); - if (_enableDistribution) { - _eventCount++; - window[_gaGlobal](_trackerSend, 'event', 'Prebid.js CPM Distribution', cpmDis, bidder, 1, _disableInteraction); - } - if (_sendFloors) { - var floor = (bid.floorData) ? bid.floorData.floorValue : 'No Floor'; - window[_gaGlobal](_trackerSend, 'event', _category, 'Bids by Floor=' + floor, 'Size=' + bid.size + ',' + bidder, cpmCents, _disableInteraction); - } else { - window[_gaGlobal](_trackerSend, 'event', _category, 'Bids', bidder, cpmCents, _disableInteraction); - } - window[_gaGlobal](_trackerSend, 'event', _category, 'Bid Load Time', bidder, bid.timeToRespond, _disableInteraction); - } - }); - } - - // check the queue - checkAnalytics(); -} - -function sendBidTimeouts(timedOutBidders) { - _analyticsQueue.push(function () { - _each(timedOutBidders, function (bidderCode) { - _eventCount++; - var bidderName = bidderCode.bidder; - window[_gaGlobal](_trackerSend, 'event', _category, 'Timeouts', bidderName, _disableInteraction); - }); - }); - - checkAnalytics(); -} - -function sendBidWonToGa(bid) { - var cpmCents = convertToCents(bid.cpm); - _analyticsQueue.push(function () { - _eventCount++; - if (_sendFloors) { - var floor = (bid.floorData) ? bid.floorData.floorValue : 'No Floor'; - window[_gaGlobal](_trackerSend, 'event', _category, 'Wins by Floor=' + floor, 'Size=' + bid.size + ',' + bid.bidderCode, cpmCents, _disableInteraction); - } else { - window[_gaGlobal](_trackerSend, 'event', _category, 'Wins', bid.bidderCode, cpmCents, _disableInteraction); - } - }); - - checkAnalytics(); -} - -/** - * Exposed for testing purposes - */ -adapter.getCpmDistribution = getCpmDistribution; - -adapterManager.registerAnalyticsAdapter({ - adapter, - code: 'ga' -}); - -export default adapter; diff --git a/modules/googleAnalyticsAdapter.md b/modules/googleAnalyticsAdapter.md deleted file mode 100644 index 8c31b07b811..00000000000 --- a/modules/googleAnalyticsAdapter.md +++ /dev/null @@ -1,38 +0,0 @@ -# Google Analytics Adapter - -The google analytics adapter pushes prebid events into google analytics. - -## Usage - -The simplest way to enable the analytics adapter is this - -```javascript -pbjs.enableAnalytics([{ - provider: 'ga' -}]); -``` - -Defaults will be used and you should see events being pushed to analytics. - -You can customize the adapter with various `options` like this - -```javascript -pbjs.enableAnalytics([{ - provider: 'ga', - options: { ... } -}]); - -Here is a full list of settings available - -- `global` (string) - name of the global analytics object. Default is `ga` -- `trackerName` (string) - use another tracker for prebid events. Default is the default tracker -- `sampling` (number) - choose a value from `0` to `1`, where `0` means 0% and `1` means 100% tracked -- `enableDistribution` (boolean) - enables additional events that track load time and cpm distribution - by creating buckets for load time and cpm -- `cpmDistribution` (cpm: number => string) - customize the cpm buckets for the cpm distribution -- `sendFloors` (boolean) - if set, will include floor data in the eventCategory field and include ad unit code in eventAction field - - -## Additional resources - -- [Prebid GA Analytics](http://prebid.org/overview/ga-analytics.html) diff --git a/test/spec/modules/googleAnalyticsAdapter_spec.js b/test/spec/modules/googleAnalyticsAdapter_spec.js deleted file mode 100644 index b801b5fe696..00000000000 --- a/test/spec/modules/googleAnalyticsAdapter_spec.js +++ /dev/null @@ -1,27 +0,0 @@ -import ga from 'modules/googleAnalyticsAdapter.js'; - -var assert = require('assert'); - -describe('Ga', function () { - describe('enableAnalytics', function () { - var cpmDistribution = function(cpm) { - return cpm <= 1 ? '<= 1$' : '> 1$'; - } - var config = { options: { trackerName: 'foo', enableDistribution: true, cpmDistribution: cpmDistribution } }; - - // enableAnalytics can only be called once - ga.enableAnalytics(config); - - it('should accept a tracker name option and output prefixed send string', function () { - var output = ga.getTrackerSend(); - assert.equal(output, 'foo.send'); - }); - - it('should use the custom cpm distribution', function() { - assert.equal(ga.getCpmDistribution(0.5), '<= 1$'); - assert.equal(ga.getCpmDistribution(1), '<= 1$'); - assert.equal(ga.getCpmDistribution(2), '> 1$'); - assert.equal(ga.getCpmDistribution(5.23), '> 1$'); - }); - }); -});