@ht-sdks/events-sdk-js-browser 1.5.7 → 1.6.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.
Files changed (102) hide show
  1. package/README.md +41 -0
  2. package/dist/cjs/browser/dedupe-plugin-likes.js +53 -0
  3. package/dist/cjs/browser/dedupe-plugin-likes.js.map +1 -0
  4. package/dist/cjs/browser/index.js +89 -32
  5. package/dist/cjs/browser/index.js.map +1 -1
  6. package/dist/cjs/core/analytics/index.js +12 -8
  7. package/dist/cjs/core/analytics/index.js.map +1 -1
  8. package/dist/cjs/core/queue/event-queue.js +7 -3
  9. package/dist/cjs/core/queue/event-queue.js.map +1 -1
  10. package/dist/cjs/generated/version.js +1 -1
  11. package/dist/cjs/index.js.map +1 -1
  12. package/dist/cjs/plugins/analytics-node/index.js +1 -2
  13. package/dist/cjs/plugins/analytics-node/index.js.map +1 -1
  14. package/dist/cjs/plugins/built-in-plugins.js +5 -0
  15. package/dist/cjs/plugins/built-in-plugins.js.map +1 -0
  16. package/dist/cjs/plugins/facebook-params/index.js +115 -0
  17. package/dist/cjs/plugins/facebook-params/index.js.map +1 -0
  18. package/dist/cjs/plugins/index.js +26 -0
  19. package/dist/cjs/plugins/index.js.map +1 -0
  20. package/dist/pkg/browser/dedupe-plugin-likes.js +48 -0
  21. package/dist/pkg/browser/dedupe-plugin-likes.js.map +1 -0
  22. package/dist/pkg/browser/index.js +89 -32
  23. package/dist/pkg/browser/index.js.map +1 -1
  24. package/dist/pkg/core/analytics/index.js +12 -8
  25. package/dist/pkg/core/analytics/index.js.map +1 -1
  26. package/dist/pkg/core/queue/event-queue.js +7 -3
  27. package/dist/pkg/core/queue/event-queue.js.map +1 -1
  28. package/dist/pkg/generated/version.js +1 -1
  29. package/dist/pkg/index.js.map +1 -1
  30. package/dist/pkg/plugins/analytics-node/index.js +0 -1
  31. package/dist/pkg/plugins/analytics-node/index.js.map +1 -1
  32. package/dist/pkg/plugins/built-in-plugins.js +2 -0
  33. package/dist/pkg/plugins/built-in-plugins.js.map +1 -0
  34. package/dist/pkg/plugins/facebook-params/index.js +112 -0
  35. package/dist/pkg/plugins/facebook-params/index.js.map +1 -0
  36. package/dist/pkg/plugins/index.js +23 -0
  37. package/dist/pkg/plugins/index.js.map +1 -0
  38. package/dist/types/browser/dedupe-plugin-likes.d.ts +7 -0
  39. package/dist/types/browser/dedupe-plugin-likes.d.ts.map +1 -0
  40. package/dist/types/browser/index.d.ts.map +1 -1
  41. package/dist/types/core/analytics/index.d.ts +7 -1
  42. package/dist/types/core/analytics/index.d.ts.map +1 -1
  43. package/dist/types/core/buffer/index.d.ts +1 -1
  44. package/dist/types/generated/version.d.ts +1 -1
  45. package/dist/types/index.d.ts +1 -0
  46. package/dist/types/index.d.ts.map +1 -1
  47. package/dist/types/plugins/analytics-node/index.d.ts.map +1 -1
  48. package/dist/types/plugins/built-in-plugins.d.ts +3 -0
  49. package/dist/types/plugins/built-in-plugins.d.ts.map +1 -0
  50. package/dist/types/plugins/facebook-params/index.d.ts +27 -0
  51. package/dist/types/plugins/facebook-params/index.d.ts.map +1 -0
  52. package/dist/types/plugins/index.d.ts +11 -0
  53. package/dist/types/plugins/index.d.ts.map +1 -0
  54. package/dist/umd/407.bundle.5cd2d2d625cfaa9e2f6e.js.map +1 -1
  55. package/dist/umd/407.bundle.5cd2d2d625cfaa9e2f6e.js.map.gz +0 -0
  56. package/dist/umd/ajs-destination.bundle.b5407b09b321c1d47d7c.js.map +1 -1
  57. package/dist/umd/ajs-destination.bundle.b5407b09b321c1d47d7c.js.map.gz +0 -0
  58. package/dist/umd/auto-track.bundle.68ce13c2007188a9368d.js.map +1 -1
  59. package/dist/umd/auto-track.bundle.68ce13c2007188a9368d.js.map.gz +0 -0
  60. package/dist/umd/events.min.js +1 -1
  61. package/dist/umd/events.min.js.gz +0 -0
  62. package/dist/umd/events.min.js.map +1 -1
  63. package/dist/umd/events.min.js.map.gz +0 -0
  64. package/dist/umd/facebook-params.bundle.bc6597a53dc18e288950.js +2 -0
  65. package/dist/umd/facebook-params.bundle.bc6597a53dc18e288950.js.gz +0 -0
  66. package/dist/umd/facebook-params.bundle.bc6597a53dc18e288950.js.map +1 -0
  67. package/dist/umd/facebook-params.bundle.bc6597a53dc18e288950.js.map.gz +0 -0
  68. package/dist/umd/google-tag-manager.bundle.5dbd0ff300a5c429a406.js.map +1 -1
  69. package/dist/umd/google-tag-manager.bundle.5dbd0ff300a5c429a406.js.map.gz +0 -0
  70. package/dist/umd/gtag.bundle.fa18b007fc2891339158.js.map +1 -1
  71. package/dist/umd/gtag.bundle.fa18b007fc2891339158.js.map.gz +0 -0
  72. package/dist/umd/index.js +1 -1
  73. package/dist/umd/index.js.gz +0 -0
  74. package/dist/umd/index.js.map +1 -1
  75. package/dist/umd/index.js.map.gz +0 -0
  76. package/dist/umd/legacyVideos.bundle.d1d6291274937f505b12.js.map +1 -1
  77. package/dist/umd/legacyVideos.bundle.d1d6291274937f505b12.js.map.gz +0 -0
  78. package/dist/umd/meta-param-builder.bundle.aab3a6e0c91673752d5a.js +2 -0
  79. package/dist/umd/meta-param-builder.bundle.aab3a6e0c91673752d5a.js.gz +0 -0
  80. package/dist/umd/meta-param-builder.bundle.aab3a6e0c91673752d5a.js.map +1 -0
  81. package/dist/umd/meta-param-builder.bundle.aab3a6e0c91673752d5a.js.map.gz +0 -0
  82. package/dist/umd/queryString.bundle.b86b3b6ef44fbbb98ec4.js.map +1 -1
  83. package/dist/umd/queryString.bundle.b86b3b6ef44fbbb98ec4.js.map.gz +0 -0
  84. package/dist/umd/remoteMiddleware.bundle.9945b0dd526dedecf614.js.map +1 -1
  85. package/dist/umd/remoteMiddleware.bundle.9945b0dd526dedecf614.js.map.gz +0 -0
  86. package/dist/umd/schemaFilter.bundle.babb6cdbe396c20dc88f.js.map +1 -1
  87. package/dist/umd/schemaFilter.bundle.babb6cdbe396c20dc88f.js.map.gz +0 -0
  88. package/dist/umd/tsub-middleware.bundle.b717b62fe947259b0454.js.map +1 -1
  89. package/dist/umd/tsub-middleware.bundle.b717b62fe947259b0454.js.map.gz +0 -0
  90. package/package.json +15 -18
  91. package/src/browser/dedupe-plugin-likes.ts +62 -0
  92. package/src/browser/index.ts +70 -11
  93. package/src/core/analytics/index.ts +9 -2
  94. package/src/core/queue/event-queue.ts +1 -1
  95. package/src/generated/version.ts +1 -1
  96. package/src/index.ts +1 -0
  97. package/src/plugins/analytics-node/index.ts +0 -1
  98. package/src/plugins/built-in-plugins.ts +3 -0
  99. package/src/plugins/facebook-params/index.ts +121 -0
  100. package/src/plugins/index.ts +22 -0
  101. package/src/test-helpers/fixtures/classic-destination.ts +23 -8
  102. package/src/types/meta-capi-param-builder-clientjs.d.ts +32 -0
@@ -0,0 +1,121 @@
1
+ import type { Context } from '../../core/context'
2
+ import type { Plugin } from '../../core/plugin'
3
+ import { PluginType } from '@ht-sdks/events-sdk-js-core'
4
+ import { Analytics } from '../../core/analytics'
5
+ import { isServer } from '../../core/environment'
6
+ import type { ClientParamBuilder } from 'meta-capi-param-builder-clientjs'
7
+
8
+ const SDK_LOAD_TIMEOUT_MS = 2000
9
+ const SDK_LOAD_TIMEOUT_MESSAGE = 'Facebook Parameter Builder SDK load timed out'
10
+
11
+ function withTimeout<T>(
12
+ promise: Promise<T>,
13
+ ms: number,
14
+ message: string
15
+ ): Promise<T> {
16
+ let timer: ReturnType<typeof setTimeout>
17
+
18
+ const timeout = new Promise<never>((_, reject) => {
19
+ timer = setTimeout(() => reject(new Error(message)), ms)
20
+ })
21
+
22
+ return Promise.race([promise, timeout]).finally(() => {
23
+ clearTimeout(timer)
24
+ })
25
+ }
26
+
27
+ class FacebookParamsPlugin implements Plugin {
28
+ private clientParamBuilder: ClientParamBuilder | null = null
29
+ private sdkReady = false
30
+
31
+ name = 'Facebook Parameters'
32
+ type: PluginType = 'enrichment'
33
+ version = '0.1.0'
34
+
35
+ isLoaded = () => this.sdkReady
36
+
37
+ load = async (_ctx: Context, _instance: Analytics): Promise<void> => {
38
+ if (isServer()) {
39
+ // Server-side rendering - skip loading
40
+ return Promise.resolve()
41
+ }
42
+
43
+ // Runs to completion on its own, even if the race below gives up waiting
44
+ const init = (async () => {
45
+ const paramBuilderModule = await import(
46
+ /* webpackChunkName: "meta-param-builder" */ 'meta-capi-param-builder-clientjs'
47
+ )
48
+ const clientParamBuilder = (paramBuilderModule.default ??
49
+ paramBuilderModule) as ClientParamBuilder
50
+
51
+ if (
52
+ !clientParamBuilder ||
53
+ typeof clientParamBuilder.processAndCollectAllParams !== 'function' ||
54
+ typeof clientParamBuilder.getFbc !== 'function' ||
55
+ typeof clientParamBuilder.getFbp !== 'function'
56
+ ) {
57
+ console.warn(
58
+ 'Facebook Parameter Builder SDK loaded but clientParamBuilder is not available or does not expose expected interface'
59
+ )
60
+ return
61
+ }
62
+
63
+ await clientParamBuilder.processAndCollectAllParams()
64
+ this.clientParamBuilder = clientParamBuilder
65
+ this.sdkReady = true
66
+ })().catch((error) => {
67
+ console.warn('Failed to load Facebook Parameter Builder SDK:', error)
68
+ })
69
+
70
+ // Timeout only bounds how long the event queue waits — it doesn't cancel init
71
+ await _instance.queue.criticalTasks.run(() =>
72
+ withTimeout(init, SDK_LOAD_TIMEOUT_MS, SDK_LOAD_TIMEOUT_MESSAGE).catch(
73
+ () => {}
74
+ )
75
+ )
76
+ }
77
+
78
+ private enrich = (ctx: Context): Context => {
79
+ if (!this.sdkReady || !this.clientParamBuilder) {
80
+ return ctx
81
+ }
82
+
83
+ try {
84
+ const evtCtx = ctx.event.context
85
+ if (!evtCtx) {
86
+ return ctx
87
+ }
88
+
89
+ // Get fbc and fbp from cookies (processAndCollectAllParams was called during load)
90
+ const fbc = this.clientParamBuilder.getFbc()
91
+ const fbp = this.clientParamBuilder.getFbp()
92
+
93
+ // Only add if not empty (getFbc/getFbp return empty string if cookie doesn't exist)
94
+ if (fbc) {
95
+ evtCtx.fbc = fbc
96
+ }
97
+
98
+ if (fbp) {
99
+ evtCtx.fbp = fbp
100
+ }
101
+ } catch (error) {
102
+ // Silently fail - don't break event processing if SDK has issues
103
+ console.warn('Error extracting Facebook parameters:', error)
104
+ }
105
+
106
+ return ctx
107
+ }
108
+
109
+ track = this.enrich
110
+ identify = this.enrich
111
+ page = this.enrich
112
+ group = this.enrich
113
+ alias = this.enrich
114
+ screen = this.enrich
115
+ }
116
+
117
+ /**
118
+ * Default plugin instance using Meta's official ParamBuilder SDK.
119
+ * Used when plugin is loaded via string name: plugins: ["facebook-params"]
120
+ */
121
+ export const facebookParams = new FacebookParamsPlugin()
@@ -0,0 +1,22 @@
1
+ import type { Plugin } from '../core/plugin'
2
+ import type { BuiltInPluginName } from './built-in-plugins'
3
+
4
+ /**
5
+ * Creates a plugin instance from a string name.
6
+ * This allows script tag users to specify plugins by name without importing them.
7
+ *
8
+ * @param name - The name of the plugin to load
9
+ * @returns A promise that resolves to the plugin instance, or undefined if not found
10
+ */
11
+ export async function createPlugin(
12
+ name: BuiltInPluginName
13
+ ): Promise<Plugin | undefined> {
14
+ switch (name) {
15
+ case 'facebook-params':
16
+ return import(
17
+ /* webpackChunkName: "facebook-params" */ './facebook-params'
18
+ ).then((mod) => mod.facebookParams)
19
+ default:
20
+ return undefined
21
+ }
22
+ }
@@ -1,25 +1,40 @@
1
- /* eslint-disable @typescript-eslint/no-var-requires */
2
1
  /**
3
- * This is a classic destination, such as:
2
+ * Local stand-in for a classic analytics.js destination, such as:
4
3
  * https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/appcues/lib/index.js
5
4
  */
6
5
 
7
- const integration = require('@segment/analytics.js-integration')
6
+ import { ClassicIntegrationBuilder } from '../../plugins/ajs-destination/types'
8
7
 
9
8
  export const mockIntegrationName = 'Fake'
10
- export const Fake = integration(mockIntegrationName)
11
9
 
12
- Fake.prototype.initialize = function () {
10
+ function FakeIntegration(this: FakeIntegration, _options?: object) {
11
+ // classic integration constructor
12
+ }
13
+
14
+ interface FakeIntegration {
15
+ name: string
16
+ initialize: () => void
17
+ loaded: () => boolean
18
+ track: () => void
19
+ load: (callback: Function) => void
20
+ ready?: () => void
21
+ }
22
+
23
+ FakeIntegration.prototype.name = mockIntegrationName
24
+
25
+ FakeIntegration.prototype.initialize = function () {
13
26
  this.load(this.ready)
14
27
  }
15
28
 
16
- Fake.prototype.loaded = function () {
29
+ FakeIntegration.prototype.loaded = function () {
17
30
  return true
18
31
  }
19
32
 
20
- Fake.prototype.track = function () {}
33
+ FakeIntegration.prototype.track = function () {}
21
34
 
22
- Fake.prototype.load = function (callback: Function) {
35
+ FakeIntegration.prototype.load = function (callback: Function) {
23
36
  // this callback is important to actually initialize.
24
37
  callback()
25
38
  }
39
+
40
+ export const Fake = FakeIntegration as unknown as ClassicIntegrationBuilder
@@ -0,0 +1,32 @@
1
+ declare module 'meta-capi-param-builder-clientjs' {
2
+ // Facebook Parameter Builder SDK types (clientParamBuilder)
3
+ // Based on Meta's official API: https://developers.facebook.com/docs/marketing-api/conversions-api/parameter-builder-feature-library/client-side-onboarding
4
+ export interface ClientParamBuilder {
5
+ /**
6
+ * Processes and collects all parameters (fbc, fbp, client_ip_address).
7
+ * Must be called before getFbc(), getFbp(), or getClientIpAddress().
8
+ * @param url - Optional. The full URL to collect clickID from. If not provided, uses window.location.href
9
+ * @param getIpFn - Optional. Function to retrieve client IPv6 address (fallback to IPv4 if unavailable)
10
+ * @returns Promise that resolves to updated cookie object with _fbc, _fbp, and _fbi keys
11
+ */
12
+ processAndCollectAllParams(
13
+ url?: string,
14
+ getIpFn?: () => Promise<string>
15
+ ): Promise<{ _fbc?: string; _fbp?: string; _fbi?: string }>
16
+ /**
17
+ * Returns the fbc value from cookie. Returns empty string if cookie does not exist.
18
+ */
19
+ getFbc(): string
20
+ /**
21
+ * Returns the fbp value from cookie. Returns empty string if cookie does not exist.
22
+ */
23
+ getFbp(): string
24
+ /**
25
+ * Returns the client_ip_address value from cookie. Returns empty string if cookie does not exist.
26
+ */
27
+ getClientIpAddress(): string
28
+ }
29
+
30
+ const clientParamBuilder: ClientParamBuilder
31
+ export default clientParamBuilder
32
+ }