Skip to content

Commit 17e7c0c

Browse files
authored
[FB Pixel] Add support for preset data processing options (#496)
* Add support for preset data processing options * Read preset data processing opts from settings * Update HISTORY.md
1 parent a5f905a commit 17e7c0c

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

integrations/facebook-pixel/HISTORY.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2.11.1/ 2020-07-22
2+
==================
3+
4+
* Add support to override the Data Processing Options by pass them in the load options object.
5+
```
6+
analytics.load("<writeKey>", {
7+
integrations: {
8+
'Facebook Pixel': {
9+
dataProcessingOptions: [['LDU'], 1, 1000]
10+
}
11+
}
12+
});
13+
```
14+
115
2.11.0/ 2020-07-16
216
==================
317

integrations/facebook-pixel/lib/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ FacebookPixel.prototype.initialize = function() {
104104
window.fbq('set', 'autoConfig', false, this.options.pixelId);
105105
}
106106
if (this.options.limitedDataUse) {
107-
window.fbq('dataProcessingOptions', ['LDU'], 0, 0);
107+
this.validateAndSetDataProcessing(
108+
this.options.dataProcessingOptions || [['LDU'], 0, 0]
109+
);
108110
}
109111
if (this.options.initWithExistingTraits) {
110112
var traits = this.formatTraits(this.analytics);
@@ -718,6 +720,29 @@ FacebookPixel.prototype.buildPayload = function(track, isStandardEvent) {
718720
return payload;
719721
};
720722

723+
/**
724+
* Validates that a set of parameters are formatted correctly and passes them to the pixel instance.
725+
* https://developers.facebook.com/docs/marketing-apis/data-processing-options#reference
726+
*
727+
* @param {Array} options
728+
*
729+
* @api private
730+
*/
731+
FacebookPixel.prototype.validateAndSetDataProcessing = function(params) {
732+
var lenOk = params.length === 3;
733+
var valOk =
734+
Array.isArray(params[0]) &&
735+
typeof params[1] === 'number' &&
736+
typeof params[2] === 'number';
737+
738+
// Pass the data processing options if they're valid, otherwise, fallback to geolocation.
739+
if (lenOk && valOk) {
740+
window.fbq('dataProcessingOptions', params[0], params[1], params[2]);
741+
} else {
742+
window.fbq('dataProcessingOptions', ['LDU'], 0, 0);
743+
}
744+
};
745+
721746
/**
722747
* Merge two javascript objects. This works similarly to `Object.assign({}, obj1, obj2)`
723748
* but it's compatible with old browsers. The properties of the first argument takes preference

integrations/facebook-pixel/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-facebook-pixel",
33
"description": "The Facebook Pixel analytics.js integration.",
4-
"version": "2.11.0",
4+
"version": "2.11.1",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/facebook-pixel/test/index.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,38 @@ describe('Facebook Pixel', function() {
200200
);
201201
});
202202
});
203+
204+
describe('#initialize with preset data processing options', function() {
205+
before(function() {
206+
options.dataProcessingOptions = [['LDU'], 99, 99];
207+
});
208+
209+
after(function() {
210+
delete options.dataProcessingOptions;
211+
});
212+
213+
it('should call dataProcessingOptions with the preset values', function() {
214+
analytics.stub(window, 'fbq');
215+
analytics.initialize();
216+
analytics.called(window.fbq, 'dataProcessingOptions', ['LDU'], 99, 99);
217+
});
218+
});
219+
220+
describe('#initialize with fallback data processing options', function() {
221+
before(function() {
222+
options.dataProcessingOptions = ['a string', true, 99];
223+
});
224+
225+
after(function() {
226+
delete options.dataProcessingOptions;
227+
});
228+
229+
it('should call dataProcessingOptions with fallback values', function() {
230+
analytics.stub(window, 'fbq');
231+
analytics.initialize();
232+
analytics.called(window.fbq, 'dataProcessingOptions', ['LDU'], 0, 0);
233+
});
234+
});
203235
});
204236

205237
describe('loading', function() {

0 commit comments

Comments
 (0)