Skip to content

Commit 2d1a0fa

Browse files
authored
Merge pull request #22 from segment-integrations/gate-behind-option
gate initialization with traits behind option
2 parents 29a0760 + fa315d3 commit 2d1a0fa

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

lib/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var FacebookPixel = module.exports = integration('Facebook Pixel')
1818
.global('fbq')
1919
.option('pixelId', '')
2020
.option('agent', 'seg')
21+
.option('initWithExistingTraits', false)
2122
.mapping('standardEvents')
2223
.mapping('legacyEvents')
2324
.tag('<script src="//connect.facebook.net/en_US/fbevents.js">');
@@ -45,12 +46,12 @@ FacebookPixel.prototype.initialize = function() {
4546
window.fbq.version = '2.0';
4647
window.fbq.queue = [];
4748
this.load(this.ready);
48-
var traits = formatTraits(this.analytics);
49-
window.fbq(
50-
'init',
51-
this.options.pixelId,
52-
traits
53-
);
49+
if (this.options.initWithExistingTraits) {
50+
var traits = formatTraits(this.analytics);
51+
window.fbq('init', this.options.pixelId, traits);
52+
} else {
53+
window.fbq('init', this.options.pixelId);
54+
}
5455
};
5556

5657
/**
@@ -240,9 +241,9 @@ function formatRevenue(revenue) {
240241

241242
/**
242243
* Get Traits Formatted Correctly for FB.
243-
*
244+
*
244245
* https://developers.facebook.com/docs/facebook-pixel/pixel-with-ads/conversion-tracking#advanced_match
245-
*
246+
*
246247
* @api private
247248
*/
248249

@@ -263,7 +264,6 @@ function formatTraits(analytics) {
263264
}
264265
var gender = traits.gender && traits.gender.slice(0,1).toLowerCase();
265266
var birthday = traits.birthday && moment(traits.birthday).format('YYYYMMDD');
266-
267267
var address = traits.address || {};
268268
var city = address.city && address.city.split(' ').join('').toLowerCase();
269269
var state = address.state && address.state.toLowerCase();

test/index.test.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe('Facebook Pixel', function() {
1616
standardEvent: 'standard'
1717
},
1818
pixelId: '123123123',
19-
agent: 'test'
19+
agent: 'test',
20+
initWithExistingTraits: false
2021
};
2122

2223
beforeEach(function() {
@@ -47,7 +48,6 @@ describe('Facebook Pixel', function() {
4748
describe('before loading', function() {
4849
beforeEach(function() {
4950
analytics.stub(facebookPixel, 'load');
50-
analytics.initialize();
5151
});
5252

5353
afterEach(function() {
@@ -56,21 +56,48 @@ describe('Facebook Pixel', function() {
5656

5757
describe('#initialize', function() {
5858
it('should call load on initialize', function() {
59+
analytics.initialize();
5960
analytics.called(facebookPixel.load);
6061
});
6162

6263
it('should set the correct agent and version', function() {
64+
analytics.initialize();
6365
analytics.equal(window.fbq.agent, 'test');
6466
analytics.equal(window.fbq.version, '2.0');
6567
});
6668

6769
it('should set disablePushState to true', function() {
70+
analytics.initialize();
6871
analytics.equal(window.fbq.disablePushState, true);
6972
});
7073

7174
it('should create fbq object', function() {
75+
analytics.initialize();
7276
analytics.assert(window.fbq instanceof Function);
7377
});
78+
79+
before(function() {
80+
options.initWithExistingTraits = true;
81+
});
82+
83+
after(function() {
84+
options.initWithExistingTraits = false;
85+
});
86+
87+
it('should call init with the user\'s traits if option enabled', function() {
88+
var payload = {
89+
ct: 'emerald',
90+
db: '19910113',
91+
fn: 'ash',
92+
ge: 'm',
93+
ln: 'ketchum',
94+
st: 'kanto',
95+
zp: 123456
96+
};
97+
analytics.stub(window, 'fbq');
98+
analytics.initialize();
99+
analytics.called(window.fbq, 'init', options.pixelId, payload);
100+
});
74101
});
75102
});
76103

@@ -83,23 +110,6 @@ describe('Facebook Pixel', function() {
83110
it('should load', function(done) {
84111
analytics.load(facebookPixel, done);
85112
});
86-
87-
it('should call init with the user\'s traits', function() {
88-
analytics.called(
89-
window.fbq,
90-
'init',
91-
options.pixelId,
92-
{
93-
ct: 'emerald',
94-
db: '19910113',
95-
fn: 'ash',
96-
ge: 'm',
97-
ln: 'ketchum',
98-
st: 'kanto',
99-
zp: 123456
100-
}
101-
);
102-
});
103113
});
104114

105115
describe('after loading', function() {

0 commit comments

Comments
 (0)