@segment/analytics-browser-actions-ms-bing-capi 1.0.1-staging-fb8bbedd8.0 → 1.0.1-staging-feeb4db0d.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 (40) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/index.d.ts +1 -1
  3. package/dist/cjs/index.js +12 -4
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/{msclickId → msclkidPlugin}/generated-types.d.ts +0 -1
  6. package/dist/cjs/msclkidPlugin/generated-types.js.map +1 -0
  7. package/dist/{esm/msclickId → cjs/msclkidPlugin}/index.d.ts +1 -1
  8. package/dist/cjs/msclkidPlugin/index.js +26 -0
  9. package/dist/cjs/msclkidPlugin/index.js.map +1 -0
  10. package/dist/cjs/utils.d.ts +8 -0
  11. package/dist/cjs/utils.js +28 -0
  12. package/dist/cjs/utils.js.map +1 -0
  13. package/dist/esm/index.d.ts +1 -1
  14. package/dist/esm/index.js +12 -4
  15. package/dist/esm/index.js.map +1 -1
  16. package/dist/esm/{msclickId → msclkidPlugin}/generated-types.d.ts +0 -1
  17. package/dist/esm/msclkidPlugin/generated-types.js.map +1 -0
  18. package/dist/{cjs/msclickId → esm/msclkidPlugin}/index.d.ts +1 -1
  19. package/dist/esm/msclkidPlugin/index.js +24 -0
  20. package/dist/esm/msclkidPlugin/index.js.map +1 -0
  21. package/dist/esm/utils.d.ts +8 -0
  22. package/dist/esm/utils.js +24 -0
  23. package/dist/esm/utils.js.map +1 -0
  24. package/dist/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +2 -2
  26. package/src/__tests__/index.test.ts +61 -0
  27. package/src/index.ts +16 -8
  28. package/src/msclkidPlugin/generated-types.ts +3 -0
  29. package/src/msclkidPlugin/index.ts +29 -0
  30. package/src/utils.ts +32 -0
  31. package/dist/cjs/msclickId/generated-types.js.map +0 -1
  32. package/dist/cjs/msclickId/index.js +0 -69
  33. package/dist/cjs/msclickId/index.js.map +0 -1
  34. package/dist/esm/msclickId/generated-types.js.map +0 -1
  35. package/dist/esm/msclickId/index.js +0 -67
  36. package/dist/esm/msclickId/index.js.map +0 -1
  37. package/src/msclickId/generated-types.ts +0 -8
  38. package/src/msclickId/index.ts +0 -88
  39. /package/dist/cjs/{msclickId → msclkidPlugin}/generated-types.js +0 -0
  40. /package/dist/esm/{msclickId → msclkidPlugin}/generated-types.js +0 -0
package/src/index.ts CHANGED
@@ -1,20 +1,28 @@
1
1
  import type { Settings } from './generated-types'
2
2
  import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
3
3
  import { browserDestination } from '@segment/browser-destination-runtime/shim'
4
-
5
- import msclickId from './msclickId'
4
+ import { storageClickIdKey, clickIdQuerystringName, storageFallback } from './utils'
5
+ import { UniversalStorage } from '@segment/analytics-next'
6
+ import msclkidPlugin from './msclkidPlugin'
6
7
 
7
8
  // Switch from unknown to the partner SDK client types
8
- export const destination: BrowserDestinationDefinition<Settings, unknown> = {
9
- name: 'Ms Bing Capi Plugin',
9
+ export const destination: BrowserDestinationDefinition<Settings, {}> = {
10
+ name: 'Microsoft Bing CAPI Browser Plugins',
10
11
  mode: 'device',
11
- initialize: async () => {
12
- // initialize client code here
12
+ initialize: async ({ analytics }) => {
13
+ const storage = (analytics.storage as UniversalStorage<Record<string, string>>) ?? storageFallback
14
+ const urlParams = new URLSearchParams(window.location.search)
15
+ const msclkid: string | null = urlParams.get(clickIdQuerystringName) || null
16
+
17
+ if (msclkid) {
18
+ storage.set(storageClickIdKey, msclkid)
19
+ }
20
+
13
21
  return {}
14
22
  },
15
-
23
+ settings: {},
16
24
  actions: {
17
- msclickId
25
+ msclkidPlugin
18
26
  }
19
27
  }
20
28
 
@@ -0,0 +1,3 @@
1
+ // Generated file. DO NOT MODIFY IT BY HAND.
2
+
3
+ export interface Payload {}
@@ -0,0 +1,29 @@
1
+ import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
2
+ import type { Settings } from '../generated-types'
3
+ import type { Payload } from './generated-types'
4
+ import { storageClickIdKey, clickIdIntegrationFieldName, storageFallback } from '../utils'
5
+ import { UniversalStorage } from '@segment/analytics-next'
6
+
7
+ const action: BrowserActionDefinition<Settings, {}, Payload> = {
8
+ title: 'Microsoft Bing msclkid Browser Plugin',
9
+ description: 'Enriches all Segment payloads with the Microsoft Bing msclkid Querystring value',
10
+ platform: 'web',
11
+ hidden: false,
12
+ defaultSubscription: 'type = "track" or type = "identify" or type = "page" or type = "group" or type = "alias"',
13
+ fields: {},
14
+ lifecycleHook: 'enrichment',
15
+ perform: (_, { context, analytics }) => {
16
+ const storage = (analytics.storage as UniversalStorage<Record<string, string>>) ?? storageFallback
17
+ const msclkid: string | null = storage.get(storageClickIdKey)
18
+ const integrationsData: Record<string, string> = {}
19
+ if (msclkid) {
20
+ integrationsData[clickIdIntegrationFieldName] = msclkid
21
+ if (context.event.integrations?.All !== false || context.event.integrations['Snap Conversions Api']) {
22
+ context.updateEvent(`integrations.Microsoft Bing CAPI`, integrationsData)
23
+ }
24
+ }
25
+ return
26
+ }
27
+ }
28
+
29
+ export default action
package/src/utils.ts ADDED
@@ -0,0 +1,32 @@
1
+ // The name of the storage location where we'll cache the MS Bing msclkid Querystring value
2
+ export const storageClickIdKey = 'analytics_ms_capi_click_id'
3
+
4
+ // The name of the MS Bing msclkid querystring to retrieve when the page loads
5
+ export const clickIdQuerystringName = 'msclkid'
6
+
7
+ // The field name to include for the Microsoft Bing CAPI msclkid Querystring in the 'context.integrations.Microsoft Bing CAPI' object
8
+ export const clickIdIntegrationFieldName = 'msclkid'
9
+
10
+ export const getCookieValue = (cookieName: string): string | null => {
11
+ const name = cookieName + '='
12
+ const decodedCookie = decodeURIComponent(document.cookie)
13
+ const cookieArray = decodedCookie.split('; ')
14
+
15
+ for (const cookie of cookieArray) {
16
+ if (cookie.startsWith(name)) {
17
+ return cookie.substring(name.length)
18
+ }
19
+ }
20
+
21
+ return null
22
+ }
23
+
24
+ export const storageFallback = {
25
+ get: (key: string) => {
26
+ const data = window.localStorage.getItem(key)
27
+ return data
28
+ },
29
+ set: (key: string, value: string) => {
30
+ return window.localStorage.setItem(key, value)
31
+ }
32
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"generated-types.js","sourceRoot":"","sources":["../../../src/msclickId/generated-types.ts"],"names":[],"mappings":""}
@@ -1,69 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const uuid_1 = require("@lukeed/uuid");
4
- function newMsclk() {
5
- return (0, uuid_1.v4)();
6
- }
7
- function now() {
8
- return new Date().getTime();
9
- }
10
- const NINETY_DAYS = 90 * 24 * 60 * 60 * 1000;
11
- function stale(id, updated, length = NINETY_DAYS) {
12
- if (id === null || updated === null) {
13
- return true;
14
- }
15
- const accessedAt = updated;
16
- if (now() - accessedAt >= length) {
17
- return true;
18
- }
19
- return false;
20
- }
21
- const action = {
22
- title: 'Msclick Id',
23
- description: 'Generates a MSCLK ID and attaches it to every Amplitude browser based event.',
24
- platform: 'web',
25
- fields: {
26
- sessionLength: {
27
- label: 'Session Length',
28
- type: 'number',
29
- description: 'The length of the session in days.',
30
- default: 90
31
- }
32
- },
33
- lifecycleHook: 'enrichment',
34
- perform: (_, { context, payload, analytics }) => {
35
- const storageFallback = {
36
- get: (key) => {
37
- const data = window.localStorage.getItem(key);
38
- return data === null ? null : data;
39
- },
40
- set: (key, value) => {
41
- return window.localStorage.setItem(key, value.toString());
42
- }
43
- };
44
- const storage = analytics.storage
45
- ?
46
- analytics.storage
47
- : storageFallback;
48
- const raw = storage.get('analytics_msclk_id');
49
- const updatedRaw = storage.get('analytics_msclk_id.last_access');
50
- const updated = updatedRaw !== null ? Number(updatedRaw) : null;
51
- let id = raw;
52
- const sessionLength = payload.sessionLength ? payload.sessionLength * 24 * 60 * 60 * 1000 : NINETY_DAYS;
53
- if (stale(raw, updated, sessionLength)) {
54
- id = newMsclk();
55
- storage.set('analytics_msclk_id', id);
56
- }
57
- else {
58
- storage.set('analytics_msclk_id', id);
59
- }
60
- storage.set('analytics_msclk_id.last_access', now().toString());
61
- if (context.event.integrations?.All !== false || context.event.integrations['MS Bing CAPI']) {
62
- context.updateEvent('integrations.MS Bing CAPI', {});
63
- context.updateEvent('integrations.MS Bing CAPI.msclk_id', id);
64
- }
65
- return;
66
- }
67
- };
68
- exports.default = action;
69
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/msclickId/index.ts"],"names":[],"mappings":";;AAKA,uCAA2C;AAE3C,SAAS,QAAQ;IACf,OAAO,IAAA,SAAM,GAAE,CAAA;AACjB,CAAC;AAED,SAAS,GAAG;IACV,OAAO,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAA;AAC7B,CAAC;AAED,MAAM,WAAW,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;AAE5C,SAAS,KAAK,CAAC,EAAiB,EAAE,OAAsB,EAAE,SAAiB,WAAW;IACpF,IAAI,EAAE,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACpC,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,UAAU,GAAG,OAAO,CAAA;IAE1B,IAAI,GAAG,EAAE,GAAG,UAAU,IAAI,MAAM,EAAE,CAAC;QACjC,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAGD,MAAM,MAAM,GAAwD;IAClE,KAAK,EAAE,YAAY;IACnB,WAAW,EAAE,8EAA8E;IAC3F,QAAQ,EAAE,KAAK;IACf,MAAM,EAAE;QACN,aAAa,EAAE;YACb,KAAK,EAAE,gBAAgB;YACvB,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,oCAAoC;YACjD,OAAO,EAAE,EAAE;SACZ;KACF;IACD,aAAa,EAAE,YAAY;IAC3B,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,OAAO,EAAE,O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AAG,CAAC,oBAAoB,EAAE,EAAG,CAAC,CAAA;QACxC,CAAC;QAED,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAE,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAA;QAE/D,IAAI,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,KAAK,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5F,OAAO,CAAC,WAAW,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAA;YACpD,OAAO,CAAC,WAAW,CAAC,oCAAoC,EAAE,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,OAAM;IACR,CAAC;CACF,CAAA;AAED,kBAAe,MAAM,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"generated-types.js","sourceRoot":"","sources":["../../../src/msclickId/generated-types.ts"],"names":[],"mappings":""}
@@ -1,67 +0,0 @@
1
- import { v4 as uuidv4 } from '@lukeed/uuid';
2
- function newMsclk() {
3
- return uuidv4();
4
- }
5
- function now() {
6
- return new Date().getTime();
7
- }
8
- const NINETY_DAYS = 90 * 24 * 60 * 60 * 1000;
9
- function stale(id, updated, length = NINETY_DAYS) {
10
- if (id === null || updated === null) {
11
- return true;
12
- }
13
- const accessedAt = updated;
14
- if (now() - accessedAt >= length) {
15
- return true;
16
- }
17
- return false;
18
- }
19
- const action = {
20
- title: 'Msclick Id',
21
- description: 'Generates a MSCLK ID and attaches it to every Amplitude browser based event.',
22
- platform: 'web',
23
- fields: {
24
- sessionLength: {
25
- label: 'Session Length',
26
- type: 'number',
27
- description: 'The length of the session in days.',
28
- default: 90
29
- }
30
- },
31
- lifecycleHook: 'enrichment',
32
- perform: (_, { context, payload, analytics }) => {
33
- const storageFallback = {
34
- get: (key) => {
35
- const data = window.localStorage.getItem(key);
36
- return data === null ? null : data;
37
- },
38
- set: (key, value) => {
39
- return window.localStorage.setItem(key, value.toString());
40
- }
41
- };
42
- const storage = analytics.storage
43
- ?
44
- analytics.storage
45
- : storageFallback;
46
- const raw = storage.get('analytics_msclk_id');
47
- const updatedRaw = storage.get('analytics_msclk_id.last_access');
48
- const updated = updatedRaw !== null ? Number(updatedRaw) : null;
49
- let id = raw;
50
- const sessionLength = payload.sessionLength ? payload.sessionLength * 24 * 60 * 60 * 1000 : NINETY_DAYS;
51
- if (stale(raw, updated, sessionLength)) {
52
- id = newMsclk();
53
- storage.set('analytics_msclk_id', id);
54
- }
55
- else {
56
- storage.set('analytics_msclk_id', id);
57
- }
58
- storage.set('analytics_msclk_id.last_access', now().toString());
59
- if (context.event.integrations?.All !== false || context.event.integrations['MS Bing CAPI']) {
60
- context.updateEvent('integrations.MS Bing CAPI', {});
61
- context.updateEvent('integrations.MS Bing CAPI.msclk_id', id);
62
- }
63
- return;
64
- }
65
- };
66
- export default action;
67
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/msclickId/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,cAAc,CAAA;AAE3C,SAAS,QAAQ;IACf,OAAO,MAAM,EAAE,CAAA;AACjB,CAAC;AAED,SAAS,GAAG;IACV,OAAO,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAA;AAC7B,CAAC;AAED,MAAM,WAAW,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;AAE5C,SAAS,KAAK,CAAC,EAAiB,EAAE,OAAsB,EAAE,SAAiB,WAAW;IACpF,IAAI,EAAE,KAAK,IAAI,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACpC,OAAO,IAAI,CAAA;IACb,CAAC;IAED,MAAM,UAAU,GAAG,OAAO,CAAA;IAE1B,IAAI,GAAG,EAAE,GAAG,UAAU,IAAI,MAAM,EAAE,CAAC;QACjC,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO,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G,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,WAAW,CAAA;QACvG,IAAI,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,aAAa,CAAC,EAAE,CAAC;YACvC,EAAE,GAAG,QAAQ,EAAE,CAAA;YACf,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAA;QACvC,CAAC;aAAM,CAAC;YAGN,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,EAAG,CAAC,CAAA;QACxC,CAAC;QAED,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAE,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAA;QAE/D,IAAI,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,KAAK,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5F,OAAO,CAAC,WAAW,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAA;YACpD,OAAO,CAAC,WAAW,CAAC,oCAAoC,EAAE,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,OAAM;IACR,CAAC;CACF,CAAA;AAED,eAAe,MAAM,CAAA"}
@@ -1,8 +0,0 @@
1
- // Generated file. DO NOT MODIFY IT BY HAND.
2
-
3
- export interface Payload {
4
- /**
5
- * The length of the session in days.
6
- */
7
- sessionLength?: number
8
- }
@@ -1,88 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-call */
2
- import { UniversalStorage } from '@segment/analytics-next'
3
- import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
4
- import type { Settings } from '../generated-types'
5
- import type { Payload } from './generated-types'
6
- import { v4 as uuidv4 } from '@lukeed/uuid'
7
-
8
- function newMsclk(): string {
9
- return uuidv4()
10
- }
11
-
12
- function now(): number {
13
- return new Date().getTime()
14
- }
15
-
16
- const NINETY_DAYS = 90 * 24 * 60 * 60 * 1000
17
-
18
- function stale(id: string | null, updated: number | null, length: number = NINETY_DAYS): id is null {
19
- if (id === null || updated === null) {
20
- return true
21
- }
22
-
23
- const accessedAt = updated
24
-
25
- if (now() - accessedAt >= length) {
26
- return true
27
- }
28
-
29
- return false
30
- }
31
-
32
- // Change from unknown to the partner SDK types
33
- const action: BrowserActionDefinition<Settings, unknown, Payload> = {
34
- title: 'Msclick Id',
35
- description: 'Generates a MSCLK ID and attaches it to every Amplitude browser based event.',
36
- platform: 'web',
37
- fields: {
38
- sessionLength: {
39
- label: 'Session Length',
40
- type: 'number',
41
- description: 'The length of the session in days.',
42
- default: 90
43
- }
44
- },
45
- lifecycleHook: 'enrichment',
46
- perform: (_, { context, payload, analytics }) => {
47
- const storageFallback = {
48
- get: (key: string) => {
49
- const data = window.localStorage.getItem(key)
50
- return data === null ? null : data
51
- },
52
- set: (key: string, value: string | number) => {
53
- return window.localStorage.setItem(key, value.toString())
54
- }
55
- }
56
-
57
- const storage = analytics.storage
58
- ? // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
59
- (analytics.storage as UniversalStorage<Record<string, string>>)
60
- : storageFallback
61
-
62
- const raw = storage.get('analytics_msclk_id')
63
- const updatedRaw = storage.get('analytics_msclk_id.last_access')
64
- const updated = updatedRaw !== null ? Number(updatedRaw) : null
65
-
66
- let id: string | null = raw
67
- const sessionLength = payload.sessionLength ? payload.sessionLength * 24 * 60 * 60 * 1000 : NINETY_DAYS
68
- if (stale(raw, updated, sessionLength)) {
69
- id = newMsclk()
70
- storage.set('analytics_msclk_id', id)
71
- } else {
72
- // we are storing the session id regardless, so it gets synced between different storage mediums
73
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- id can't be null because of stale check
74
- storage.set('analytics_msclk_id', id!)
75
- }
76
-
77
- storage.set('analytics_msclk_id.last_access', now().toString())
78
-
79
- if (context.event.integrations?.All !== false || context.event.integrations['MS Bing CAPI']) {
80
- context.updateEvent('integrations.MS Bing CAPI', {})
81
- context.updateEvent('integrations.MS Bing CAPI.msclk_id', id)
82
- }
83
-
84
- return
85
- }
86
- }
87
-
88
- export default action