Skip to content

Commit af24f30

Browse files
authored
feat: Support page view de-duplication (#23)
1 parent 23846ab commit af24f30

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/FacebookEventForwarder.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
PageEvent: 4,
2424
CrashReport: 5,
2525
OptOut: 6,
26-
Commerce: 16
26+
Commerce: 16,
2727
},
2828
IdentityType = {
2929
Other: 0,
@@ -49,6 +49,14 @@
4949
PhoneNumber3: 21,
5050
},
5151
SupportedCommerceTypes = [],
52+
// Standard FB Event Names from https://developers.facebook.com/docs/facebook-pixel/reference#standard-events
53+
ADD_TO_CART_EVENT_NAME = 'AddToCart';
54+
ADD_TO_WISHLIST_EVENT_NAME = 'AddToWishlist';
55+
CHECKOUT_EVENT_NAME = 'InitiateCheckout';
56+
PAGE_VIEW_EVENT_NAME = 'PageView';
57+
PURCHASE_EVENT_NAME = 'Purchase';
58+
REMOVE_FROM_CART_EVENT_NAME = 'RemoveFromCart';
59+
VIEW_CONTENT_EVENT_NAME = 'ViewContent';
5260
constructor = function () {
5361
var self = this,
5462
isInitialized = false,
@@ -90,9 +98,15 @@
9098
visitorData['external_id'] = selectedIdentity[0].Identity;
9199
}
92100
}
101+
}
93102

94-
fbq('init', settings.pixelId, visitorData);
103+
if (settings.disablePushState === 'True') {
104+
// Facebook will automatically track page views whenever a new state is pushed to the HTML 5 History State API
105+
// this option can be disabled to prevent duplicate page views
106+
// https://developers.facebook.com/docs/facebook-pixel/implementation/tag_spa/#tagging-single-page-applications
107+
fbq.disablePushState = true;
95108
}
109+
fbq('init', settings.pixelId, visitorData);
96110

97111
isInitialized = true;
98112

@@ -187,20 +201,20 @@
187201
params['value'] = totalValue;
188202

189203
if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToWishlist){
190-
eventName = 'AddToWishlist';
204+
eventName = ADD_TO_WISHLIST_EVENT_NAME;
191205
}
192206
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.AddToCart){
193-
eventName = 'AddToCart';
207+
eventName = ADD_TO_CART_EVENT_NAME;
194208
}
195209
else{
196-
eventName = 'ViewContent';
210+
eventName = VIEW_CONTENT_EVENT_NAME;
197211
}
198212

199213
}
200214
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ||
201215
event.ProductAction.ProductActionType == mParticle.ProductActionType.Purchase) {
202216

203-
eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? 'InitiateCheckout' : 'Purchase';
217+
eventName = event.ProductAction.ProductActionType == mParticle.ProductActionType.Checkout ? CHECKOUT_EVENT_NAME : PURCHASE_EVENT_NAME;
204218

205219
if (event.ProductAction.TotalAmount) {
206220
params['value'] = event.ProductAction.TotalAmount;
@@ -215,7 +229,7 @@
215229
params['num_items'] = num_items;
216230
}
217231
else if (event.ProductAction.ProductActionType == mParticle.ProductActionType.RemoveFromCart) {
218-
eventName = 'RemoveFromCart';
232+
eventName = REMOVE_FROM_CART_EVENT_NAME;
219233

220234
// remove from cart can be performed in 1 of 2 ways:
221235
// 1. mParticle.eCommerce.logProductEvent(), which contains event.ProductAction.TotalAmount
@@ -252,7 +266,7 @@
252266
}
253267

254268
function logPageView(event) {
255-
logPageEvent(event, 'Viewed ' + event.EventName);
269+
logPageEvent(event, PAGE_VIEW_EVENT_NAME);
256270
}
257271

258272
function logPageEvent(event, eventName) {
@@ -263,6 +277,7 @@
263277
if (event.EventName) {
264278
params['content_name'] = event.EventName;
265279
}
280+
266281
fbq('trackCustom', eventName || 'customEvent', params, eventID);
267282
}
268283

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
window.mParticle = new mp();
2323
</script>
24-
<script src="../FacebookEventForwarder.js" data-cover></script>
24+
<script src="../dist/FacebookEventForwarder.iife.js" data-cover></script>
2525

2626
<script>mocha.setup('bdd')</script>
2727

test/test-bundle.js.tmp-browserify-41344641690044814375

Whitespace-only changes.

test/tests.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ describe('Facebook Forwarder', function () {
243243
});
244244

245245
checkBasicProperties('trackCustom');
246-
window.fbqObj.should.have.property('eventName', 'Viewed testevent');
246+
window.fbqObj.should.have.property('eventName', 'PageView');
247247
done();
248248
});
249249

@@ -255,8 +255,27 @@ describe('Facebook Forwarder', function () {
255255
});
256256

257257
checkBasicProperties('trackCustom');
258-
window.fbqObj.should.have.property('eventName', 'Viewed testevent');
259-
window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID)
258+
window.fbqObj.should.have.property('eventName', 'PageView');
259+
window.fbqObj.eventData.should.have.property(
260+
'eventID',
261+
SOURCE_MESSAGE_ID
262+
);
263+
264+
done();
265+
});
266+
267+
it('should disable push state if passed in settings', function (done) {
268+
mParticle.forwarder.init(
269+
{
270+
pixelCode: '1228810793810857',
271+
disablePushState: 'True',
272+
},
273+
reportService.cb,
274+
true
275+
);
276+
277+
window.fbq.should.have.property('disablePushState', true);
278+
// window.fbqObj.eventData.should.have.property('eventID', SOURCE_MESSAGE_ID)
260279

261280
done();
262281
});

0 commit comments

Comments
 (0)