@yesplz-ai/analytics-react-native 0.1.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/README.md ADDED
@@ -0,0 +1,183 @@
1
+ # @yesplz-ai/analytics-react-native
2
+
3
+ YesPlz Analytics SDK for React Native and Expo apps. This package provides manual event tracking for product discovery, cart, checkout, purchase, and user identity events.
4
+
5
+ ## Installing the Library
6
+
7
+ After setting up your development environment for React Native, navigate to your app's root directory and install the YesPlz React Native Analytics SDK. The SDK requires React Native 0.74+ and `@react-native-async-storage/async-storage`.
8
+
9
+ ```sh
10
+ npm install @yesplz-ai/analytics-react-native @react-native-async-storage/async-storage
11
+ ```
12
+
13
+ Or with Yarn:
14
+
15
+ ```sh
16
+ yarn add @yesplz-ai/analytics-react-native @react-native-async-storage/async-storage
17
+ ```
18
+
19
+ Or with pnpm:
20
+
21
+ ```sh
22
+ pnpm add @yesplz-ai/analytics-react-native @react-native-async-storage/async-storage
23
+ ```
24
+
25
+ For Expo projects, install the AsyncStorage peer dependency with Expo:
26
+
27
+ ```sh
28
+ npx expo install @react-native-async-storage/async-storage
29
+ ```
30
+
31
+ For bare React Native iOS apps, navigate to your application's iOS folder and install native dependencies. You do not need to update your Podfile to add YesPlz.
32
+
33
+ ```sh
34
+ cd ios
35
+ pod install
36
+ ```
37
+
38
+ ## Basic Setup
39
+
40
+ After installation, import the singleton `analytics` instance from the SDK, then initialize it once when your app starts. The value passed to `init` is your YesPlz app/shop ID.
41
+
42
+ ```ts
43
+ import { analytics } from '@yesplz-ai/analytics-react-native';
44
+
45
+ analytics.init('YOUR_APP_ID');
46
+ ```
47
+
48
+ If a customer is already signed in when your app starts, you can identify them during initialization:
49
+
50
+ ```ts
51
+ analytics.init('YOUR_APP_ID', {
52
+ userId: 'customer_123',
53
+ userProperties: {
54
+ loyalty_tier: 'gold',
55
+ },
56
+ });
57
+ ```
58
+
59
+ You can track events immediately after calling `init`. If your app tracks before initialization completes, the SDK queues the event and sends it when ready.
60
+
61
+ After initialization, direct tracking calls and event payloads are the same as the web integration. For more details, refer to the YesPlz Analytics documentation: https://docs.yesplz.ai/docs/analytics/#2-api-calls-direct-tracking
62
+
63
+ ## Tracking Events
64
+
65
+ ```ts
66
+ import { analytics, EventType, ItemListId } from '@yesplz-ai/analytics-react-native';
67
+
68
+ // Product list impression
69
+ analytics.track(EventType.ViewItemList, {
70
+ item_list_id: ItemListId.SEARCH_RESULTS,
71
+ search_term: 'linen dress',
72
+ items: [
73
+ { item_id: 'sku_1001', index: 0 },
74
+ { item_id: 'sku_1002', index: 1 },
75
+ ],
76
+ });
77
+
78
+ // Product view
79
+ analytics.track(EventType.ViewProduct, {
80
+ id: 'sku_1001',
81
+ item_list_id: ItemListId.SEARCH_RESULTS,
82
+ currency: 'USD',
83
+ value: 128,
84
+ });
85
+
86
+ // Add to cart
87
+ analytics.track(EventType.AddToCart, {
88
+ currency: 'USD',
89
+ value: 128,
90
+ items: [{ item_id: 'sku_1001', quantity: 1, price: 128 }],
91
+ });
92
+
93
+ // Begin checkout
94
+ analytics.track(EventType.BeginCheckout, {
95
+ currency: 'USD',
96
+ value: 128,
97
+ items: [{ item_id: 'sku_1001', quantity: 1, price: 128 }],
98
+ });
99
+
100
+ // Purchase
101
+ analytics.track(EventType.Purchase, {
102
+ transaction_id: 'order_9001',
103
+ currency: 'USD',
104
+ value: 138,
105
+ tax: 10,
106
+ shipping: 0,
107
+ items: [{ item_id: 'sku_1001', quantity: 1, price: 128 }],
108
+ });
109
+
110
+ await analytics.flush();
111
+ ```
112
+
113
+ Call `flush()` after high-value events such as purchases to send queued events immediately.
114
+
115
+ ## User Identity
116
+
117
+ Associate events with a signed-in customer:
118
+
119
+ ```ts
120
+ await analytics.setUser('customer_123', {
121
+ email_hash: 'hash-value',
122
+ loyalty_tier: 'gold',
123
+ });
124
+ ```
125
+
126
+ Clear the customer when they sign out:
127
+
128
+ ```ts
129
+ await analytics.setUser(null);
130
+ ```
131
+
132
+ ## Flush on App Background
133
+
134
+ For critical flows, flush when the app moves to the background:
135
+
136
+ ```ts
137
+ import { AppState } from 'react-native';
138
+ import { analytics } from '@yesplz-ai/analytics-react-native';
139
+
140
+ const subscription = AppState.addEventListener('change', (state) => {
141
+ if (state === 'background' || state === 'inactive') {
142
+ void analytics.flush();
143
+ }
144
+ });
145
+
146
+ // Later, during cleanup:
147
+ subscription.remove();
148
+ ```
149
+
150
+ ## TypeScript Payload Example
151
+
152
+ ```ts
153
+ import {
154
+ analytics,
155
+ EventType,
156
+ ItemListId,
157
+ type TrackProperties,
158
+ } from '@yesplz-ai/analytics-react-native';
159
+
160
+ const purchase: TrackProperties = {
161
+ transaction_id: 'order_9001',
162
+ currency: 'USD',
163
+ value: 138,
164
+ item_list_id: ItemListId.YOU_MAY_ALSO_LIKE,
165
+ items: [
166
+ {
167
+ item_id: 'sku_1001',
168
+ quantity: 1,
169
+ price: 128,
170
+ size: 'M',
171
+ color: 'Black',
172
+ },
173
+ ],
174
+ };
175
+
176
+ analytics.track(EventType.Purchase, purchase);
177
+ ```
178
+
179
+ ## Notes
180
+
181
+ - Tracking is manual. Call `analytics.track(...)` at the points in your app where customer actions occur.
182
+ - Use stable product IDs in `id` and `item_id` fields so attribution and reporting stay consistent.
183
+ - Include `currency`, `value`, and `items` for commerce events whenever available.