@usesophi/sophi-event-react-native 0.0.0-stage → 0.0.1-alpha01

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 (74) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +116 -2
  3. package/SophiEventSDK-ReactNative.podspec +23 -0
  4. package/android/build.gradle +39 -0
  5. package/android/consumer-rules.pro +1 -0
  6. package/android/src/main/AndroidManifest.xml +3 -0
  7. package/android/src/main/java/com/sophi/eventreactnative/SophiEventsModule.kt +199 -0
  8. package/android/src/main/java/com/sophi/eventreactnative/SophiEventsPackage.kt +11 -0
  9. package/index.ts +4 -0
  10. package/ios/Frameworks/SophiEventSDK.xcframework/Info.plist +44 -0
  11. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Headers/SophiEventSDK-Swift.h +382 -0
  12. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Info.plist +0 -0
  13. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.abi.json +21707 -0
  14. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +413 -0
  15. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  16. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.swiftinterface +413 -0
  17. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/module.modulemap +3 -0
  18. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/PrivacyInfo.xcprivacy +153 -0
  19. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/SophiEventSDK +0 -0
  20. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Headers/SophiEventSDK-Swift.h +760 -0
  21. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Info.plist +0 -0
  22. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +21707 -0
  23. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +413 -0
  24. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  25. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +413 -0
  26. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +21707 -0
  27. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +413 -0
  28. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  29. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +413 -0
  30. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/module.modulemap +3 -0
  31. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/PrivacyInfo.xcprivacy +153 -0
  32. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/SophiEventSDK +0 -0
  33. package/ios/Frameworks/SophiIdentity.xcframework/Info.plist +44 -0
  34. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Headers/SophiIdentity-Swift.h +382 -0
  35. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Info.plist +0 -0
  36. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.abi.json +6314 -0
  37. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.private.swiftinterface +170 -0
  38. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  39. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.swiftinterface +170 -0
  40. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/module.modulemap +3 -0
  41. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/PrivacyInfo.xcprivacy +50 -0
  42. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/SophiIdentity +0 -0
  43. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Headers/SophiIdentity-Swift.h +760 -0
  44. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Info.plist +0 -0
  45. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.abi.json +6314 -0
  46. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +170 -0
  47. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  48. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.swiftinterface +170 -0
  49. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.abi.json +6314 -0
  50. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +170 -0
  51. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  52. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +170 -0
  53. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/module.modulemap +3 -0
  54. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/PrivacyInfo.xcprivacy +50 -0
  55. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/SophiIdentity +0 -0
  56. package/ios/SophiEventsModule.m +54 -0
  57. package/ios/SophiEventsModuleImpl.swift +178 -0
  58. package/lib/index.d.ts +4 -0
  59. package/lib/index.js +10 -0
  60. package/lib/src/SophiEvents.d.ts +3 -0
  61. package/lib/src/SophiEvents.js +11 -0
  62. package/lib/src/client.d.ts +60 -0
  63. package/lib/src/client.js +151 -0
  64. package/lib/src/types.d.ts +204 -0
  65. package/lib/src/types.js +3 -0
  66. package/lib/src/wire.d.ts +10 -0
  67. package/lib/src/wire.js +191 -0
  68. package/native-versions.json +16 -0
  69. package/package.json +62 -4
  70. package/react-native.config.js +10 -0
  71. package/src/SophiEvents.ts +13 -0
  72. package/src/client.ts +191 -0
  73. package/src/types.ts +216 -0
  74. package/src/wire.ts +189 -0
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@usesophi/sophi-event-react-native",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1-alpha01",
4
+ "description": "Sophi storefront event SDK for React Native",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "react-native": "index.ts",
8
+ "files": [
9
+ "lib",
10
+ "src",
11
+ "index.ts",
12
+ "ios/*.swift",
13
+ "ios/*.m",
14
+ "ios/*.h",
15
+ "ios/Frameworks/SophiIdentity.xcframework",
16
+ "ios/Frameworks/SophiEventSDK.xcframework",
17
+ "android/build.gradle",
18
+ "android/consumer-rules.pro",
19
+ "android/src/main",
20
+ "SophiEventSDK-ReactNative.podspec",
21
+ "README.md",
22
+ "react-native.config.js",
23
+ "native-versions.json",
24
+ "LICENSE"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsc -p tsconfig.json",
28
+ "prepare": "npm run build",
29
+ "typecheck": "tsc --noEmit -p tsconfig.json",
30
+ "lint": "eslint .",
31
+ "test": "npm run build && node --test tests/*.test.cjs",
32
+ "native:fetch": "node scripts/fetch-native.cjs",
33
+ "check:package": "node scripts/check-package.cjs",
34
+ "prepack": "npm run build && node scripts/fetch-native.cjs",
35
+ "example:prepare": "node scripts/prepare-example.cjs",
36
+ "release:local": "node scripts/release.cjs --local"
37
+ },
38
+ "peerDependencies": {
39
+ "react": ">=19.0.0 <20",
40
+ "react-native": "^0.81.6 || ^0.82.1 || ^0.83.1 || ^0.84.1"
41
+ },
42
+ "devDependencies": {
43
+ "@eslint/js": "^9.39.5",
44
+ "@types/react": "^19.2.0",
45
+ "eslint": "^9.39.5",
46
+ "globals": "^17.14.0",
47
+ "react": "19.2.3",
48
+ "react-native": "0.84.1",
49
+ "typescript": "^5.9.0",
50
+ "typescript-eslint": "^8.73.0"
51
+ },
52
+ "engines": {
53
+ "node": ">=22.11"
54
+ },
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/Let-Me-Shop-For-You/SophiEventSDK-ReactNative.git"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public"
61
+ },
62
+ "license": "UNLICENSED",
63
+ "author": "Sophi"
64
+ }
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ dependency: {
3
+ platforms: {
4
+ android: {
5
+ packageImportPath: 'import com.sophi.eventreactnative.SophiEventsPackage;',
6
+ packageInstance: 'new SophiEventsPackage()',
7
+ },
8
+ },
9
+ },
10
+ };
@@ -0,0 +1,13 @@
1
+ import {NativeEventEmitter, NativeModules} from 'react-native';
2
+ import {createEventsClient} from './client';
3
+
4
+ const native = NativeModules.SophiEventsModule;
5
+ if (!native) {
6
+ throw new Error(
7
+ 'Sophi event native module is not linked. Rebuild the native application after installing @usesophi/sophi-event-react-native.',
8
+ );
9
+ }
10
+ const emitter = new NativeEventEmitter(native);
11
+
12
+ /** The app-wide event SDK. Configure once at startup, then report merchant actions. */
13
+ export default createEventsClient(native, listener => emitter.addListener('SophiEventDiagnostic', listener));
package/src/client.ts ADDED
@@ -0,0 +1,191 @@
1
+ import {toWire} from './wire';
2
+ import type {
3
+ BasketObject, EventConfiguration, EventDiagnostic, EventHandlers, EventReceipt, FlushResult,
4
+ JsonObject, ListingObject, PageObject, ProductObject, SophiEvent, TransactionObject, UserObject,
5
+ } from './types';
6
+
7
+ /** The single error type of the API. `code` is the same on iOS and Android. */
8
+ export class SophiEventError extends Error {
9
+ readonly code: string;
10
+ /** HTTP status for `http`. */
11
+ readonly status?: number;
12
+ /** Invalid wire paths for `validation`, e.g. `basket.line_items[0].quantity`. */
13
+ readonly fields?: string[];
14
+ constructor(code: string, message: string, details: {status?: number; fields?: string[]} = {}) {
15
+ super(message);
16
+ this.name = 'SophiEventError';
17
+ this.code = code;
18
+ if (details.status !== undefined) this.status = details.status;
19
+ if (details.fields !== undefined) this.fields = details.fields;
20
+ }
21
+ }
22
+
23
+ /** What the native module provides on both platforms. */
24
+ export interface NativeEvents {
25
+ configure(options: Record<string, unknown>): Promise<void>;
26
+ setAnalyticsEnabled(enabled: boolean): Promise<void>;
27
+ updateUser(externalUserId: string | null): Promise<void>;
28
+ logout(): Promise<void>;
29
+ track(eventJson: string): Promise<{id: string | null; status: string}>;
30
+ flush(): Promise<FlushResult>;
31
+ pendingCount(): Promise<number>;
32
+ }
33
+
34
+ type Subscribe = (listener: (diagnostic: EventDiagnostic) => void) => {remove(): void};
35
+
36
+ const STATUSES = ['queued', 'disabled', 'deliveredMemoryOnly'] as const;
37
+
38
+ function normalize(error: any): SophiEventError {
39
+ if (error instanceof SophiEventError) return error;
40
+ const info = error?.userInfo ?? {};
41
+ const code = typeof error?.code === 'string' && error.code ? error.code : 'internal_error';
42
+ return new SophiEventError(code, error?.message ?? 'The native event SDK failed', {
43
+ status: typeof info.status === 'number' ? info.status : undefined,
44
+ fields: Array.isArray(info.fields) ? info.fields.map(String) : undefined,
45
+ });
46
+ }
47
+
48
+ async function call<T>(operation: () => Promise<T>): Promise<T> {
49
+ try {
50
+ return await operation();
51
+ } catch (error) {
52
+ throw normalize(error);
53
+ }
54
+ }
55
+
56
+ function invalid(message: string): never {
57
+ throw new SophiEventError('invalid_configuration', message);
58
+ }
59
+
60
+ function configurationSnapshot(config: EventConfiguration): Record<string, unknown> {
61
+ if (!config || typeof config !== 'object') invalid('A configuration object is required');
62
+ if (typeof config.apiKey !== 'string' || !config.apiKey.trim()) invalid('apiKey is required');
63
+ if (config.baseUrl !== undefined && (typeof config.baseUrl !== 'string' || !/^https:\/\/[^/?#\s]+\/?$/i.test(config.baseUrl)))
64
+ invalid('baseUrl must be an HTTPS origin without a path, query or fragment');
65
+ const consent = config.analyticsEnabled;
66
+ if (consent !== undefined && consent !== null && typeof consent !== 'boolean')
67
+ invalid('analyticsEnabled must be true, false or left out');
68
+ const user = config.externalUserId;
69
+ if (user !== undefined && user !== null && typeof user !== 'string') invalid('externalUserId must be a string');
70
+ const limits: Record<string, number> = {};
71
+ for (const [key, value] of Object.entries(config.queueLimits ?? {})) {
72
+ if (!['maxAgeMs', 'maxEvents', 'maxBytes', 'maxEventBytes'].includes(key)) invalid(`Unknown queue limit ${key}`);
73
+ if (value === undefined) continue;
74
+ if (!Number.isSafeInteger(value) || value <= 0) invalid(`${key} must be a positive integer`);
75
+ limits[key] = value;
76
+ }
77
+ return {
78
+ apiKey: config.apiKey,
79
+ baseUrl: config.baseUrl ?? null,
80
+ // The native bridges read a missing value as "not known yet".
81
+ analyticsEnabled: typeof consent === 'boolean' ? consent : null,
82
+ externalUserId: user ?? null,
83
+ queueLimits: limits,
84
+ };
85
+ }
86
+
87
+ export interface SophiEventsClient {
88
+ /** Configure once at startup and await it before tracking. Calling it again replaces the setup. */
89
+ configure(config: EventConfiguration, handlers?: EventHandlers): Promise<void>;
90
+ /** Report the consent answer when it arrives. `false` deletes the stored queue. */
91
+ setAnalyticsEnabled(enabled: boolean): Promise<void>;
92
+ /** The merchant customer ID after sign-in, or `null` for a guest. */
93
+ updateUser(externalUserId: string | null): Promise<void>;
94
+ /** Sign-out: forgets the shopper and deletes its pending events. */
95
+ logout(): Promise<void>;
96
+ /** Accepts one occurrence. A queued receipt means stored on the device; delivery may follow later. */
97
+ track(event: SophiEvent): Promise<EventReceipt>;
98
+ pageView(page: PageObject): Promise<EventReceipt>;
99
+ productView(page: PageObject, product: ProductObject): Promise<EventReceipt>;
100
+ listingView(page: PageObject, listing: ListingObject): Promise<EventReceipt>;
101
+ basketView(page: PageObject, basket: BasketObject): Promise<EventReceipt>;
102
+ /** One call per added unit, each with the resulting complete basket. */
103
+ addToCart(page: PageObject, product: ProductObject, basket: BasketObject): Promise<EventReceipt>;
104
+ removeFromCart(page: PageObject, product: ProductObject, basket: BasketObject): Promise<EventReceipt>;
105
+ checkoutStarted(page: PageObject, basket: BasketObject): Promise<EventReceipt>;
106
+ /** Once per confirmed order. */
107
+ purchase(page: PageObject, transaction: TransactionObject): Promise<EventReceipt>;
108
+ /** Sent from memory only; requires granted analytics. */
109
+ identify(page: PageObject, user: UserObject): Promise<EventReceipt>;
110
+ custom(page: PageObject, properties: JsonObject): Promise<EventReceipt>;
111
+ /** Sends what is due now. Queued events are also sent automatically. */
112
+ flush(): Promise<FlushResult>;
113
+ /** Events waiting on this device for the current shopper. */
114
+ pendingCount(): Promise<number>;
115
+ }
116
+
117
+ export function createEventsClient(native: NativeEvents, subscribe: Subscribe): SophiEventsClient {
118
+ let subscription: {remove(): void} | undefined;
119
+ let handlers: EventHandlers = {};
120
+
121
+ const listen = () => {
122
+ if (subscription) return;
123
+ subscription = subscribe(diagnostic => {
124
+ try {
125
+ handlers.onDiagnostic?.({
126
+ code: String(diagnostic.code),
127
+ eventId: diagnostic.eventId ? String(diagnostic.eventId).toLowerCase() : null,
128
+ count: Number(diagnostic.count) || 0,
129
+ });
130
+ } catch {
131
+ // A host logging callback must never break event delivery.
132
+ }
133
+ });
134
+ };
135
+
136
+ const track = async (event: SophiEvent): Promise<EventReceipt> => {
137
+ const wire = toWire(event);
138
+ if ('fields' in wire)
139
+ throw new SophiEventError('validation', `Invalid event fields: ${wire.fields.join(', ')}`, {fields: wire.fields});
140
+ const receipt = await call(() => native.track(wire.json));
141
+ const status = (STATUSES as readonly string[]).includes(receipt?.status) ? receipt.status : 'queued';
142
+ return {id: receipt?.id ? String(receipt.id).toLowerCase() : null, status: status as EventReceipt['status']};
143
+ };
144
+
145
+ return {
146
+ async configure(config, next = {}) {
147
+ const snapshot = configurationSnapshot(config);
148
+ if (next.onDiagnostic !== undefined && typeof next.onDiagnostic !== 'function')
149
+ invalid('onDiagnostic must be a function');
150
+ handlers = {...next};
151
+ listen();
152
+ await call(() => native.configure(snapshot));
153
+ },
154
+ async setAnalyticsEnabled(enabled) {
155
+ if (typeof enabled !== 'boolean') invalid('setAnalyticsEnabled takes true or false');
156
+ await call(() => native.setAnalyticsEnabled(enabled));
157
+ },
158
+ async updateUser(externalUserId) {
159
+ if (externalUserId !== null && (typeof externalUserId !== 'string' || !externalUserId.trim()))
160
+ invalid('updateUser takes a nonempty customer ID or null');
161
+ await call(() => native.updateUser(externalUserId));
162
+ },
163
+ async logout() {
164
+ await call(() => native.logout());
165
+ },
166
+ track,
167
+ pageView: page => track({type: 'page_view', page}),
168
+ productView: (page, product) => track({type: 'product_view', page, product}),
169
+ listingView: (page, listing) => track({type: 'listing_view', page, listing}),
170
+ basketView: (page, basket) => track({type: 'basket_view', page, basket}),
171
+ addToCart: (page, product, basket) => track({type: 'add_to_cart', page, product, basket}),
172
+ removeFromCart: (page, product, basket) => track({type: 'remove_from_cart', page, product, basket}),
173
+ checkoutStarted: (page, basket) => track({type: 'checkout_started', page, basket}),
174
+ purchase: (page, transaction) => track({type: 'purchase', page, transaction}),
175
+ identify: (page, user) => track({type: 'identify', page, user}),
176
+ custom: (page, properties) => track({type: 'custom', page, custom: properties}),
177
+ async flush() {
178
+ const result = await call(() => native.flush());
179
+ return {
180
+ delivered: Number(result?.delivered) || 0,
181
+ retried: Number(result?.retried) || 0,
182
+ discarded: Number(result?.discarded) || 0,
183
+ pending: Number(result?.pending) || 0,
184
+ pausedReason: result?.pausedReason ?? null,
185
+ };
186
+ },
187
+ async pendingCount() {
188
+ return Number(await call(() => native.pendingCount())) || 0;
189
+ },
190
+ };
191
+ }
package/src/types.ts ADDED
@@ -0,0 +1,216 @@
1
+ /** Public types of the Sophi React Native event SDK. Field names match EVENT-REFERENCE.md. */
2
+
3
+ /** JSON value for `custom` and `appliedFilters`: no functions, dates, undefined or non-finite numbers. */
4
+ export type JsonValue = string | number | boolean | null | JsonValue[] | {[key: string]: JsonValue};
5
+ export type JsonObject = {[key: string]: JsonValue};
6
+
7
+ export type PageType =
8
+ | 'Home' | 'Category' | 'Product' | 'Basket' | 'Checkout' | 'Confirmation' | 'Search' | 'Account' | 'Custom';
9
+
10
+ export type EventType =
11
+ | 'page_view' | 'product_view' | 'listing_view' | 'basket_view' | 'add_to_cart'
12
+ | 'remove_from_cart' | 'checkout_started' | 'purchase' | 'identify' | 'custom';
13
+
14
+ export interface PageObject {
15
+ type: PageType;
16
+ /** A native deep link such as `merchant://products/42` is valid. */
17
+ url: string;
18
+ title?: string;
19
+ language?: string;
20
+ categoryId?: string;
21
+ searchQuery?: string;
22
+ custom?: JsonObject;
23
+ }
24
+
25
+ export interface ConsentObject {
26
+ gdprOptin: boolean;
27
+ version: string;
28
+ /** Epoch seconds. */
29
+ timestamp: number;
30
+ }
31
+ export interface IdentityObject { uuid?: string; email?: string; phoneNumber?: string }
32
+ export interface ProfileObject {
33
+ name?: string;
34
+ surname?: string;
35
+ /** `F`, `M`, `O` or `U`. */
36
+ gender?: string;
37
+ birthday?: string;
38
+ language?: string;
39
+ }
40
+ export interface BehaviorObject {
41
+ returning?: boolean;
42
+ hasTransacted?: boolean;
43
+ transactionCount?: number;
44
+ /** Epoch seconds. */
45
+ lastTransactionAt?: number;
46
+ }
47
+ export interface SegmentationObject { staticSegmentIds?: Array<string | number> }
48
+ export interface UserObject {
49
+ consent: ConsentObject;
50
+ identity?: IdentityObject;
51
+ profile?: ProfileObject;
52
+ behavior?: BehaviorObject;
53
+ segmentation?: SegmentationObject;
54
+ custom?: JsonObject;
55
+ }
56
+
57
+ export interface ProductObject {
58
+ id: string;
59
+ categoryIds: string[];
60
+ name: string;
61
+ url: string;
62
+ productImageUrl: string;
63
+ taxonomy: string[];
64
+ /** Uppercase three-letter code. */
65
+ currency: string;
66
+ /** Major currency units, not minor-unit integers. */
67
+ unitPrice: number;
68
+ unitSalePrice: number;
69
+ /** 0 or 1. */
70
+ inStock: number;
71
+ variantId?: string;
72
+ sku?: string;
73
+ groupCode?: string;
74
+ omnibusPrice?: number;
75
+ stock?: number;
76
+ brand?: string;
77
+ color?: string;
78
+ size?: string;
79
+ gender?: string;
80
+ locale?: string;
81
+ description?: string;
82
+ tags?: string[];
83
+ custom?: JsonObject;
84
+ }
85
+
86
+ export interface LineItem {
87
+ productId: string;
88
+ quantity: number;
89
+ currency: string;
90
+ unitPrice: number;
91
+ unitSalePrice: number;
92
+ lineTotal: number;
93
+ variantId?: string;
94
+ sku?: string;
95
+ }
96
+
97
+ export interface BasketObject {
98
+ currency: string;
99
+ total: number;
100
+ lineItems: LineItem[];
101
+ basketId?: string;
102
+ subtotal?: number;
103
+ tax?: number;
104
+ shippingCost?: number;
105
+ discountTotal?: number;
106
+ couponCode?: string;
107
+ }
108
+
109
+ export interface ListingItem {
110
+ productId: string;
111
+ position: number;
112
+ name: string;
113
+ url: string;
114
+ productImageUrl: string;
115
+ currency: string;
116
+ unitPrice: number;
117
+ unitSalePrice: number;
118
+ variantId?: string;
119
+ inStock?: number;
120
+ brand?: string;
121
+ }
122
+
123
+ export interface ListingObject {
124
+ categoryId?: string;
125
+ slug?: string;
126
+ searchQuery?: string;
127
+ name?: string;
128
+ taxonomy?: string[];
129
+ totalResults?: number;
130
+ pageNumber?: number;
131
+ pageSize?: number;
132
+ sort?: string;
133
+ appliedFilters?: JsonObject;
134
+ items?: ListingItem[];
135
+ }
136
+
137
+ export interface TransactionObject {
138
+ orderId: string;
139
+ currency: string;
140
+ total: number;
141
+ lineItems: LineItem[];
142
+ subtotal?: number;
143
+ tax?: number;
144
+ shippingCost?: number;
145
+ discountTotal?: number;
146
+ couponCode?: string;
147
+ paymentMethod?: string;
148
+ }
149
+
150
+ /** An explicit merchant action. Use the typed helpers, or `track` to attach optional context. */
151
+ export interface SophiEvent {
152
+ type: EventType;
153
+ page: PageObject;
154
+ product?: ProductObject;
155
+ basket?: BasketObject;
156
+ listing?: ListingObject;
157
+ transaction?: TransactionObject;
158
+ user?: UserObject;
159
+ custom?: JsonObject;
160
+ }
161
+
162
+ export interface QueueLimits {
163
+ /** At most 7 days. */
164
+ maxAgeMs?: number;
165
+ maxEvents?: number;
166
+ maxBytes?: number;
167
+ maxEventBytes?: number;
168
+ }
169
+
170
+ export interface EventConfiguration {
171
+ apiKey: string;
172
+ /** HTTPS origin only, e.g. `https://api.usesophi.com` (the default). Use the same origin as chat. */
173
+ baseUrl?: string;
174
+ /**
175
+ * The shopper's analytics consent. Leave it out (or `null`) while it is not known yet: nothing is
176
+ * collected and nothing stored is deleted. `false` deletes the stored queue.
177
+ */
178
+ analyticsEnabled?: boolean | null;
179
+ /** The merchant's customer ID when the shopper is signed in. */
180
+ externalUserId?: string | null;
181
+ queueLimits?: QueueLimits;
182
+ }
183
+
184
+ export interface EventDiagnostic {
185
+ code: string;
186
+ eventId: string | null;
187
+ count: number;
188
+ }
189
+
190
+ export interface EventHandlers {
191
+ /** Delivery and storage diagnostics, for logging only. Never contains event payloads. */
192
+ onDiagnostic?: (diagnostic: EventDiagnostic) => void;
193
+ }
194
+
195
+ export interface EventReceipt {
196
+ /** Lowercase UUID of this occurrence; `null` when nothing was accepted. */
197
+ id: string | null;
198
+ /** `queued`: stored on the device. `disabled`: analytics not granted. `deliveredMemoryOnly`: identify. */
199
+ status: 'queued' | 'disabled' | 'deliveredMemoryOnly';
200
+ }
201
+
202
+ export interface FlushResult {
203
+ delivered: number;
204
+ retried: number;
205
+ discarded: number;
206
+ pending: number;
207
+ pausedReason: string | null;
208
+ }
209
+
210
+ /**
211
+ * Error codes, the same on iOS and Android: `validation` (see `fields`), `not_configured`,
212
+ * `invalid_configuration`, `event_too_large`, `queue_full`, `storage_unavailable`, `network`,
213
+ * `http` (see `status`), `invalid_response`, `invalid_receipt`, `backend_unsupported`, `disabled`,
214
+ * `merge_pending`, `identity_changed`, `duplicate_identity_module`, `internal_error`.
215
+ */
216
+ export type EventErrorCode = string;