@chordcommerce/analytics 1.16.1 → 1.17.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.
@@ -1,4 +1,5 @@
1
1
  import type { AnyAnalytics, EventProperties, Options, ValidateResult, ChordAnalyticsOptions, AnyOptions, CartViewedInput, IdentifyTraits, CheckoutStartedInput, CheckoutStepCompletedInput, CheckoutStepViewedInput, EmailCapturedInput, ProductListViewedInput, ProductListFilteredInput, ProductAddedInput, ProductClickedInput, VariantClickedInput, ProductRemovedInput, ProductViewedInput, ProductsSearchedInput, CouponAppliedInput, CouponDeniedInput, CouponEnteredInput, CouponRemovedInput, SignedInInput, SignedOutInput, SignedUpInput, LoginStartedInput, ObjectTypes, SubscriptionCancelledInput, NavigationClickedInput, PaymentInfoEnteredInput } from '../types/index.js';
2
+ import { AnalyticsMiddleware } from '../types/client.js';
2
3
  export interface ChordAnalyticsSnippet {
3
4
  _loadOptions: ChordAnalyticsOptions<ObjectTypes>;
4
5
  invoked: boolean;
@@ -21,6 +22,10 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
21
22
  * Options for ChordAnalytics.
22
23
  */
23
24
  options: ChordAnalyticsOptions<T>;
25
+ /**
26
+ * Middleware to be applied to the event properties
27
+ */
28
+ middleware: any[];
24
29
  /**
25
30
  * Promise that resolves when CDP is ready
26
31
  */
@@ -60,6 +65,7 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
60
65
  platform: import("../types/client.js").platformMetadata;
61
66
  store: import("../types/client.js").storeMetadata;
62
67
  };
68
+ addSourceMiddleware: (func: AnalyticsMiddleware) => void;
63
69
  /**
64
70
  * Send a `track` event to the CDP with any event name and properties.
65
71
  */
@@ -22,6 +22,10 @@ export class ChordAnalytics {
22
22
  * Allows snippet.js to detect whether the snippet has started running yet.
23
23
  */
24
24
  this.invoked = true;
25
+ /**
26
+ * Middleware to be applied to the event properties
27
+ */
28
+ this.middleware = [];
25
29
  /**
26
30
  * Promise that resolves when CDP is ready
27
31
  */
@@ -93,6 +97,10 @@ export class ChordAnalytics {
93
97
  patch: 0,
94
98
  } });
95
99
  };
100
+ // eslint-disable-next-line class-methods-use-this
101
+ this.addSourceMiddleware = (func) => {
102
+ this.middleware.push(func);
103
+ };
96
104
  /**
97
105
  * Send a `track` event to the CDP with any event name and properties.
98
106
  */
@@ -104,7 +112,39 @@ export class ChordAnalytics {
104
112
  yield this.cdpReadyPromise;
105
113
  }
106
114
  const formattedProps = this.options.stripNull && props ? pruneNullValues(props) : props;
107
- const finalEventProps = Object.assign(Object.assign({}, formattedProps), { meta: this.meta() });
115
+ let middlewareProps = {};
116
+ if (this.middleware.length > 0) {
117
+ const searchParams = typeof window !== 'undefined' ? window.location.search : '';
118
+ const urlSearchParams = new URLSearchParams(searchParams);
119
+ const searchData = Object.fromEntries(urlSearchParams.entries());
120
+ // process middleware synchronously
121
+ this.middleware.forEach((func) => {
122
+ try {
123
+ const result = func({
124
+ payload: {
125
+ obj: {
126
+ properties: props,
127
+ event,
128
+ context: {
129
+ page: {
130
+ search: searchParams,
131
+ searchParams: searchData,
132
+ },
133
+ },
134
+ },
135
+ },
136
+ next: (_) => _,
137
+ });
138
+ if (result && typeof result === 'object') {
139
+ middlewareProps = Object.assign(Object.assign({}, middlewareProps), result);
140
+ }
141
+ }
142
+ catch (error) {
143
+ console.warn('Middleware error:', error);
144
+ }
145
+ });
146
+ }
147
+ const finalEventProps = Object.assign(Object.assign(Object.assign({}, formattedProps), middlewareProps), { meta: this.meta() });
108
148
  if (this.options.debug) {
109
149
  this.validate(event, finalEventProps).forEach((valid) => {
110
150
  if (!valid.success) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import { jest } from '@jest/globals';
3
+ // Make jest available globally
4
+ globalThis.jest = jest;
@@ -1,4 +1,4 @@
1
- import type { AnalyticsSnippet, Analytics, AnalyticsBrowser, Options } from '@segment/analytics-next';
1
+ import type { EventProperties, AnalyticsSnippet, Analytics, AnalyticsBrowser, Options } from '@segment/analytics-next';
2
2
  import { ChordAnalyticsFormatters } from './formatters.js';
3
3
  export interface ValidateResult {
4
4
  success: boolean;
@@ -39,6 +39,7 @@ export interface ChordAnalyticsOptions<T extends ObjectTypes = ObjectTypes> {
39
39
  stripNull?: boolean;
40
40
  metadata: EventMetadata;
41
41
  awaitConsentTimeout?: number;
42
+ middleware?: Array<() => void>;
42
43
  }
43
44
  export interface EventMetadata {
44
45
  i18n: i18nMetadata;
@@ -74,3 +75,22 @@ export type ChordAnalyticsQueue = {
74
75
  page: (...args: any[]) => void;
75
76
  reset: (...args: any[]) => void;
76
77
  };
78
+ type PageContext = {
79
+ search: string;
80
+ searchParams: Record<string, string>;
81
+ };
82
+ type MiddlewarePayload = {
83
+ obj: {
84
+ properties?: EventProperties;
85
+ event: string;
86
+ context: {
87
+ page: PageContext;
88
+ };
89
+ };
90
+ };
91
+ type MiddlewareArgs = {
92
+ payload: MiddlewarePayload;
93
+ next: <T>(arg: T) => T;
94
+ };
95
+ export type AnalyticsMiddleware = (args: MiddlewareArgs) => Partial<EventProperties> | void;
96
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chordcommerce/analytics",
3
3
  "description": "Chord Commerce event tracking",
4
- "version": "1.16.1",
4
+ "version": "1.17.0",
5
5
  "author": "Chord Commerce <help@chord.co> (https://www.chord.co)",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
@@ -18,6 +18,7 @@
18
18
  "clean": "rimraf dist",
19
19
  "test": "jest",
20
20
  "test:watch": "jest --watch",
21
+ "test:file": "NODE_OPTIONS=--experimental-vm-modules jest",
21
22
  "test:ci": "yarn test --ci --runInBand --reporters=default --passWithNoTests",
22
23
  "zod": "yarn ts-to-zod src/types/typewriter.ts src/validators/tracking-plan.ts && yarn ts-to-zod src/types/objects.ts src/validators/objects.ts"
23
24
  },