@amplitude/analytics-react-native 0.0.1-dev.8 → 0.1.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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -1
  3. package/lib/commonjs/config.js +17 -9
  4. package/lib/commonjs/config.js.map +1 -1
  5. package/lib/commonjs/index.js +2 -2
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/commonjs/plugins/identity.js +1 -1
  8. package/lib/commonjs/plugins/identity.js.map +1 -1
  9. package/lib/commonjs/react-native-client.js +10 -2
  10. package/lib/commonjs/react-native-client.js.map +1 -1
  11. package/lib/commonjs/version.js +1 -1
  12. package/lib/commonjs/version.js.map +1 -1
  13. package/lib/module/config.js +17 -9
  14. package/lib/module/config.js.map +1 -1
  15. package/lib/module/index.js +5 -3
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/module/plugins/identity.js +1 -1
  18. package/lib/module/plugins/identity.js.map +1 -1
  19. package/lib/module/react-native-client.js +10 -2
  20. package/lib/module/react-native-client.js.map +1 -1
  21. package/lib/module/version.js +1 -1
  22. package/lib/module/version.js.map +1 -1
  23. package/lib/typescript/attribution/campaign-tracker.d.ts +0 -1
  24. package/lib/typescript/attribution/campaign-tracker.d.ts.map +1 -1
  25. package/lib/typescript/config.d.ts +1 -1
  26. package/lib/typescript/config.d.ts.map +1 -1
  27. package/lib/typescript/index.d.ts +2 -1
  28. package/lib/typescript/index.d.ts.map +1 -1
  29. package/lib/typescript/react-native-client.d.ts.map +1 -1
  30. package/lib/typescript/version.d.ts +1 -1
  31. package/lib/typescript/version.d.ts.map +1 -1
  32. package/package.json +11 -9
  33. package/src/attribution/campaign-parser.ts +78 -0
  34. package/src/attribution/campaign-tracker.ts +112 -0
  35. package/src/attribution/constants.ts +32 -0
  36. package/src/config.ts +236 -0
  37. package/src/cookie-migration/index.ts +54 -0
  38. package/src/index.ts +24 -0
  39. package/src/plugins/context.ts +106 -0
  40. package/src/plugins/identity.ts +21 -0
  41. package/src/react-native-client.ts +339 -0
  42. package/src/session-manager.ts +81 -0
  43. package/src/storage/cookie.ts +97 -0
  44. package/src/storage/local-storage.ts +67 -0
  45. package/src/storage/utm-cookie.ts +27 -0
  46. package/src/transports/fetch.ts +23 -0
  47. package/src/typings/browser-snippet.d.ts +7 -0
  48. package/src/typings/ua-parser.d.ts +4 -0
  49. package/src/utils/analytics-connector.ts +5 -0
  50. package/src/utils/cookie-name.ts +9 -0
  51. package/src/utils/language.ts +7 -0
  52. package/src/utils/platform.ts +9 -0
  53. package/src/utils/query-params.ts +18 -0
  54. package/src/version.ts +1 -0
package/src/config.ts ADDED
@@ -0,0 +1,236 @@
1
+ import {
2
+ Event,
3
+ ReactNativeOptions,
4
+ ReactNativeConfig as IReactNativeConfig,
5
+ Storage,
6
+ TrackingOptions,
7
+ UserSession,
8
+ SessionManager as ISessionManager,
9
+ } from '@amplitude/analytics-types';
10
+ import { Config, MemoryStorage, UUID } from '@amplitude/analytics-core';
11
+
12
+ import { CookieStorage } from './storage/cookie';
13
+ import { FetchTransport } from './transports/fetch';
14
+ import { LocalStorage } from './storage/local-storage';
15
+ import { getCookieName } from './utils/cookie-name';
16
+ import { getQueryParams } from './utils/query-params';
17
+ import { SessionManager } from './session-manager';
18
+
19
+ export const getDefaultConfig = () => {
20
+ const cookieStorage = new MemoryStorage<UserSession>();
21
+ return {
22
+ cookieExpiration: 365,
23
+ cookieSameSite: 'Lax',
24
+ cookieSecure: false,
25
+ cookieStorage,
26
+ disableCookies: false,
27
+ domain: '',
28
+ sessionManager: new SessionManager(cookieStorage, ''),
29
+ sessionTimeout: 30 * 60 * 1000,
30
+ storageProvider: new MemoryStorage<Event[]>(),
31
+ trackingOptions: {
32
+ city: true,
33
+ country: true,
34
+ carrier: true,
35
+ deviceManufacturer: true,
36
+ deviceModel: true,
37
+ dma: true,
38
+ ipAddress: true,
39
+ language: true,
40
+ osName: true,
41
+ osVersion: true,
42
+ platform: true,
43
+ region: true,
44
+ versionName: true,
45
+ },
46
+ transportProvider: new FetchTransport(),
47
+ };
48
+ };
49
+
50
+ export class ReactNativeConfig extends Config implements IReactNativeConfig {
51
+ appVersion?: string;
52
+ cookieExpiration: number;
53
+ cookieSameSite: string;
54
+ cookieSecure: boolean;
55
+ cookieStorage: Storage<UserSession>;
56
+ disableCookies: boolean;
57
+ domain: string;
58
+ partnerId?: string;
59
+ sessionTimeout: number;
60
+ trackingOptions: TrackingOptions;
61
+ sessionManager: ISessionManager;
62
+
63
+ constructor(apiKey: string, userId?: string, options?: ReactNativeOptions) {
64
+ const defaultConfig = getDefaultConfig();
65
+ super({
66
+ flushIntervalMillis: 1000,
67
+ flushMaxRetries: 5,
68
+ flushQueueSize: 30,
69
+ ...options,
70
+ apiKey,
71
+ storageProvider: options?.storageProvider ?? defaultConfig.storageProvider,
72
+ transportProvider: options?.transportProvider ?? defaultConfig.transportProvider,
73
+ });
74
+ this.cookieStorage = options?.cookieStorage ?? defaultConfig.cookieStorage;
75
+ this.sessionManager = options?.sessionManager ?? defaultConfig.sessionManager;
76
+ this.sessionTimeout = options?.sessionTimeout ?? defaultConfig.sessionTimeout;
77
+
78
+ this.appVersion = options?.appVersion;
79
+ this.cookieExpiration = options?.cookieExpiration ?? defaultConfig.cookieExpiration;
80
+ this.cookieSameSite = options?.cookieSameSite ?? defaultConfig.cookieSameSite;
81
+ this.cookieSecure = options?.cookieSecure ?? defaultConfig.cookieSecure;
82
+ this.deviceId = options?.deviceId;
83
+ this.disableCookies = options?.disableCookies ?? defaultConfig.disableCookies;
84
+ this.domain = options?.domain ?? defaultConfig.domain;
85
+ this.lastEventTime = this.lastEventTime ?? options?.lastEventTime;
86
+ this.optOut = Boolean(options?.optOut);
87
+ this.partnerId = options?.partnerId;
88
+ this.sessionId = options?.sessionId;
89
+ this.trackingOptions = options?.trackingOptions ?? defaultConfig.trackingOptions;
90
+ this.userId = userId;
91
+ }
92
+
93
+ get deviceId() {
94
+ return this.sessionManager.getDeviceId();
95
+ }
96
+
97
+ set deviceId(deviceId: string | undefined) {
98
+ this.sessionManager.setDeviceId(deviceId);
99
+ }
100
+
101
+ get userId() {
102
+ return this.sessionManager.getUserId();
103
+ }
104
+
105
+ set userId(userId: string | undefined) {
106
+ this.sessionManager.setUserId(userId);
107
+ }
108
+
109
+ get sessionId() {
110
+ return this.sessionManager.getSessionId();
111
+ }
112
+
113
+ set sessionId(sessionId: number | undefined) {
114
+ this.sessionManager.setSessionId(sessionId);
115
+ }
116
+
117
+ get optOut() {
118
+ return this.sessionManager.getOptOut();
119
+ }
120
+
121
+ set optOut(optOut: boolean) {
122
+ this.sessionManager?.setOptOut(Boolean(optOut));
123
+ }
124
+
125
+ get lastEventTime() {
126
+ return this.sessionManager.getLastEventTime();
127
+ }
128
+
129
+ set lastEventTime(lastEventTime: number | undefined) {
130
+ this.sessionManager.setLastEventTime(lastEventTime);
131
+ }
132
+ }
133
+
134
+ export const useReactNativeConfig = async (
135
+ apiKey: string,
136
+ userId?: string,
137
+ options?: ReactNativeOptions,
138
+ ): Promise<IReactNativeConfig> => {
139
+ const defaultConfig = getDefaultConfig();
140
+ const domain = options?.domain ?? (await getTopLevelDomain());
141
+ const cookieStorage = await createCookieStorage({ ...options, domain });
142
+ const cookieName = getCookieName(apiKey);
143
+ const cookies = await cookieStorage.get(cookieName);
144
+ const queryParams = getQueryParams();
145
+ const sessionManager = await new SessionManager(cookieStorage, apiKey).load();
146
+
147
+ return new ReactNativeConfig(apiKey, userId ?? cookies?.userId, {
148
+ ...options,
149
+ cookieStorage,
150
+ sessionManager,
151
+ deviceId: createDeviceId(cookies?.deviceId, options?.deviceId, queryParams.deviceId),
152
+ domain,
153
+ optOut: options?.optOut ?? Boolean(cookies?.optOut),
154
+ sessionId: (await cookieStorage.get(cookieName))?.sessionId ?? options?.sessionId,
155
+ storageProvider: await createEventsStorage(options),
156
+ trackingOptions: { ...defaultConfig.trackingOptions, ...options?.trackingOptions },
157
+ transportProvider: options?.transportProvider ?? new FetchTransport(),
158
+ });
159
+ };
160
+
161
+ export const createCookieStorage = async (
162
+ overrides?: ReactNativeOptions,
163
+ baseConfig = getDefaultConfig(),
164
+ ): Promise<Storage<UserSession>> => {
165
+ const options = { ...baseConfig, ...overrides };
166
+ const cookieStorage = overrides?.cookieStorage;
167
+ if (!cookieStorage || !(await cookieStorage.isEnabled())) {
168
+ return createFlexibleStorage<UserSession>(options);
169
+ }
170
+ return cookieStorage;
171
+ };
172
+
173
+ export const createFlexibleStorage = async <T>(options: ReactNativeOptions): Promise<Storage<T>> => {
174
+ let storage: Storage<T> = new CookieStorage({
175
+ domain: options.domain,
176
+ expirationDays: options.cookieExpiration,
177
+ sameSite: options.cookieSameSite,
178
+ secure: options.cookieSecure,
179
+ });
180
+ if (options.disableCookies || !(await storage.isEnabled())) {
181
+ storage = new LocalStorage();
182
+ if (!(await storage.isEnabled())) {
183
+ storage = new MemoryStorage();
184
+ }
185
+ }
186
+ return storage;
187
+ };
188
+
189
+ export const createEventsStorage = async (overrides?: ReactNativeOptions): Promise<Storage<Event[]> | undefined> => {
190
+ const hasStorageProviderProperty = overrides && Object.prototype.hasOwnProperty.call(overrides, 'storageProvider');
191
+ // If storageProperty is explicitly undefined like `{ storageProperty: undefined }`
192
+ // then storageProvider is undefined
193
+ // If storageProvider is implicitly undefined like `{ }`
194
+ // then storageProvider is LocalStorage
195
+ // Otherwise storageProvider is overriden
196
+ if (!hasStorageProviderProperty || overrides.storageProvider) {
197
+ for (const storage of [overrides?.storageProvider, new LocalStorage<Event[]>()]) {
198
+ if (storage && (await storage.isEnabled())) {
199
+ return storage;
200
+ }
201
+ }
202
+ }
203
+ return undefined;
204
+ };
205
+
206
+ export const createDeviceId = (idFromCookies?: string, idFromOptions?: string, idFromQueryParams?: string) => {
207
+ return idFromOptions || idFromQueryParams || idFromCookies || UUID();
208
+ };
209
+
210
+ export const getTopLevelDomain = async (url?: string) => {
211
+ if (!(await new CookieStorage<string>().isEnabled()) || (!url && typeof location === 'undefined')) {
212
+ return '';
213
+ }
214
+
215
+ const host = url ?? location.hostname;
216
+ const parts = host.split('.');
217
+ const levels = [];
218
+ const storageKey = 'AMP_TLDTEST';
219
+
220
+ for (let i = parts.length - 2; i >= 0; --i) {
221
+ levels.push(parts.slice(i).join('.'));
222
+ }
223
+ for (let i = 0; i < levels.length; i++) {
224
+ const domain = levels[i];
225
+ const options = { domain: '.' + domain };
226
+ const storage = new CookieStorage<number>(options);
227
+ await storage.set(storageKey, 1);
228
+ const value = await storage.get(storageKey);
229
+ if (value) {
230
+ await storage.remove(storageKey);
231
+ return '.' + domain;
232
+ }
233
+ }
234
+
235
+ return '';
236
+ };
@@ -0,0 +1,54 @@
1
+ import { BrowserOptions, Storage, UserSession } from '@amplitude/analytics-types';
2
+ import { getOldCookieName } from '../utils/cookie-name';
3
+ import { LocalStorage } from '../storage/local-storage';
4
+ import { CookieStorage } from '../storage/cookie';
5
+
6
+ export const parseOldCookies = async (apiKey: string, options?: BrowserOptions): Promise<UserSession> => {
7
+ let storage: Storage<string> = new CookieStorage<string>();
8
+ if (!(await storage.isEnabled()) || options?.disableCookies) {
9
+ storage = new LocalStorage<string>();
10
+ }
11
+ if (!(await storage.isEnabled())) {
12
+ return {
13
+ optOut: false,
14
+ };
15
+ }
16
+
17
+ const oldCookieName = getOldCookieName(apiKey);
18
+ const cookies = await storage.getRaw(oldCookieName);
19
+
20
+ if (!cookies) {
21
+ return {
22
+ optOut: false,
23
+ };
24
+ }
25
+
26
+ await storage.remove(oldCookieName);
27
+ const [deviceId, userId, optOut, sessionId, lastEventTime] = cookies.split('.');
28
+ return {
29
+ deviceId,
30
+ userId: decode(userId),
31
+ sessionId: parseTime(sessionId),
32
+ lastEventTime: parseTime(lastEventTime),
33
+ optOut: Boolean(optOut),
34
+ };
35
+ };
36
+
37
+ export const parseTime = (num: string) => {
38
+ const integer = parseInt(num, 32);
39
+ if (isNaN(integer)) {
40
+ return undefined;
41
+ }
42
+ return integer;
43
+ };
44
+
45
+ export const decode = (value?: string): string | undefined => {
46
+ if (!atob || !escape || !value) {
47
+ return undefined;
48
+ }
49
+ try {
50
+ return decodeURIComponent(escape(atob(value)));
51
+ } catch {
52
+ return undefined;
53
+ }
54
+ };
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ export {
2
+ add,
3
+ flush,
4
+ getDeviceId,
5
+ getSessionId,
6
+ getUserId,
7
+ groupIdentify,
8
+ identify,
9
+ init,
10
+ logEvent,
11
+ remove,
12
+ revenue,
13
+ setDeviceId,
14
+ setGroup,
15
+ setOptOut,
16
+ setSessionId,
17
+ setUserId,
18
+ track,
19
+ } from './react-native-client';
20
+ export { Revenue, Identify } from '@amplitude/analytics-core';
21
+ // Hack - react-native apps have trouble with:
22
+ // export * as Types from '@amplitude/analytics-types
23
+ import * as Types from '@amplitude/analytics-types';
24
+ export { Types };
@@ -0,0 +1,106 @@
1
+ import { BeforePlugin, ReactNativeConfig, Event, PluginType } from '@amplitude/analytics-types';
2
+ import UAParser from '@amplitude/ua-parser-js';
3
+ import { UUID } from '@amplitude/analytics-core';
4
+ import { getLanguage } from '../utils/language';
5
+ import { VERSION } from '../version';
6
+ import { NativeModules } from 'react-native';
7
+
8
+ const BROWSER_PLATFORM = 'Web';
9
+ const IP_ADDRESS = '$remote';
10
+
11
+ type NativeContext = {
12
+ version: string;
13
+ platform: string;
14
+ language: string;
15
+ osName: string;
16
+ osVersion: string;
17
+ deviceBrand: string;
18
+ deviceManufacturer: string;
19
+ deviceModel: string;
20
+ carrier: string;
21
+ };
22
+
23
+ export interface AmplitudeReactNative {
24
+ getApplicationContext(): Promise<NativeContext>;
25
+ }
26
+
27
+ export class Context implements BeforePlugin {
28
+ name = 'context';
29
+ type = PluginType.BEFORE as const;
30
+
31
+ // this.config is defined in setup() which will always be called first
32
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
+ // @ts-ignore
34
+ config: ReactNativeConfig;
35
+ eventId = 0;
36
+ uaResult: UAParser.IResult;
37
+ nativeModule: AmplitudeReactNative | undefined = NativeModules.AmplitudeReactNative as
38
+ | AmplitudeReactNative
39
+ | undefined;
40
+ library = `amplitude-react-native-ts/${VERSION}`;
41
+
42
+ constructor() {
43
+ let agent: string | undefined;
44
+ /* istanbul ignore else */
45
+ if (typeof navigator !== 'undefined') {
46
+ agent = navigator.userAgent;
47
+ }
48
+ this.uaResult = new UAParser(agent).getResult();
49
+ }
50
+
51
+ setup(config: ReactNativeConfig): Promise<undefined> {
52
+ this.config = config;
53
+ return Promise.resolve(undefined);
54
+ }
55
+
56
+ async execute(context: Event): Promise<Event> {
57
+ /**
58
+ * Manages user session triggered by new events
59
+ */
60
+ if (!this.isSessionValid()) {
61
+ // Creates new session
62
+ this.config.sessionId = Date.now();
63
+ } // else use previously creates session
64
+ // Updates last event time to extend time-based session
65
+ this.config.lastEventTime = Date.now();
66
+ const time = new Date().getTime();
67
+ const nativeContext = await this.nativeModule?.getApplicationContext();
68
+ const appVersion = nativeContext?.version || this.config.appVersion;
69
+ const platform = nativeContext?.platform || BROWSER_PLATFORM;
70
+ const osName = nativeContext?.osName || this.uaResult.browser.name;
71
+ const osVersion = nativeContext?.osVersion || this.uaResult.browser.version;
72
+ const deviceVendor = nativeContext?.deviceManufacturer || this.uaResult.device.vendor;
73
+ const deviceModel = nativeContext?.deviceModel || this.uaResult.device.model || this.uaResult.os.name;
74
+ const language = nativeContext?.language || getLanguage();
75
+ const carrier = nativeContext?.carrier;
76
+
77
+ const event: Event = {
78
+ user_id: this.config.userId,
79
+ device_id: this.config.deviceId,
80
+ session_id: this.config.sessionId,
81
+ time,
82
+ ...(this.config.appVersion && { app_version: appVersion }),
83
+ ...(this.config.trackingOptions.platform && { platform: platform }),
84
+ ...(this.config.trackingOptions.osName && { os_name: osName }),
85
+ ...(this.config.trackingOptions.osVersion && { os_version: osVersion }),
86
+ ...(this.config.trackingOptions.deviceManufacturer && { device_manufacturer: deviceVendor }),
87
+ ...(this.config.trackingOptions.deviceModel && { device_model: deviceModel }),
88
+ ...(this.config.trackingOptions.language && { language: language }),
89
+ ...(this.config.trackingOptions.carrier && { carrier: carrier }),
90
+ ...(this.config.trackingOptions.ipAddress && { ip: IP_ADDRESS }),
91
+ insert_id: UUID(),
92
+ partner_id: this.config.partnerId,
93
+ plan: this.config.plan,
94
+ ...context,
95
+ event_id: this.eventId++,
96
+ library: this.library,
97
+ };
98
+ return event;
99
+ }
100
+
101
+ isSessionValid() {
102
+ const lastEventTime = this.config.lastEventTime || Date.now();
103
+ const timeSinceLastEvent = Date.now() - lastEventTime;
104
+ return timeSinceLastEvent < this.config.sessionTimeout;
105
+ }
106
+ }
@@ -0,0 +1,21 @@
1
+ import { BeforePlugin, PluginType, Event, Config } from '@amplitude/analytics-types';
2
+ import { getAnalyticsConnector } from '../utils/analytics-connector';
3
+
4
+ export class IdentityEventSender implements BeforePlugin {
5
+ name = 'identity';
6
+ type = PluginType.BEFORE as const;
7
+
8
+ identityStore = getAnalyticsConnector().identityStore;
9
+
10
+ async execute(context: Event): Promise<Event> {
11
+ const userProperties = context.user_properties as Record<string, any>;
12
+ if (userProperties) {
13
+ this.identityStore.editIdentity().updateUserProperties(userProperties).commit();
14
+ }
15
+ return context;
16
+ }
17
+
18
+ setup(_: Config): Promise<undefined> {
19
+ return Promise.resolve(undefined);
20
+ }
21
+ }