@segment/analytics-browser-actions-facebook-conversions-api-web 1.9.0 → 1.9.1-staging-c01044061.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/functions.ts CHANGED
@@ -1,64 +1,52 @@
1
- import { WindowWithOptionalFbq, InitOptions, LDU, UserData, FBClient} from './types'
1
+ import { WindowWithOptionalFbq, InitOptions, LDU, UserData, FBClient } from './types'
2
2
  import type { Settings } from './generated-types'
3
- import { USER_DATA_KEY, INIT_COUNT_KEY} from './constants'
3
+ import { USER_DATA_KEY, INIT_COUNT_KEY } from './constants'
4
4
  import { UniversalStorage, Analytics } from '@segment/analytics-next'
5
5
 
6
6
  export function initScript(settings: Settings, analytics: Analytics) {
7
- const {
8
- pixelId,
9
- disablePushState,
10
- disableAutoConfig,
11
- disableFirstPartyCookies,
12
- agent,
13
- ldu
14
- } = settings as Settings & { ldu: keyof typeof LDU }
15
-
16
- (function(
17
- f: WindowWithOptionalFbq,
18
- b: Document,
19
- e: 'script',
20
- v: string,
21
- n: FBClient | undefined = undefined,
22
- t: HTMLScriptElement | undefined = undefined,
7
+ const { pixelId, disablePushState, disableAutoConfig, disableFirstPartyCookies, agent, ldu } =
8
+ settings as Settings & { ldu: keyof typeof LDU }
9
+
10
+ ;(function (
11
+ f: WindowWithOptionalFbq,
12
+ b: Document,
13
+ e: 'script',
14
+ v: string,
15
+ n: FBClient | undefined = undefined,
16
+ t: HTMLScriptElement | undefined = undefined,
23
17
  s: Element | null = null
24
- ){
25
- if (f.fbq) return;
26
- n = f.fbq = function() {
18
+ ) {
19
+ if (f.fbq) return
20
+ n = f.fbq = function () {
27
21
  /* eslint-disable */
28
22
  // @ts-expect-error - n is defined by the time this executes
29
- n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments);
23
+ n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments)
30
24
  /* eslint-enable */
31
- };
32
- if (!f._fbq) f._fbq = n;
33
- n.push = n;
34
- n.loaded = true;
35
- n.version = '2.0';
36
- n.queue = [];
37
- t = b.createElement(e);
38
- t.async = true;
39
- t.src = v;
40
- s = b.getElementsByTagName(e)[0];
25
+ }
26
+ if (!f._fbq) f._fbq = n
27
+ n.push = n
28
+ n.loaded = true
29
+ n.version = '2.0'
30
+ n.queue = []
31
+ t = b.createElement(e)
32
+ t.async = true
33
+ t.src = v
34
+ s = b.getElementsByTagName(e)[0]
41
35
  if (s && s.parentNode) {
42
- s.parentNode.insertBefore(t, s);
36
+ s.parentNode.insertBefore(t, s)
43
37
  }
44
- })(
45
- window,
46
- document,
47
- 'script',
48
- 'https://connect.facebook.net/en_US/fbevents.js'
49
- );
50
-
51
- if(ldu === LDU.Disabled.key) {
38
+ })(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js')
39
+
40
+ if (ldu === LDU.Disabled.key) {
52
41
  window.fbq('dataProcessingOptions', [])
53
- }
54
- else {
42
+ } else {
55
43
  const lduObj = getLDU(ldu)
56
44
  window.fbq('dataProcessingOptions', ['LDU'], lduObj.country, lduObj.state)
57
45
  }
58
46
 
59
- if(disablePushState) {
47
+ if (disablePushState) {
60
48
  window.fbq.disablePushState = true
61
- }
49
+ }
62
50
 
63
51
  if (disableAutoConfig) {
64
52
  window.fbq('set', 'autoConfig', false, pixelId)
@@ -69,13 +57,13 @@ export function initScript(settings: Settings, analytics: Analytics) {
69
57
  }
70
58
 
71
59
  const userData = getStoredUserData(analytics)
72
- const options: InitOptions | undefined = ( agent ? { agent } : undefined)
60
+ const options: InitOptions | undefined = agent ? { agent } : undefined
73
61
  const initArgs: [string, UserData?, InitOptions?] = [pixelId]
74
62
 
75
63
  if (userData && Object.keys(userData).length > 0) {
76
- initArgs.push(userData);
64
+ initArgs.push(userData)
77
65
  if (options) {
78
- initArgs.push(options);
66
+ initArgs.push(options)
79
67
  }
80
68
  } else if (options) {
81
69
  initArgs.push(undefined, options)
@@ -86,7 +74,7 @@ export function initScript(settings: Settings, analytics: Analytics) {
86
74
  setStorageInitCount(analytics, 1)
87
75
  deleteStorageUserData(analytics)
88
76
 
89
- if(!disablePushState) {
77
+ if (!disablePushState) {
90
78
  window.fbq('trackSingle', pixelId, 'PageView')
91
79
  }
92
80
  }
@@ -118,24 +106,29 @@ export function deleteStorageUserData(analytics: Analytics) {
118
106
  storage.set(USER_DATA_KEY, '')
119
107
  }
120
108
 
121
- function getStoredUserData(analytics: Analytics): UserData | undefined {
122
- const storage = (analytics.storage as UniversalStorage<Record<string, string>>) ?? storageFallback
123
- const userDataFromStorage: string | null = storage.get(USER_DATA_KEY)
124
- if(userDataFromStorage) {
125
- try {
126
- const parsed = JSON.parse(userDataFromStorage)
127
- if(!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || Object.keys(parsed as object).length === 0) {
128
- return undefined
129
- }
130
- return parsed as UserData
131
- } catch {
132
- return undefined
133
- }
109
+ function getStoredUserData(analytics: Analytics): UserData | undefined {
110
+ const storage = (analytics.storage as UniversalStorage<Record<string, string>>) ?? storageFallback
111
+ const userDataFromStorage: string | null = storage.get(USER_DATA_KEY)
112
+ if (userDataFromStorage) {
113
+ try {
114
+ const parsed = JSON.parse(userDataFromStorage)
115
+ if (
116
+ !parsed ||
117
+ typeof parsed !== 'object' ||
118
+ Array.isArray(parsed) ||
119
+ Object.keys(parsed as object).length === 0
120
+ ) {
121
+ return undefined
122
+ }
123
+ return parsed as UserData
124
+ } catch {
125
+ return undefined
134
126
  }
135
- return undefined
127
+ }
128
+ return undefined
136
129
  }
137
130
 
138
131
  function getLDU(ldu: keyof typeof LDU) {
139
- const lduObj = LDU[ldu]
140
- return { country: lduObj.country, state: lduObj.state }
141
- }
132
+ const lduObj = LDU[ldu]
133
+ return { country: lduObj.country, state: lduObj.state }
134
+ }
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@ import { defaultValues } from '@segment/actions-core'
8
8
 
9
9
  declare global {
10
10
  interface Window {
11
- fbq: FBClient,
11
+ fbq: FBClient
12
12
  _fbq: FBClient
13
13
  }
14
14
  }
@@ -23,22 +23,25 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
23
23
  description: 'The Pixel ID associated with your Facebook Pixel.',
24
24
  label: 'Pixel ID',
25
25
  type: 'string',
26
- required: true
26
+ required: true
27
27
  },
28
28
  disablePushState: {
29
- description: "If set to true, prevents Facebook Pixel from sending PageView events on history state changes. Set to true if you want to trigger PageView events manually via the pageView Action.",
29
+ description:
30
+ 'If set to true, prevents Facebook Pixel from sending PageView events on history state changes. Set to true if you want to trigger PageView events manually via the pageView Action.',
30
31
  label: 'Disable Push State',
31
32
  type: 'boolean',
32
33
  default: false
33
34
  },
34
35
  disableAutoConfig: {
35
- description: "Control whether Facebook’s Meta Pixel automatically collects additional page and button data to optimize ads and measurement. When this toggle is on, Auto Config is disabled and only basic pixel tracking will occur. Turning it off enables Auto Config, allowing the Pixel to automatically send page metadata and button interactions to improve ad delivery and reporting.",
36
+ description:
37
+ 'Control whether Facebook’s Meta Pixel automatically collects additional page and button data to optimize ads and measurement. When this toggle is on, Auto Config is disabled and only basic pixel tracking will occur. Turning it off enables Auto Config, allowing the Pixel to automatically send page metadata and button interactions to improve ad delivery and reporting.',
36
38
  label: 'Disable Auto Config',
37
39
  type: 'boolean',
38
40
  default: true
39
41
  },
40
42
  disableFirstPartyCookies: {
41
- description: "Control whether Facebook’s Meta Pixel uses first-party cookies. When this toggle is on, first-party cookies are disabled, enhancing user privacy. Turning it off enables the use of first-party cookies for more accurate tracking.",
43
+ description:
44
+ 'Control whether Facebook’s Meta Pixel uses first-party cookies. When this toggle is on, first-party cookies are disabled, enhancing user privacy. Turning it off enables the use of first-party cookies for more accurate tracking.',
42
45
  label: 'Disable First Party Cookies',
43
46
  type: 'boolean',
44
47
  default: false
@@ -55,8 +58,8 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
55
58
  type: 'string',
56
59
  required: true,
57
60
  choices: [
58
- { label: 'LDU disabled', value: LDU.Disabled.key},
59
- { label: "LDU enabled - Use Meta Geolocation Logic", value: LDU.GeolocationLogic.key },
61
+ { label: 'LDU disabled', value: LDU.Disabled.key },
62
+ { label: 'LDU enabled - Use Meta Geolocation Logic', value: LDU.GeolocationLogic.key },
60
63
  { label: 'LDU enabled - California only', value: LDU.California.key },
61
64
  { label: 'LDU enabled - Colorado only', value: LDU.Colorado.key },
62
65
  { label: 'LDU enabled - Connecticut only', value: LDU.Connecticut.key },
@@ -87,8 +90,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
87
90
  name: 'AddPaymentInfo',
88
91
  subscribe: 'event = "Payment Info Entered"',
89
92
  partnerAction: 'send',
90
- mapping:
91
- {
93
+ mapping: {
92
94
  ...defaultValues(send.fields),
93
95
  event_config: {
94
96
  event_name: 'AddPaymentInfo',
@@ -101,8 +103,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
101
103
  name: 'AddToCart',
102
104
  subscribe: 'event = "Product Added"',
103
105
  partnerAction: 'send',
104
- mapping:
105
- {
106
+ mapping: {
106
107
  ...defaultValues(send.fields),
107
108
  event_config: {
108
109
  event_name: 'AddToCart',
@@ -117,13 +118,12 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
117
118
  value: { '@path': '$.properties.price' }
118
119
  },
119
120
  type: 'automatic'
120
- },
121
+ },
121
122
  {
122
123
  name: 'AddToWishlist',
123
124
  subscribe: 'event = "Product Added To Wishlist"',
124
125
  partnerAction: 'send',
125
- mapping:
126
- {
126
+ mapping: {
127
127
  ...defaultValues(send.fields),
128
128
  event_config: {
129
129
  event_name: 'AddToWishlist',
@@ -143,8 +143,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
143
143
  name: 'CompleteRegistration',
144
144
  subscribe: 'event = "Signed Up"',
145
145
  partnerAction: 'send',
146
- mapping:
147
- {
146
+ mapping: {
148
147
  ...defaultValues(send.fields),
149
148
  event_config: {
150
149
  event_name: 'CompleteRegistration',
@@ -152,13 +151,12 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
152
151
  }
153
152
  },
154
153
  type: 'automatic'
155
- },
154
+ },
156
155
  {
157
156
  name: 'InitiateCheckout',
158
157
  subscribe: 'event = "Checkout Started"',
159
158
  partnerAction: 'send',
160
- mapping:
161
- {
159
+ mapping: {
162
160
  ...defaultValues(send.fields),
163
161
  event_config: {
164
162
  event_name: 'InitiateCheckout',
@@ -186,14 +184,13 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
186
184
  name: 'Purchase',
187
185
  subscribe: 'event = "Order Completed"',
188
186
  partnerAction: 'send',
189
- mapping:
190
- {
187
+ mapping: {
191
188
  ...defaultValues(send.fields),
192
189
  event_config: {
193
190
  event_name: 'Purchase',
194
191
  show_fields: false
195
192
  },
196
- value: { '@path': '$.properties.revenue' },
193
+ value: { '@path': '$.properties.revenue' },
197
194
  custom_data: {
198
195
  order_id: { '@path': '$.properties.order_id' }
199
196
  }
@@ -204,8 +201,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
204
201
  name: 'Search',
205
202
  subscribe: 'event = "Products Searched"',
206
203
  partnerAction: 'send',
207
- mapping:
208
- {
204
+ mapping: {
209
205
  ...defaultValues(send.fields),
210
206
  event_config: {
211
207
  event_name: 'Search',
@@ -224,8 +220,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
224
220
  name: 'ViewContent',
225
221
  subscribe: 'event = "Product Viewed"',
226
222
  partnerAction: 'send',
227
- mapping:
228
- {
223
+ mapping: {
229
224
  ...defaultValues(send.fields),
230
225
  event_config: {
231
226
  event_name: 'ViewContent',
@@ -235,7 +230,7 @@ export const destination: BrowserDestinationDefinition<Settings, FBClient> = {
235
230
  id: { '@path': '$.properties.product_id' },
236
231
  quantity: { '@path': '$.properties.quantity' },
237
232
  item_price: { '@path': '$.properties.price' }
238
- },
233
+ },
239
234
  content_ids: { '@path': '$.properties.product_id' },
240
235
  value: { '@path': '$.properties.price' }
241
236
  },
@@ -2,47 +2,79 @@ import { DependsOnConditions, Condition } from '@segment/actions-core/destinatio
2
2
  import type { FBStandardEventType, FBNonStandardEventType } from '../types'
3
3
 
4
4
  export const fieldDependencies: Record<string, (FBStandardEventType | FBNonStandardEventType)[]> = {
5
- custom_event_name: ['CustomEvent'],
6
- content_category: ['PageView', 'ViewContent', 'Search'],
7
- content_ids: ['AddPaymentInfo','AddToCart','AddToWishlist','InitiateCheckout','Purchase','Search','ViewContent'],
8
- content_name: ['PageView', 'ViewContent', 'Search'],
9
- content_type: ['AddToCart', 'Purchase', 'Search', 'ViewContent'],
10
- contents: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'InitiateCheckout', 'Purchase', 'Search', 'ViewContent'],
11
- currency: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'CompleteRegistration', 'InitiateCheckout', 'Lead', 'Purchase', 'Search', 'StartTrial', 'Subscribe', 'ViewContent'],
12
- delivery_category: ['Purchase', 'InitiateCheckout'],
13
- num_items: ['InitiateCheckout'],
14
- predicted_ltv: ['Purchase', 'Subscribe', 'StartTrial', 'CompleteRegistration', 'AddPaymentInfo', 'CustomEvent' ],
15
- net_revenue: ['Purchase'],
16
- search_string: ['Search'],
17
- status: ['CompleteRegistration'],
18
- value: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'CompleteRegistration', 'InitiateCheckout', 'Lead', 'Purchase', 'Search', 'StartTrial', 'Subscribe', 'ViewContent'],
5
+ custom_event_name: ['CustomEvent'],
6
+ content_category: ['PageView', 'ViewContent', 'Search'],
7
+ content_ids: [
8
+ 'AddPaymentInfo',
9
+ 'AddToCart',
10
+ 'AddToWishlist',
11
+ 'InitiateCheckout',
12
+ 'Purchase',
13
+ 'Search',
14
+ 'ViewContent'
15
+ ],
16
+ content_name: ['PageView', 'ViewContent', 'Search'],
17
+ content_type: ['AddToCart', 'Purchase', 'Search', 'ViewContent'],
18
+ contents: ['AddPaymentInfo', 'AddToCart', 'AddToWishlist', 'InitiateCheckout', 'Purchase', 'Search', 'ViewContent'],
19
+ currency: [
20
+ 'AddPaymentInfo',
21
+ 'AddToCart',
22
+ 'AddToWishlist',
23
+ 'CompleteRegistration',
24
+ 'InitiateCheckout',
25
+ 'Lead',
26
+ 'Purchase',
27
+ 'Search',
28
+ 'StartTrial',
29
+ 'Subscribe',
30
+ 'ViewContent'
31
+ ],
32
+ delivery_category: ['Purchase', 'InitiateCheckout'],
33
+ num_items: ['InitiateCheckout'],
34
+ predicted_ltv: ['Purchase', 'Subscribe', 'StartTrial', 'CompleteRegistration', 'AddPaymentInfo', 'CustomEvent'],
35
+ net_revenue: ['Purchase'],
36
+ search_string: ['Search'],
37
+ status: ['CompleteRegistration'],
38
+ value: [
39
+ 'AddPaymentInfo',
40
+ 'AddToCart',
41
+ 'AddToWishlist',
42
+ 'CompleteRegistration',
43
+ 'InitiateCheckout',
44
+ 'Lead',
45
+ 'Purchase',
46
+ 'Search',
47
+ 'StartTrial',
48
+ 'Subscribe',
49
+ 'ViewContent'
50
+ ]
19
51
  }
20
52
 
21
53
  export function getDependenciesFor(fieldName: string): DependsOnConditions {
22
54
  const conditions: Condition[] = [
23
- {
24
- fieldKey: 'event_config.show_fields',
25
- operator: 'is',
26
- value: 'true'
27
- }
55
+ {
56
+ fieldKey: 'event_config.show_fields',
57
+ operator: 'is',
58
+ value: 'true'
59
+ }
28
60
  ]
29
61
 
30
62
  if (fieldDependencies[fieldName]) {
31
- conditions.push({
32
- fieldKey: 'event_config.event_name',
33
- operator: 'is',
34
- value: fieldDependencies[fieldName]
35
- })
63
+ conditions.push({
64
+ fieldKey: 'event_config.event_name',
65
+ operator: 'is',
66
+ value: fieldDependencies[fieldName]
67
+ })
36
68
  }
37
69
 
38
70
  return {
39
- match: 'any',
40
- conditions
71
+ match: 'any',
72
+ conditions
41
73
  }
42
74
  }
43
75
 
44
76
  export function getNotVisibleForEvent(event: FBStandardEventType | FBNonStandardEventType): string[] {
45
- return Object.entries(fieldDependencies)
46
- .filter(([_, events]) => !events.includes(event))
47
- .map(([fieldName]) => fieldName)
48
- }
77
+ return Object.entries(fieldDependencies)
78
+ .filter(([_, events]) => !events.includes(event))
79
+ .map(([fieldName]) => fieldName)
80
+ }