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

Add cpmDistribution function for Google Analytics adapter #4240

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/googleAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _enableCheck = true;
var _category = 'Prebid.js Bids';
var _eventCount = 0;
var _enableDistribution = false;
var _cpmDistribution = null;
var _trackerSend = null;
var _sampled = true;

Expand All @@ -42,6 +43,9 @@ adapter.enableAnalytics = function ({ provider, options }) {
if (options && typeof options.enableDistribution !== 'undefined') {
_enableDistribution = options.enableDistribution;
}
if (options && typeof options.cpmDistribution === 'function') {
_cpmDistribution = options.cpmDistribution;
}

var bid = null;

Expand Down Expand Up @@ -166,6 +170,9 @@ function getLoadTimeDistribution(time) {
}

function getCpmDistribution(cpm) {
if (_cpmDistribution) {
return _cpmDistribution(cpm);
}
var distribution;
if (cpm >= 0 && cpm < 0.5) {
distribution = '$0-0.5';
Expand Down Expand Up @@ -255,6 +262,11 @@ function sendBidWonToGa(bid) {
checkAnalytics();
}

/**
* Exposed for testing purposes
*/
adapter.getCpmDistribution = getCpmDistribution;

adapterManager.registerAnalyticsAdapter({
adapter,
code: 'ga'
Expand Down
37 changes: 37 additions & 0 deletions modules/googleAnalyticsAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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


## Additional resources

- [Prebid GA Analytics](http://prebid.org/overview/ga-analytics.html)
18 changes: 15 additions & 3 deletions test/spec/modules/googleAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ var assert = require('assert');

describe('Ga', function () {
describe('enableAnalytics', function () {
it('should accept a tracker name option and output prefixed send string', function () {
var config = { options: { trackerName: 'foo' } };
ga.enableAnalytics(config);
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$');
});
});
});