Skip to content

Commit

Permalink
Add integration perfect-audience
Browse files Browse the repository at this point in the history
  • Loading branch information
SegmentDestinationsBot committed Dec 10, 2019
1 parent 265ccbc commit e247b74
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 0 deletions.
48 changes: 48 additions & 0 deletions integrations/perfect-audience/HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

2.2.0 / 2016-09-07
==================

* support ecom spec v2

2.1.1 / 2016-08-16
==================

* Fix for release

2.1.0 / 2016-08-16
==================

* Merge pull request #2 from segment-integrations/update/ecommerce-spec-v2
* update ecommerce spec syntax to v2

2.0.0 / 2016-06-21
==================

* Remove Duo compatibility
* Add CI setup (coverage, linting, cross-browser compatibility, etc.)
* Update eslint configuration

1.0.4 / 2016-05-07
==================

* Bump Analytics.js core, tester, integration to use Facade 2.x

1.0.3 / 2015-06-30
==================

* Replace analytics.js dependency with analytics.js-core

1.0.2 / 2015-06-24
==================

* Bump analytics.js-integration version

1.0.1 / 2015-06-24
==================

* Bump analytics.js-integration version

1.0.0 / 2015-06-09
==================

* Initial commit :sparkles:
12 changes: 12 additions & 0 deletions integrations/perfect-audience/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# analytics.js-integration-perfect-audience [![Build Status][ci-badge]][ci-link]

Perfect Audience integration for [Analytics.js][].

## License

Released under the [MIT license](LICENSE).


[Analytics.js]: https://segment.com/docs/libraries/analytics.js/
[ci-link]: https://circleci.com/gh/segment-integrations/analytics.js-integration-perfect-audience
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-perfect-audience.svg?style=svg
102 changes: 102 additions & 0 deletions integrations/perfect-audience/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

/**
* Module dependencies.
*/

var integration = require('@segment/analytics.js-integration');
var push = require('global-queue')('_pq');

/**
* Expose `PerfectAudience` integration.
*/

var PerfectAudience = module.exports = integration('Perfect Audience')
.assumesPageview()
.global('_pq')
.option('siteId', '')
.tag('<script src="//tag.perfectaudience.com/serve/{{ siteId }}.js">');

/**
* Initialize.
*
* http://support.perfectaudience.com/knowledgebase/articles/212490-visitor-tracking-api
*
* @api public
*/

PerfectAudience.prototype.initialize = function() {
window._pq = window._pq || [];
this.load(this.ready);
};

/**
* Loaded?
*
* @api private
* @return {boolean}
*/

PerfectAudience.prototype.loaded = function() {
return !!(window._pq && window._pq.push);
};

/**
* Track.
*
* http://support.perfectaudience.com/knowledgebase/articles/212490-visitor-tracking-api
*
* @api public
* @param {Track} event
*/

PerfectAudience.prototype.track = function(track) {
var total = track.total() || track.revenue();
var orderId = track.orderId();
var props = {};
var sendProps = false;
if (total) {
props.revenue = total;
sendProps = true;
}
if (orderId) {
props.orderId = orderId;
sendProps = true;
}

if (!sendProps) return push('track', track.event());
return push('track', track.event(), props);
};

/**
* Product viewed.
*
* http://support.perfectaudience.com/knowledgebase/articles/212490-visitor-tracking-api
*
* @api private
* @param {Track} track
*/

PerfectAudience.prototype.productViewed = function(track) {
var product = track.productId() || track.id() || track.sku();
push('track', track.event());
push('trackProduct', product);
};

/**
* Completed Purchase.
*
* http://support.perfectaudience.com/knowledgebase/articles/212490-visitor-tracking-api
*
* @api private
* @param {Track} track
*/

PerfectAudience.prototype.orderCompleted = function(track) {
var total = track.total() || track.revenue();
var orderId = track.orderId();
var props = {};
if (total) props.revenue = total;
if (orderId) props.orderId = orderId;
push('track', track.event(), props);
};
54 changes: 54 additions & 0 deletions integrations/perfect-audience/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@segment/analytics.js-integration-perfect-audience",
"description": "The Perfect Audience analytics.js integration.",
"version": "2.2.0",
"keywords": [
"analytics.js",
"analytics.js-integration",
"segment",
"perfect-audience"
],
"main": "lib/index.js",
"scripts": {
"test": "make test"
},
"author": "Segment \u003cfriends@segment.com\u003e",
"license": "SEE LICENSE IN LICENSE",
"homepage": "https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/perfect-audience#readme",
"bugs": {
"url": "https://github.com/segmentio/analytics.js-integrations/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/segmentio/analytics.js-integrations.git"
},
"dependencies": {
"@segment/analytics.js-integration": "^3.1.0",
"global-queue": "^1.0.1"
},
"devDependencies": {
"@segment/analytics.js-core": "^3.0.0",
"@segment/analytics.js-integration-tester": "^3.1.0",
"@segment/clear-env": "^2.0.0",
"@segment/eslint-config": "^3.1.1",
"browserify": "^13.0.0",
"browserify-istanbul": "^2.0.0",
"eslint": "^2.9.0",
"eslint-plugin-mocha": "^2.2.0",
"eslint-plugin-require-path-exists": "^1.1.5",
"istanbul": "^0.4.3",
"karma": "1.3.0",
"karma-browserify": "^5.0.4",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-junit-reporter": "^1.0.0",
"karma-mocha": "1.0.1",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sauce-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.26",
"mocha": "^2.2.5",
"npm-check": "^5.2.1",
"phantomjs-prebuilt": "^2.1.7",
"watchify": "^3.7.0"
}
}
3 changes: 3 additions & 0 deletions integrations/perfect-audience/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@segment/eslint-config/mocha"
}
158 changes: 158 additions & 0 deletions integrations/perfect-audience/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
'use strict';

var Analytics = require('@segment/analytics.js-core').constructor;
var integration = require('@segment/analytics.js-integration');
var sandbox = require('@segment/clear-env');
var tester = require('@segment/analytics.js-integration-tester');
var PerfectAudience = require('../lib/');

describe('Perfect Audience', function() {
var analytics;
var perfect;
var options = {
siteId: '4ff6ade4361ed500020000a5'
};

beforeEach(function() {
analytics = new Analytics();
perfect = new PerfectAudience(options);
analytics.use(PerfectAudience);
analytics.use(tester);
analytics.add(perfect);
});

afterEach(function() {
analytics.restore();
analytics.reset();
perfect.reset();
sandbox();
});

it('should have the right settings', function() {
analytics.compare(PerfectAudience, integration('Perfect Audience')
.assumesPageview()
.global('_pq')
.option('siteId', ''));
});

describe('before loading', function() {
beforeEach(function() {
analytics.stub(perfect, 'load');
});

describe('#initialize', function() {
it('should create the window._pq object', function() {
analytics.assert(!window._pq);
analytics.initialize();
analytics.page();
analytics.assert(window._pq instanceof Array);
});

it('should call #load', function() {
analytics.initialize();
analytics.page();
analytics.called(perfect.load);
});
});
});

describe('loading', function() {
it('should load', function(done) {
analytics.load(perfect, done);
});
});

describe('after loading', function() {
beforeEach(function(done) {
analytics.once('ready', done);
analytics.initialize();
analytics.page();
});

describe('#track', function() {
beforeEach(function() {
analytics.stub(window._pq, 'push');
});

it('should send an event', function() {
analytics.track('event');
analytics.called(window._pq.push, ['track', 'event']);
});

it('should send event and orderId, revenue properties when passed', function() {
analytics.track('event', {
orderId: '12345',
total: 30
});
analytics.called(window._pq.push, ['track', 'event', { orderId: '12345', revenue: 30 }]);
});
});

describe('#productViewed', function() {
beforeEach(function() {
analytics.stub(window._pq, 'push');
});

it('should send a track and a trackProduct with product ID', function() {
analytics.track('Product Viewed', {
product_id: '507f1f77bcf86cd799439011',
sku: '45790-32',
name: 'Monopoly: 3rd Edition',
price: 18.99,
category: 'Games'
});
analytics.called(window._pq.push, ['track', 'Product Viewed']);
analytics.called(window._pq.push, ['trackProduct', '507f1f77bcf86cd799439011']);
});

it('should send a track and a trackProduct with product SKU, if there is not product ID', function() {
analytics.track('Product Viewed', {
sku: '45790-32',
name: 'Monopoly: 3rd Edition',
price: 18.99,
category: 'Games'
});
analytics.called(window._pq.push, ['track', 'Product Viewed']);
analytics.called(window._pq.push, ['trackProduct', '45790-32']);
});
});

describe('#orderCompleted', function() {
beforeEach(function() {
analytics.stub(window._pq, 'push');
});

it('should send event and orderId, revenue properties', function() {
analytics.track('Order Completed', {
order_id: '12345',
total: 30,
revenue: 25,
shipping: 3,
tax: 2,
discount: 2.5,
coupon: 'hasbros',
currency: 'USD',
products: [
{
id: '507f1f77bcf86cd799439011',
sku: '45790-32',
name: 'Monopoly: 3rd Edition',
price: 19,
quantity: 1,
category: 'Games'
},
{
id: '505bd76785ebb509fc183733',
sku: '46493-32',
name: 'Uno Card Game',
price: 3,
quantity: 2,
category: 'Games'
}
]
});
analytics.called(window._pq.push, ['track', 'Order Completed', { orderId: '12345', revenue: 30 }]);
});
});
});
});

0 comments on commit e247b74

Please sign in to comment.