@shware/analytics 0.0.1

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,46 @@
1
+ ## Config
2
+
3
+ layout.tsx
4
+
5
+ ```tsx
6
+ import { setupAnalytics } from '@shware/analytics';
7
+ import { v4 as uuidv4 } from 'uuid';
8
+
9
+ setupAnalytics({
10
+ endpoint: 'https://api.example.com/v1/analytics',
11
+ storage: {
12
+ getItem: async (key) => localStorage.getItem(key),
13
+ setItem: async (key, value) => localStorage.setItem(key, value),
14
+ },
15
+ deviceIdFetcher: async () => {
16
+ const cached = localStorage.getItem('device_id');
17
+ if (cached) return cached;
18
+ const id = crypto?.randomUUID ? crypto.randomUUID() : uuidv4();
19
+ localStorage.setItem('device_id', id);
20
+ return id;
21
+ },
22
+ });
23
+
24
+ function App() {
25
+ return <div>My React App</div>;
26
+ }
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ```tsx
32
+ import { track } from '@shware/analytics';
33
+
34
+ function Button() {
35
+ const onClick = async () => {
36
+ await api.login();
37
+ track('login', { method: 'google' });
38
+ };
39
+ return <button onClick={onClick}>Login with Google</button>;
40
+ }
41
+ ```
42
+
43
+ ## Backend API
44
+
45
+ - /analytics/tracks: track events
46
+ - /analytics/visitor: app visitors
@@ -0,0 +1,5 @@
1
+ export * from './setup';
2
+ export * from './track';
3
+ export * from './types';
4
+ export * from './visitor';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './setup';
2
+ export * from './track';
3
+ export * from './types';
4
+ export * from './visitor';
@@ -0,0 +1,26 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { ThirdPartyLogger, TrackTags } from '../types';
3
+ export interface Storage {
4
+ getItem: (key: string) => (string | null) | Promise<string | null>;
5
+ setItem: (key: string, value: string) => void | Promise<void>;
6
+ }
7
+ export interface Options {
8
+ release: string;
9
+ storage: Storage;
10
+ endpoint: string;
11
+ getTags: () => Promise<TrackTags>;
12
+ getDeviceId: () => Promise<string>;
13
+ thirdPartyLoggers?: ThirdPartyLogger[];
14
+ }
15
+ interface Config {
16
+ release: string;
17
+ storage: Storage;
18
+ http: AxiosInstance;
19
+ getTags: () => Promise<TrackTags>;
20
+ getDeviceId: () => Promise<string>;
21
+ thirdPartyLoggers: ThirdPartyLogger[];
22
+ }
23
+ export declare const config: Config;
24
+ export declare function setupAnalytics(init: Options): void;
25
+ export {};
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/setup/index.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEvD,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACnE,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/D;AAED,MAAM,WAAW,OAAO;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;IAClC,WAAW,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IACnC,iBAAiB,CAAC,EAAE,gBAAgB,EAAE,CAAC;CACxC;AAED,UAAU,MAAM;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,aAAa,CAAC;IACpB,OAAO,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,CAAC;IAClC,WAAW,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IACnC,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;CACvC;AAED,eAAO,MAAM,MAAM,EAAE,MAOpB,CAAC;AAEF,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,QAO3C"}
@@ -0,0 +1,18 @@
1
+ import axios from 'axios';
2
+ export var config = {
3
+ http: null,
4
+ release: '0.0.0',
5
+ storage: null,
6
+ getTags: null,
7
+ getDeviceId: null,
8
+ thirdPartyLoggers: [],
9
+ };
10
+ export function setupAnalytics(init) {
11
+ var _a;
12
+ config.release = init.release;
13
+ config.storage = init.storage;
14
+ config.getTags = init.getTags;
15
+ config.getDeviceId = init.getDeviceId;
16
+ config.thirdPartyLoggers = (_a = init.thirdPartyLoggers) !== null && _a !== void 0 ? _a : [];
17
+ config.http = axios.create({ baseURL: init.endpoint, withCredentials: true, adapter: 'fetch' });
18
+ }
@@ -0,0 +1,3 @@
1
+ export declare const standardEventNames: readonly ["add_payment_info", "add_shipping_info", "add_to_cart", "add_to_wishlist", "begin_checkout", "close_convert_lead", "close_unconvert_lead", "disqualify_lead", "earn_virtual_currency", "generate_lead", "join_group", "level_end", "level_start", "level_up", "login", "post_score", "purchase", "qualify_lead", "refund", "remove_from_cart", "search", "select_content", "select_item", "select_promotion", "share", "sign_up", "spend_virtual_currency", "tutorial_begin", "tutorial_complete", "unlock_achievement", "view_cart", "view_item", "view_item_list", "view_promotion", "working_lead"];
2
+ export declare const reservedEventNames: string[];
3
+ //# sourceMappingURL=ga.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ga.d.ts","sourceRoot":"","sources":["../../src/track/ga.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,klBAoCrB,CAAC;AAEX,eAAO,MAAM,kBAAkB,UAgC9B,CAAC"}
@@ -0,0 +1,70 @@
1
+ export var standardEventNames = [
2
+ 'add_payment_info',
3
+ 'add_shipping_info',
4
+ 'add_to_cart',
5
+ 'add_to_wishlist',
6
+ 'begin_checkout',
7
+ 'close_convert_lead',
8
+ 'close_unconvert_lead',
9
+ 'disqualify_lead',
10
+ 'earn_virtual_currency',
11
+ 'generate_lead',
12
+ 'join_group',
13
+ 'level_end',
14
+ 'level_start',
15
+ 'level_up',
16
+ 'login',
17
+ 'post_score',
18
+ 'purchase',
19
+ 'qualify_lead',
20
+ 'refund',
21
+ 'remove_from_cart',
22
+ 'search',
23
+ 'select_content',
24
+ 'select_item',
25
+ 'select_promotion',
26
+ 'share',
27
+ 'sign_up',
28
+ 'spend_virtual_currency',
29
+ 'tutorial_begin',
30
+ 'tutorial_complete',
31
+ 'unlock_achievement',
32
+ 'view_cart',
33
+ 'view_item',
34
+ 'view_item_list',
35
+ 'view_promotion',
36
+ 'working_lead',
37
+ ];
38
+ export var reservedEventNames = [
39
+ 'ad_activeview',
40
+ 'ad_click',
41
+ 'ad_exposure',
42
+ 'ad_query',
43
+ 'ad_reward',
44
+ 'adunit_exposure',
45
+ 'app_background',
46
+ 'app_clear_data',
47
+ 'app_exception',
48
+ 'app_remove',
49
+ 'app_store_refund',
50
+ 'app_store_subscription_cancel',
51
+ 'app_store_subscription_convert',
52
+ 'app_store_subscription_renew',
53
+ 'app_update',
54
+ 'app_upgrade',
55
+ 'dynamic_link_app_open',
56
+ 'dynamic_link_app_update',
57
+ 'dynamic_link_first_open',
58
+ 'error',
59
+ 'first_open',
60
+ 'first_visit',
61
+ 'in_app_purchase',
62
+ 'notification_dismiss',
63
+ 'notification_foreground',
64
+ 'notification_open',
65
+ 'notification_receive',
66
+ 'os_update',
67
+ 'session_start',
68
+ 'session_start_with_rollout',
69
+ 'user_engagement',
70
+ ];
@@ -0,0 +1,8 @@
1
+ import { EventName, Properties, TrackEventResponse } from '../types';
2
+ export interface TrackOptions {
3
+ enableThirdPartyLogging?: boolean;
4
+ onSucceed?: (response?: TrackEventResponse) => void;
5
+ onError?: (error: unknown) => void;
6
+ }
7
+ export declare function track<T extends EventName = EventName>(name: T, properties?: Properties<T>, trackOptions?: TrackOptions): void;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/track/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,SAAS,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAI1F,MAAM,WAAW,YAAY;IAC3B,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,SAAS,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE,kBAAkB,KAAK,IAAI,CAAC;IACpD,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACpC;AAuCD,wBAAgB,KAAK,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,EACnD,IAAI,EAAE,CAAC,EACP,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,EAC1B,YAAY,GAAE,YAA6B,QAG5C"}
@@ -0,0 +1,96 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { TokenBucket } from 'limiter';
38
+ import { config } from '../setup';
39
+ import { getVisitor } from '../visitor';
40
+ var defaultOptions = { enableThirdPartyLogging: true };
41
+ var REQUEST_TOKENS = 2;
42
+ var tokenBucket = new TokenBucket({
43
+ bucketSize: 20,
44
+ interval: 'second',
45
+ tokensPerInterval: 1,
46
+ });
47
+ function trackAsync(name_1, properties_1) {
48
+ return __awaiter(this, arguments, void 0, function (name, properties, trackOptions) {
49
+ var dto, data_1, e_1;
50
+ var _a;
51
+ var _b, _c;
52
+ if (trackOptions === void 0) { trackOptions = defaultOptions; }
53
+ return __generator(this, function (_d) {
54
+ switch (_d.label) {
55
+ case 0:
56
+ _d.trys.push([0, 5, , 6]);
57
+ return [4 /*yield*/, tokenBucket.removeTokens(REQUEST_TOKENS)];
58
+ case 1:
59
+ _d.sent();
60
+ _a = {
61
+ name: name,
62
+ properties: properties
63
+ };
64
+ return [4 /*yield*/, config.getTags()];
65
+ case 2:
66
+ _a.tags = _d.sent();
67
+ return [4 /*yield*/, getVisitor()];
68
+ case 3:
69
+ dto = (_a.visitor_id = (_d.sent()).id,
70
+ _a.timestamp = new Date().toISOString(),
71
+ _a);
72
+ return [4 /*yield*/, config.http.post("/events", dto)];
73
+ case 4:
74
+ data_1 = (_d.sent()).data;
75
+ // send to third-party loggers, for example Google Analytics and Facebook Pixel
76
+ if (!trackOptions.enableThirdPartyLogging || !config.thirdPartyLoggers)
77
+ return [2 /*return*/];
78
+ config.thirdPartyLoggers.forEach(function (logger) { return logger(name, properties, data_1.id); });
79
+ (_b = trackOptions.onSucceed) === null || _b === void 0 ? void 0 : _b.call(trackOptions, data_1);
80
+ return [3 /*break*/, 6];
81
+ case 5:
82
+ e_1 = _d.sent();
83
+ if (e_1 instanceof Error) {
84
+ console.log('Failed to send track event:', e_1.message);
85
+ }
86
+ (_c = trackOptions.onError) === null || _c === void 0 ? void 0 : _c.call(trackOptions, e_1);
87
+ return [3 /*break*/, 6];
88
+ case 6: return [2 /*return*/];
89
+ }
90
+ });
91
+ });
92
+ }
93
+ export function track(name, properties, trackOptions) {
94
+ if (trackOptions === void 0) { trackOptions = defaultOptions; }
95
+ trackAsync(name, properties, trackOptions).catch(console.error);
96
+ }
@@ -0,0 +1,314 @@
1
+ export interface Item {
2
+ item_brand?: string;
3
+ item_id?: string;
4
+ item_name?: string;
5
+ item_category?: string;
6
+ item_category2?: string;
7
+ item_category3?: string;
8
+ item_category4?: string;
9
+ item_category5?: string;
10
+ item_list_id?: string;
11
+ item_list_name?: string;
12
+ item_location_id?: string;
13
+ item_variant?: string;
14
+ quantity?: number;
15
+ price?: number;
16
+ }
17
+ /**
18
+ * ref: https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag
19
+ * ref: https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event
20
+ * */
21
+ export type StandardEventProperties = {
22
+ add_payment_info: {
23
+ items?: Item[];
24
+ currency?: string;
25
+ value?: number;
26
+ coupon?: string;
27
+ payment_type?: string;
28
+ };
29
+ add_shipping_info: {
30
+ items?: Item[];
31
+ currency?: string;
32
+ value?: number;
33
+ coupon?: string;
34
+ shipping_tier?: string;
35
+ };
36
+ add_to_cart: {
37
+ items?: Item[];
38
+ currency?: string;
39
+ value?: number;
40
+ };
41
+ add_to_wishlist: {
42
+ items?: Item[];
43
+ currency?: string;
44
+ value?: number;
45
+ };
46
+ begin_checkout: {
47
+ currency?: string;
48
+ value?: number;
49
+ coupon?: string;
50
+ items?: Item[];
51
+ [key: string]: any;
52
+ };
53
+ close_convert_lead: {
54
+ currency: string;
55
+ value: number;
56
+ };
57
+ close_unconvert_lead: {
58
+ currency: string;
59
+ value: number;
60
+ unconvert_lead_reason?: string;
61
+ };
62
+ disqualify_lead: {
63
+ currency: string;
64
+ value: number;
65
+ disqualified_lead_reason?: string;
66
+ };
67
+ earn_virtual_currency: {
68
+ virtual_currency_name: string;
69
+ value: number;
70
+ };
71
+ generate_lead: {
72
+ currency?: string;
73
+ value?: number;
74
+ };
75
+ join_group: {
76
+ group_id: string;
77
+ };
78
+ level_end: {
79
+ level: number;
80
+ success?: string;
81
+ };
82
+ level_start: {
83
+ level: number;
84
+ };
85
+ level_up: {
86
+ level: number;
87
+ character?: string;
88
+ };
89
+ login: {
90
+ method: string;
91
+ };
92
+ post_score: {
93
+ score: number;
94
+ level?: number;
95
+ character?: string;
96
+ };
97
+ purchase: {
98
+ affiliation?: string;
99
+ coupon?: string;
100
+ currency?: string;
101
+ items?: Item[];
102
+ shipping?: number;
103
+ tax?: number;
104
+ value?: number;
105
+ transaction_id?: string;
106
+ [key: string]: any;
107
+ };
108
+ qualify_lead: {
109
+ currency: string;
110
+ value: number;
111
+ };
112
+ refund: {
113
+ affiliation?: string;
114
+ coupon?: string;
115
+ currency?: string;
116
+ items?: Item[];
117
+ shipping?: number;
118
+ tax?: number;
119
+ value?: number;
120
+ transaction_id?: string;
121
+ };
122
+ remove_from_cart: {
123
+ items?: Item[];
124
+ value?: number;
125
+ currency?: string;
126
+ };
127
+ search: {
128
+ search_term: string;
129
+ number_of_nights?: number;
130
+ number_of_rooms?: number;
131
+ number_of_passengers?: number;
132
+ origin?: string;
133
+ destination?: string;
134
+ start_date?: string;
135
+ end_date?: string;
136
+ travel_class?: string;
137
+ };
138
+ select_content: {
139
+ content_type: string;
140
+ item_id: string;
141
+ };
142
+ select_item: {
143
+ items?: Item[];
144
+ content_type: string;
145
+ item_list_id: string;
146
+ item_list_name: string;
147
+ };
148
+ select_promotion: {
149
+ creative_name: string;
150
+ creative_slot: string;
151
+ items?: Item[];
152
+ location_id: string;
153
+ promotion_id: string;
154
+ promotion_name: string;
155
+ };
156
+ share: {
157
+ content_type: string;
158
+ item_id: string;
159
+ method: string;
160
+ activity_type?: string | null;
161
+ post_id?: string;
162
+ };
163
+ sign_up: {
164
+ method: string;
165
+ };
166
+ spend_virtual_currency: {
167
+ item_name: string;
168
+ virtual_currency_name: string;
169
+ value: number;
170
+ };
171
+ tutorial_begin: undefined;
172
+ tutorial_complete: undefined;
173
+ unlock_achievement: {
174
+ achievement_id: string;
175
+ };
176
+ view_cart: {
177
+ items?: Item[];
178
+ currency?: string;
179
+ value?: number;
180
+ };
181
+ view_item: {
182
+ items?: Item[];
183
+ currency?: string;
184
+ value?: number;
185
+ };
186
+ view_item_list: {
187
+ items?: Item[];
188
+ item_list_id?: string;
189
+ item_list_name?: string;
190
+ };
191
+ view_promotion: {
192
+ items?: Item[];
193
+ location_id?: string;
194
+ creative_name?: string;
195
+ creative_slot?: string;
196
+ promotion_id?: string;
197
+ promotion_name?: string;
198
+ };
199
+ working_lead: {
200
+ currency: string;
201
+ value: number;
202
+ lead_status?: string;
203
+ };
204
+ ad_impression: {
205
+ value?: number;
206
+ currency?: string;
207
+ ad_format?: string;
208
+ ad_platform?: string;
209
+ ad_source?: string;
210
+ ad_unit_name?: string;
211
+ };
212
+ app_open: undefined;
213
+ campaign_details: {
214
+ source: string;
215
+ medium: string;
216
+ campaign: string;
217
+ term?: string;
218
+ content?: string;
219
+ aclid?: string;
220
+ cp1?: string;
221
+ };
222
+ screen_view: {
223
+ screen_name?: string;
224
+ screen_class?: string;
225
+ };
226
+ view_search_results: {
227
+ search_term: string;
228
+ };
229
+ };
230
+ export type AllowedPropertyValues = string | number | boolean | null;
231
+ export type EventName = Lowercase<string>;
232
+ export type CustomEventProperties = Record<Lowercase<string>, AllowedPropertyValues>;
233
+ export type Properties<T extends EventName = EventName> = T extends keyof StandardEventProperties ? StandardEventProperties[T] : CustomEventProperties;
234
+ export interface UserData {
235
+ userId: string;
236
+ email?: string;
237
+ firstName?: string;
238
+ lastName?: string;
239
+ phone?: string;
240
+ dateOfBirth?: string;
241
+ gender?: string;
242
+ city?: string;
243
+ state?: string;
244
+ postal?: string;
245
+ country?: string;
246
+ }
247
+ export type ThirdPartyLogger = <T extends EventName = EventName>(name: T, properties?: Properties<T>, event_id?: string) => void;
248
+ export interface PlatformInfo {
249
+ os?: string;
250
+ os_name?: string;
251
+ os_version?: string;
252
+ browser?: string;
253
+ browser_name?: string;
254
+ browser_version?: string;
255
+ platform?: 'ios' | 'android' | 'web' | 'macos' | 'windows' | 'linux' | 'unknown';
256
+ }
257
+ export interface DeviceInfo {
258
+ device?: string;
259
+ device_id?: string;
260
+ device_type?: string;
261
+ device_vendor?: string;
262
+ device_pixel_ratio?: string;
263
+ screen_resolution?: `${number}x${number}`;
264
+ }
265
+ export interface EnvironmentInfo {
266
+ release?: string;
267
+ language?: string;
268
+ timezone?: string;
269
+ environment?: 'development' | 'production';
270
+ }
271
+ export interface SourceInfo {
272
+ source_url?: string;
273
+ source?: 'app' | 'web' | 'offline';
274
+ }
275
+ export interface ThirdPartyFields {
276
+ /**
277
+ * Meta pixel fields
278
+ * ref: https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/customer-information-parameters#fbc
279
+ * Stored in the _fbc/_fbp browser cookie under your domain
280
+ */
281
+ fbc?: string;
282
+ fbp?: string;
283
+ }
284
+ export interface TrackTags extends PlatformInfo, DeviceInfo, EnvironmentInfo, SourceInfo, ThirdPartyFields {
285
+ [key: string]: string | undefined;
286
+ }
287
+ export interface CreateTrackEventDTO<T extends EventName = EventName> {
288
+ name: string;
289
+ tags: TrackTags;
290
+ visitor_id: string;
291
+ properties?: Properties<T>;
292
+ timestamp: string;
293
+ }
294
+ export interface TrackEventResponse {
295
+ /**
296
+ * track event id
297
+ * some tracking system will use event_id and event_name for deduplication
298
+ * */
299
+ id: string;
300
+ }
301
+ export type VisitorProperties = Record<Lowercase<string>, AllowedPropertyValues | AllowedPropertyValues[] | undefined>;
302
+ export interface Visitor {
303
+ id: string;
304
+ device_id: string;
305
+ properties: VisitorProperties;
306
+ }
307
+ export interface CreateVisitorDTO {
308
+ device_id: string;
309
+ properties?: VisitorProperties;
310
+ }
311
+ export interface UpdateVisitorDTO {
312
+ properties: VisitorProperties;
313
+ }
314
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB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AC,CAAC,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,kBAAkB;IACjC;;;SAGK;IACL,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,MAAM,iBAAiB,GAAG,MAAM,CACpC,SAAS,CAAC,MAAM,CAAC,EACjB,qBAAqB,GAAG,qBAAqB,EAAE,GAAG,SAAS,CAC5D,CAAC;AAEF,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,iBAAiB,CAAC;CAC/B;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,iBAAiB,CAAC;CAChC;AAED,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,iBAAiB,CAAC;CAC/B"}
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { Visitor, VisitorProperties } from '../types';
2
+ export declare function getVisitor(): Promise<Visitor>;
3
+ export declare function setVisitor(properties: VisitorProperties): Promise<Visitor>;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/visitor/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAsC,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAsB/F,wBAAsB,UAAU,IAAI,OAAO,CAAC,OAAO,CAAC,CAOnD;AAED,wBAAsB,UAAU,CAAC,UAAU,EAAE,iBAAiB,oBAM7D"}
@@ -0,0 +1,112 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { config } from '../setup';
38
+ var key = 'visitor_id';
39
+ function getOrCreateVisitor() {
40
+ return __awaiter(this, void 0, void 0, function () {
41
+ var visitorId, response, dto, response;
42
+ var _a;
43
+ return __generator(this, function (_b) {
44
+ switch (_b.label) {
45
+ case 0: return [4 /*yield*/, config.storage.getItem(key)];
46
+ case 1:
47
+ visitorId = _b.sent();
48
+ if (!visitorId) return [3 /*break*/, 3];
49
+ return [4 /*yield*/, config.http.get("/visitors/".concat(visitorId))];
50
+ case 2:
51
+ response = _b.sent();
52
+ return [2 /*return*/, response.data];
53
+ case 3:
54
+ _a = {};
55
+ return [4 /*yield*/, config.getDeviceId()];
56
+ case 4:
57
+ _a.device_id = _b.sent();
58
+ return [4 /*yield*/, config.getTags()];
59
+ case 5:
60
+ dto = (_a.properties = _b.sent(),
61
+ _a);
62
+ return [4 /*yield*/, config.http.post("/visitors", dto)];
63
+ case 6:
64
+ response = _b.sent();
65
+ return [4 /*yield*/, config.storage.setItem(key, response.data.id)];
66
+ case 7:
67
+ _b.sent();
68
+ return [2 /*return*/, response.data];
69
+ }
70
+ });
71
+ });
72
+ }
73
+ var visitor = null;
74
+ var visitorFetcher = null;
75
+ export function getVisitor() {
76
+ return __awaiter(this, void 0, void 0, function () {
77
+ return __generator(this, function (_a) {
78
+ switch (_a.label) {
79
+ case 0:
80
+ if (visitor)
81
+ return [2 /*return*/, visitor];
82
+ if (visitorFetcher)
83
+ return [2 /*return*/, visitorFetcher];
84
+ visitorFetcher = getOrCreateVisitor();
85
+ return [4 /*yield*/, visitorFetcher];
86
+ case 1:
87
+ visitor = _a.sent();
88
+ visitorFetcher = null;
89
+ return [2 /*return*/, visitor];
90
+ }
91
+ });
92
+ });
93
+ }
94
+ export function setVisitor(properties) {
95
+ return __awaiter(this, void 0, void 0, function () {
96
+ var dto, id, response;
97
+ return __generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0:
100
+ dto = { properties: properties };
101
+ return [4 /*yield*/, getVisitor()];
102
+ case 1:
103
+ id = (_a.sent()).id;
104
+ return [4 /*yield*/, config.http.patch("/visitors/".concat(id), dto)];
105
+ case 2:
106
+ response = _a.sent();
107
+ visitor = response.data;
108
+ return [2 /*return*/, response.data];
109
+ }
110
+ });
111
+ });
112
+ }
@@ -0,0 +1,4 @@
1
+ import { TrackTags } from '../types';
2
+ export declare function getDeviceId(): string;
3
+ export declare function getTags(release: string): TrackTags;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/web/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,wBAAgB,WAAW,WAM1B;AAED,wBAAgB,OAAO,CAAC,OAAO,EAAE,MAAM,aA4BtC"}
@@ -0,0 +1,40 @@
1
+ import Bowser from 'bowser';
2
+ import cookie from 'cookie';
3
+ import { v4 as uuidv4 } from 'uuid';
4
+ export function getDeviceId() {
5
+ var cached = localStorage.getItem('device_id');
6
+ if (cached)
7
+ return cached;
8
+ var id = (crypto === null || crypto === void 0 ? void 0 : crypto.randomUUID) ? crypto.randomUUID() : uuidv4();
9
+ localStorage.setItem('device_id', id);
10
+ return id;
11
+ }
12
+ export function getTags(release) {
13
+ var parser = Bowser.getParser(window.navigator.userAgent);
14
+ var os = parser.getOS();
15
+ var browser = parser.getBrowser();
16
+ var platform = parser.getPlatform();
17
+ var parsed = cookie.parse(document.cookie);
18
+ var tags = {
19
+ os: "".concat(os.name, " ").concat(os.version),
20
+ os_name: os.name,
21
+ os_version: os.version,
22
+ browser: "".concat(browser.name, " ").concat(browser.version),
23
+ browser_name: browser.name,
24
+ browser_version: browser.version,
25
+ platform: 'web',
26
+ device: platform.model,
27
+ device_id: getDeviceId(),
28
+ device_type: platform.type,
29
+ device_vendor: platform.vendor,
30
+ device_pixel_ratio: "".concat(window.devicePixelRatio),
31
+ screen_resolution: "".concat(window.screen.width, "x").concat(window.screen.height),
32
+ release: release,
33
+ language: navigator.language,
34
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
35
+ environment: process.env.NODE_ENV === 'development' ? 'development' : 'production',
36
+ fbc: parsed._fbc,
37
+ fbp: parsed._fbp,
38
+ };
39
+ return tags;
40
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@shware/analytics",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "tsc --watch",
7
+ "build": "tsc"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./web": {
15
+ "types": "./dist/web.d.ts",
16
+ "default": "./dist/web.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {
23
+ "axios": "^1.8.3",
24
+ "axios-retry": "^4.5.0",
25
+ "bowser": "^2.11.0",
26
+ "cookie": "^1.0.2",
27
+ "limiter": "^3.0.0",
28
+ "uuid": "^11.1.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^22.13.10",
32
+ "typescript": "5.8.2"
33
+ }
34
+ }