Skip to content

Commit

Permalink
Make GA integration work with named trackers
Browse files Browse the repository at this point in the history
allow configuring ga tracker name and add some tests
add ga spec
coding style updates
update prefixedSend usage
use trackerSend instead of prefixedSend
fix tests

fix trackerSend undefined error - @protonate
  • Loading branch information
bborn authored and protonate committed Apr 8, 2016
1 parent 04ad2cc commit 3551d9e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/ga.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _category = 'Prebid.js Bids';
var _eventCount = 0;
var _enableDistribution = false;
var _timedOutBidders = [];
var _trackerSend = null;

/**
* This will enable sending data to google analytics. Only call once, or duplicate data will be sent!
Expand All @@ -33,6 +34,8 @@ exports.enableAnalytics = function (gaOptions) {
_gaGlobal = 'ga';
}

_trackerSend = gaOptions.trackerName ? gaOptions.trackerName + '.send' : 'send';

if (typeof gaOptions.enableDistribution !== 'undefined') {
_enableDistribution = gaOptions.enableDistribution;
}
Expand Down Expand Up @@ -88,6 +91,10 @@ exports.enableAnalytics = function (gaOptions) {
});
};

exports.getTrackerSend = function getTrackerSend() {
return _trackerSend;
};

/**
* Check if gaGlobal or window.ga is defined on page. If defined execute all the GA commands
*/
Expand Down Expand Up @@ -178,7 +185,7 @@ function sendBidRequestToGa(bid) {
if (bid && bid.bidderCode) {
_analyticsQueue.push(function () {
_eventCount++;
window[_gaGlobal]('send', 'event', _category, 'Requests', bid.bidderCode, 1, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', _category, 'Requests', bid.bidderCode, 1, _disibleInteraction);
});
}

Expand All @@ -195,19 +202,19 @@ function sendBidResponseToGa(bid) {
if (typeof bid.timeToRespond !== 'undefined' && _enableDistribution) {
_eventCount++;
var dis = getLoadTimeDistribution(bid.timeToRespond);
window[_gaGlobal]('send', 'event', 'Prebid.js Load Time Distribution', dis, bidder, 1, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', 'Prebid.js Load Time Distribution', dis, bidder, 1, _disibleInteraction);
}

if (bid.cpm > 0) {
_eventCount = _eventCount + 2;
var cpmDis = getCpmDistribution(bid.cpm);
if (_enableDistribution) {
_eventCount++;
window[_gaGlobal]('send', 'event', 'Prebid.js CPM Distribution', cpmDis, bidder, 1, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', 'Prebid.js CPM Distribution', cpmDis, bidder, 1, _disibleInteraction);
}

window[_gaGlobal]('send', 'event', _category, 'Bids', bidder, cpmCents, _disibleInteraction);
window[_gaGlobal]('send', 'event', _category, 'Bid Load Time', bidder, bid.timeToRespond, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', _category, 'Bids', bidder, cpmCents, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', _category, 'Bid Load Time', bidder, bid.timeToRespond, _disibleInteraction);
}
});
}
Expand All @@ -223,7 +230,7 @@ function sendBidTimeouts(bid) {
utils._each(_timedOutBidders, function (bidderCode) {
if (bid.bidder === bidderCode) {
_eventCount++;
window[_gaGlobal]('send', 'event', _category, 'Timeouts', bidderCode, bid.timeToRespond, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', _category, 'Timeouts', bidderCode, bid.timeToRespond, _disibleInteraction);
}
});
});
Expand All @@ -236,7 +243,7 @@ function sendBidWonToGa(bid) {
var cpmCents = convertToCents(bid.cpm);
_analyticsQueue.push(function () {
_eventCount++;
window[_gaGlobal]('send', 'event', _category, 'Wins', bid.bidderCode, cpmCents, _disibleInteraction);
window[_gaGlobal](_trackerSend, 'event', _category, 'Wins', bid.bidderCode, cpmCents, _disibleInteraction);
});

checkAnalytics();
Expand Down
28 changes: 28 additions & 0 deletions test/spec/ga_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var assert = require('assert');
var ga = require('../../src/ga');

describe('Ga', function () {

describe('enableAnalytics', function () {

it('should accept a tracker name option and output prefixed send string', function () {
var options = { trackerName: 'foo' };
ga.enableAnalytics(options);

var output = ga.getTrackerSend();
assert.equal(output, 'foo.send');
});


it('should output normal send string when trackerName is not set', function () {
var options = {};
ga.enableAnalytics(options);

var output = ga.getTrackerSend();
assert.equal(output, 'send');
});

});


});

0 comments on commit 3551d9e

Please sign in to comment.