Skip to content

Commit 7160d3d

Browse files
authored
fix(plugin-facebook): Fix Dedup issues in Facebook Plugin (segmentio#929)
* fix(plugin-facebook): add event_id for dedup * fix(plugin-facebook): bump FBSDK-Next to latest, add logtime test * fix(plugin-facebook): update lockfile
1 parent da01df8 commit 7160d3d

4 files changed

Lines changed: 59 additions & 35 deletions

File tree

packages/plugins/plugin-facebook-app-events/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@
4545
"homepage": "https://github.com/segmentio/analytics-react-native/tree/master/packages/plugins/plugin-facebook-app-events#readme",
4646
"peerDependencies": {
4747
"@segment/analytics-react-native": "^2.18.0",
48-
"react-native-fbsdk-next": "^11"
48+
"react-native-fbsdk-next": "^12"
4949
},
5050
"devDependencies": {
5151
"@segment/analytics-react-native": "^2.18.0",
5252
"@segment/analytics-rn-shared": "workspace:^",
5353
"@segment/sovran-react-native": "^1.1.0",
5454
"jest": "^29.7.0",
5555
"react-native-builder-bob": "^0.23.1",
56-
"react-native-fbsdk-next": "^11",
56+
"react-native-fbsdk-next": "^12",
5757
"rimraf": "^5.0.5",
5858
"typescript": "^5.2.2"
5959
},

packages/plugins/plugin-facebook-app-events/src/FacebookAppEventsPlugin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
SegmentClient,
1313
SegmentError,
1414
TrackEventType,
15+
unknownToString,
1516
UpdateType,
1617
} from '@segment/analytics-react-native';
1718
import { AppEventsLogger, Settings } from 'react-native-fbsdk-next';
@@ -72,6 +73,13 @@ const sanitizeEvent = (
7273
params._logTime = event._logTime;
7374
}
7475

76+
// Map messageId to event_id to support FB deduplication
77+
// https://developers.facebook.com/docs/marketing-api/conversions-api/deduplicate-pixel-and-server-events#event-deduplication-options
78+
const messageId = unknownToString(event.messageId);
79+
if (messageId !== null && messageId !== undefined && messageId !== '') {
80+
params.event_id = messageId;
81+
}
82+
7583
return {
7684
...params,
7785
fb_num_items: productCount,

packages/plugins/plugin-facebook-app-events/src/__tests__/FacebookAppEventsPlugin.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
SegmentClient,
3+
SegmentEvent,
34
TrackEventType,
45
UpdateType,
56
} from '@segment/analytics-react-native';
@@ -118,6 +119,7 @@ describe('FacebookAppEventsPlugin', () => {
118119

119120
it('logs a custom event', () => {
120121
const payload = {
122+
messageId: '12345',
121123
type: 'track',
122124
event: 'ACTION',
123125
context: {
@@ -132,12 +134,13 @@ describe('FacebookAppEventsPlugin', () => {
132134
foo: 'bar',
133135
},
134136
timestamp: '2021-11-08T21:42:32.242Z',
135-
};
137+
} as SegmentEvent;
136138

137139
const expected = {
138140
_appVersion: '1.0',
139141
_logTime: 1636407752,
140142
fb_num_items: 0,
143+
event_id: '12345',
141144
};
142145

143146
plugin.track(payload as TrackEventType);
@@ -147,6 +150,7 @@ describe('FacebookAppEventsPlugin', () => {
147150

148151
it('logs an order completed event', () => {
149152
const payload = {
153+
messageId: '12345',
150154
type: 'track',
151155
event: 'Order Completed',
152156
context: {
@@ -167,6 +171,7 @@ describe('FacebookAppEventsPlugin', () => {
167171
const expected = {
168172
_appVersion: '1.0',
169173
_logTime: 1636407752,
174+
event_id: '12345',
170175
fb_num_items: 0,
171176
_valueToSum: 10,
172177
};
@@ -181,6 +186,7 @@ describe('FacebookAppEventsPlugin', () => {
181186

182187
it('maps event names', () => {
183188
const payload = {
189+
messageId: '12345',
184190
type: 'track',
185191
event: 'original_event',
186192
context: {
@@ -200,6 +206,7 @@ describe('FacebookAppEventsPlugin', () => {
200206
const expected = {
201207
_appVersion: '1.0',
202208
_logTime: 1636407752,
209+
event_id: '12345',
203210
fb_num_items: 0,
204211
};
205212

@@ -213,6 +220,7 @@ describe('FacebookAppEventsPlugin', () => {
213220

214221
it('skips logTime when timestamp is not a date', () => {
215222
const payload = {
223+
messageId: '12345',
216224
type: 'track',
217225
event: 'ACTION',
218226
context: {
@@ -231,6 +239,40 @@ describe('FacebookAppEventsPlugin', () => {
231239

232240
const expected = {
233241
_appVersion: '1.0',
242+
event_id: '12345',
243+
fb_num_items: 0,
244+
};
245+
246+
plugin.track(payload as TrackEventType);
247+
248+
expect(AppEventsLogger.logEvent).toHaveBeenCalledWith('ACTION', expected);
249+
});
250+
251+
it('logTime is converted to number', () => {
252+
const time = new Date(1704267463 * 1000).toISOString();
253+
254+
const payload = {
255+
messageId: '12345',
256+
type: 'track',
257+
event: 'ACTION',
258+
context: {
259+
app: {
260+
build: '1',
261+
name: 'Analytics',
262+
namespace: 'org.reactjs.native.AnalyticsReactNativeExample',
263+
version: '1.0',
264+
},
265+
},
266+
properties: {
267+
foo: 'bar',
268+
},
269+
timestamp: time,
270+
};
271+
272+
const expected = {
273+
_appVersion: '1.0',
274+
_logTime: Math.floor(Date.parse(time) / 1000), // Unix timestamp
275+
event_id: '12345',
234276
fb_num_items: 0,
235277
};
236278

yarn.lock

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,12 +3532,12 @@ __metadata:
35323532
"@segment/sovran-react-native": "npm:^1.1.0"
35333533
jest: "npm:^29.7.0"
35343534
react-native-builder-bob: "npm:^0.23.1"
3535-
react-native-fbsdk-next: "npm:^11"
3535+
react-native-fbsdk-next: "npm:^12"
35363536
rimraf: "npm:^5.0.5"
35373537
typescript: "npm:^5.2.2"
35383538
peerDependencies:
35393539
"@segment/analytics-react-native": ^2.18.0
3540-
react-native-fbsdk-next: ^11
3540+
react-native-fbsdk-next: ^12
35413541
languageName: unknown
35423542
linkType: soft
35433543

@@ -13123,18 +13123,16 @@ __metadata:
1312313123
languageName: node
1312413124
linkType: hard
1312513125

13126-
"react-native-fbsdk-next@npm:^11":
13127-
version: 11.3.0
13128-
resolution: "react-native-fbsdk-next@npm:11.3.0"
13129-
dependencies:
13130-
xml2js: "npm:^0.4.23"
13126+
"react-native-fbsdk-next@npm:^12":
13127+
version: 12.1.4
13128+
resolution: "react-native-fbsdk-next@npm:12.1.4"
1313113129
peerDependencies:
1313213130
expo: ">=47.0.0"
1313313131
react-native: ">=0.63.3"
1313413132
peerDependenciesMeta:
1313513133
expo:
1313613134
optional: true
13137-
checksum: 10c0/8122cfac39c93886255dc6335e4d3d3361c8a64636773767e4daf700acb319f673c910b12b55230f3b46d320437157bc3e8f6753588125e31a3f789bd622f378
13135+
checksum: 10c0/b9722c0936bc6373af330b7429be45c8b6d87600a8a863f367a581928133d4009b7802411440b8e64c36a42506c42f0f70dd3bf917c565b78d3af4f425cb442a
1313813136
languageName: node
1313913137
linkType: hard
1314013138

@@ -13715,13 +13713,6 @@ __metadata:
1371513713
languageName: node
1371613714
linkType: hard
1371713715

13718-
"sax@npm:>=0.6.0":
13719-
version: 1.3.0
13720-
resolution: "sax@npm:1.3.0"
13721-
checksum: 10c0/599dbe0ba9d8bd55e92d920239b21d101823a6cedff71e542589303fa0fa8f3ece6cf608baca0c51be846a2e88365fac94a9101a9c341d94b98e30c4deea5bea
13722-
languageName: node
13723-
linkType: hard
13724-
1372513716
"scheduler@npm:0.24.0-canary-efb381bbf-20230505":
1372613717
version: 0.24.0-canary-efb381bbf-20230505
1372713718
resolution: "scheduler@npm:0.24.0-canary-efb381bbf-20230505"
@@ -15555,23 +15546,6 @@ __metadata:
1555515546
languageName: node
1555615547
linkType: hard
1555715548

15558-
"xml2js@npm:^0.4.23":
15559-
version: 0.4.23
15560-
resolution: "xml2js@npm:0.4.23"
15561-
dependencies:
15562-
sax: "npm:>=0.6.0"
15563-
xmlbuilder: "npm:~11.0.0"
15564-
checksum: 10c0/a3f41c9afc46d5bd0bea4070e5108777b605fd5ce2ebb978a68fd4c75513978ad5939f8135664ffea6f1adb342f391b1ba1584ed7955123b036e9ab8a1d26566
15565-
languageName: node
15566-
linkType: hard
15567-
15568-
"xmlbuilder@npm:~11.0.0":
15569-
version: 11.0.1
15570-
resolution: "xmlbuilder@npm:11.0.1"
15571-
checksum: 10c0/74b979f89a0a129926bc786b913459bdbcefa809afaa551c5ab83f89b1915bdaea14c11c759284bb9b931e3b53004dbc2181e21d3ca9553eeb0b2a7b4e40c35b
15572-
languageName: node
15573-
linkType: hard
15574-
1557515549
"xtend@npm:~4.0.1":
1557615550
version: 4.0.2
1557715551
resolution: "xtend@npm:4.0.2"

0 commit comments

Comments
 (0)