Skip to content

Commit

Permalink
Add integration facebook-conversion-tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
SegmentDestinationsBot committed Dec 10, 2019
1 parent 8dbc299 commit 670f183
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 0 deletions.
36 changes: 36 additions & 0 deletions integrations/facebook-conversion-tracking/HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
2.0.0 / 2016-06-21
==================

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

1.0.5 / 2016-05-07
==================

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

1.0.4 / 2015-06-30
==================

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

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/facebook-conversion-tracking/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# analytics.js-integration-facebook-conversion-tracking [![Build Status][ci-badge]][ci-link]

Facebook Conversion Tracking 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-facebook-conversion-tracking
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-facebook-conversion-tracking.svg?style=svg
78 changes: 78 additions & 0 deletions integrations/facebook-conversion-tracking/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

/**
* Module dependencies.
*/

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

/**
* Expose `Facebook`
*/

var Facebook = module.exports = integration('Facebook Conversion Tracking')
.global('_fbq')
.option('currency', 'USD')
.tag('<script src="//connect.facebook.net/en_US/fbds.js">')
.mapping('events');

/**
* Initialize Facebook Conversion Tracking
*
* https://developers.facebook.com/docs/ads-for-websites/conversion-pixel-code-migration
*
* @api public
*/

Facebook.prototype.initialize = function() {
window._fbq = window._fbq || [];
this.load(this.ready);
window._fbq.loaded = true;
};

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

Facebook.prototype.loaded = function() {
return !!(window._fbq && window._fbq.loaded);
};

/**
* Page.
*
* @api public
* @param {Page} page
*/

Facebook.prototype.page = function(page) {
this.track(page.track(page.fullName()));
};

/**
* Track.
*
* https://developers.facebook.com/docs/reference/ads-api/custom-audience-website-faq/#fbpixel
*
* @api public
* @param {Track} track
*/

Facebook.prototype.track = function(track) {
var event = track.event();
var events = this.events(event);
var revenue = track.revenue() || 0;
var self = this;

each(function(event) {
push('track', event, {
currency: self.options.currency,
value: revenue.toFixed(2)
});
}, events);
};
55 changes: 55 additions & 0 deletions integrations/facebook-conversion-tracking/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@segment/analytics.js-integration-facebook-conversion-tracking",
"description": "The Facebook Conversion Tracking analytics.js integration.",
"version": "2.0.0",
"keywords": [
"analytics.js",
"analytics.js-integration",
"segment",
"facebook-conversion-tracking"
],
"main": "lib/index.js",
"scripts": {
"test": "make test"
},
"author": "Segment \u003c[email protected]\u003e",
"license": "SEE LICENSE IN LICENSE",
"homepage": "https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/facebook-conversion-tracking#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": {
"@ndhoule/each": "^2.0.1",
"@segment/analytics.js-integration": "^2.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/facebook-conversion-tracking/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@segment/eslint-config/mocha"
}
118 changes: 118 additions & 0 deletions integrations/facebook-conversion-tracking/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'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 Facebook = require('../lib/');

describe('Facebook Conversion Tracking', function() {
var analytics;
var facebook;
var options = {
events: {
signup: 0,
login: 1,
play: 2,
'Loaded a Page': 3,
'Viewed Name Page': 4,
'Viewed Category Name Page': 5
}
};

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

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

it('should have the correct settings', function() {
analytics.compare(Facebook, integration('Facebook Conversion Tracking')
.option('currency', 'USD')
.mapping('events'));
});

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

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

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

it('should track unnamed/categorized page', function() {
analytics.page({ url: 'http://localhost:34448/test/' });
analytics.called(window._fbq.push, ['track', 3, {
currency: 'USD',
value: '0.00'
}]);
});

it('should track un-categorized page', function() {
analytics.page('Name', { url: 'http://localhost:34448/test/' });
analytics.called(window._fbq.push, ['track', 4, {
currency: 'USD',
value: '0.00'
}]);
});

it('should track page view with fullname', function() {
analytics.page('Category', 'Name', { url: 'http://localhost:34448/test/' });
analytics.called(window._fbq.push, ['track', 5, {
currency: 'USD',
value: '0.00'
}]);
});
});

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

it('should send event if found', function() {
analytics.track('signup', {});
analytics.called(window._fbq.push, ['track', 0, {
currency: 'USD',
value: '0.00'
}]);
});

it('should support array events', function() {
facebook.options.events = [{ key: 'event', value: 4 }];
analytics.track('event');
analytics.called(window._fbq.push, ['track', 4, {
currency: 'USD',
value: '0.00'
}]);
});

it('should send revenue', function() {
analytics.track('login', { revenue: '$50' });
analytics.called(window._fbq.push, ['track', 1, {
value: '50.00',
currency: 'USD'
}]);
});
});
});
});

0 comments on commit 670f183

Please sign in to comment.