@amplitude/analytics-react-native 0.0.1-dev.6 → 0.0.1-dev.7

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 (107) hide show
  1. package/README.md +150 -0
  2. package/android/src/main/java/com/amplitude/reactnative/AmplitudeReactNativeModule.kt +5 -5
  3. package/ios/AmplitudeReactNative.swift +4 -4
  4. package/lib/commonjs/attribution/campaign-tracker.js +1 -1
  5. package/lib/commonjs/attribution/campaign-tracker.js.map +1 -1
  6. package/lib/commonjs/config.js +2 -22
  7. package/lib/commonjs/config.js.map +1 -1
  8. package/lib/commonjs/index.js +2 -16
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/plugins/context.js +4 -4
  11. package/lib/commonjs/plugins/context.js.map +1 -1
  12. package/lib/commonjs/plugins/identity.js +40 -0
  13. package/lib/commonjs/plugins/identity.js.map +1 -0
  14. package/lib/commonjs/react-native-client.js +23 -57
  15. package/lib/commonjs/react-native-client.js.map +1 -1
  16. package/lib/commonjs/storage/cookie.js +1 -6
  17. package/lib/commonjs/storage/cookie.js.map +1 -1
  18. package/lib/commonjs/utils/analytics-connector.js +15 -0
  19. package/lib/commonjs/utils/analytics-connector.js.map +1 -0
  20. package/lib/commonjs/utils/query-params.js +1 -6
  21. package/lib/commonjs/utils/query-params.js.map +1 -1
  22. package/lib/commonjs/version.js +1 -1
  23. package/lib/commonjs/version.js.map +1 -1
  24. package/lib/module/attribution/campaign-tracker.js +1 -1
  25. package/lib/module/attribution/campaign-tracker.js.map +1 -1
  26. package/lib/module/config.js +1 -15
  27. package/lib/module/config.js.map +1 -1
  28. package/lib/module/index.js +3 -4
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/module/plugins/context.js +4 -4
  31. package/lib/module/plugins/context.js.map +1 -1
  32. package/lib/module/plugins/identity.js +29 -0
  33. package/lib/module/plugins/identity.js.map +1 -0
  34. package/lib/module/react-native-client.js +21 -55
  35. package/lib/module/react-native-client.js.map +1 -1
  36. package/lib/module/storage/cookie.js +1 -6
  37. package/lib/module/storage/cookie.js.map +1 -1
  38. package/lib/module/utils/analytics-connector.js +5 -0
  39. package/lib/module/utils/analytics-connector.js.map +1 -0
  40. package/lib/module/utils/query-params.js +1 -6
  41. package/lib/module/utils/query-params.js.map +1 -1
  42. package/lib/module/version.js +1 -1
  43. package/lib/module/version.js.map +1 -1
  44. package/lib/typescript/config.d.ts +1 -4
  45. package/lib/typescript/config.d.ts.map +1 -1
  46. package/lib/typescript/index.d.ts +2 -4
  47. package/lib/typescript/index.d.ts.map +1 -1
  48. package/lib/typescript/plugins/context.d.ts +5 -5
  49. package/lib/typescript/plugins/context.d.ts.map +1 -1
  50. package/lib/typescript/plugins/identity.d.ts +9 -0
  51. package/lib/typescript/plugins/identity.d.ts.map +1 -0
  52. package/lib/typescript/react-native-client.d.ts +7 -26
  53. package/lib/typescript/react-native-client.d.ts.map +1 -1
  54. package/lib/typescript/storage/cookie.d.ts.map +1 -1
  55. package/lib/typescript/utils/analytics-connector.d.ts +3 -0
  56. package/lib/typescript/utils/analytics-connector.d.ts.map +1 -0
  57. package/lib/typescript/utils/query-params.d.ts.map +1 -1
  58. package/lib/typescript/version.d.ts +1 -1
  59. package/package.json +1 -2
  60. package/lib/commonjs/constants.js +0 -31
  61. package/lib/commonjs/constants.js.map +0 -1
  62. package/lib/commonjs/transports/send-beacon.js +0 -43
  63. package/lib/commonjs/transports/send-beacon.js.map +0 -1
  64. package/lib/commonjs/transports/xhr.js +0 -54
  65. package/lib/commonjs/transports/xhr.js.map +0 -1
  66. package/lib/commonjs/utils/snippet-helper.js +0 -56
  67. package/lib/commonjs/utils/snippet-helper.js.map +0 -1
  68. package/lib/module/constants.js +0 -13
  69. package/lib/module/constants.js.map +0 -1
  70. package/lib/module/transports/send-beacon.js +0 -33
  71. package/lib/module/transports/send-beacon.js.map +0 -1
  72. package/lib/module/transports/xhr.js +0 -44
  73. package/lib/module/transports/xhr.js.map +0 -1
  74. package/lib/module/utils/snippet-helper.js +0 -41
  75. package/lib/module/utils/snippet-helper.js.map +0 -1
  76. package/lib/typescript/constants.d.ts +0 -13
  77. package/lib/typescript/constants.d.ts.map +0 -1
  78. package/lib/typescript/transports/send-beacon.d.ts +0 -6
  79. package/lib/typescript/transports/send-beacon.d.ts.map +0 -1
  80. package/lib/typescript/transports/xhr.d.ts +0 -7
  81. package/lib/typescript/transports/xhr.d.ts.map +0 -1
  82. package/lib/typescript/utils/snippet-helper.d.ts +0 -16
  83. package/lib/typescript/utils/snippet-helper.d.ts.map +0 -1
  84. package/src/attribution/campaign-parser.ts +0 -78
  85. package/src/attribution/campaign-tracker.ts +0 -112
  86. package/src/attribution/constants.ts +0 -32
  87. package/src/config.ts +0 -240
  88. package/src/constants.ts +0 -14
  89. package/src/cookie-migration/index.ts +0 -54
  90. package/src/index.ts +0 -24
  91. package/src/plugins/context.ts +0 -106
  92. package/src/react-native-client.ts +0 -366
  93. package/src/session-manager.ts +0 -81
  94. package/src/storage/cookie.ts +0 -100
  95. package/src/storage/local-storage.ts +0 -67
  96. package/src/storage/utm-cookie.ts +0 -27
  97. package/src/transports/fetch.ts +0 -23
  98. package/src/transports/send-beacon.ts +0 -34
  99. package/src/transports/xhr.ts +0 -36
  100. package/src/typings/browser-snippet.d.ts +0 -7
  101. package/src/typings/ua-parser.d.ts +0 -4
  102. package/src/utils/cookie-name.ts +0 -9
  103. package/src/utils/language.ts +0 -7
  104. package/src/utils/platform.ts +0 -9
  105. package/src/utils/query-params.ts +0 -21
  106. package/src/utils/snippet-helper.ts +0 -35
  107. package/src/version.ts +0 -1
package/src/config.ts DELETED
@@ -1,240 +0,0 @@
1
- import {
2
- Event,
3
- ReactNativeOptions,
4
- ReactNativeConfig as IReactNativeConfig,
5
- Storage,
6
- TrackingOptions,
7
- TransportType,
8
- UserSession,
9
- SessionManager as ISessionManager,
10
- } from '@amplitude/analytics-types';
11
- import { Config, MemoryStorage, UUID } from '@amplitude/analytics-core';
12
-
13
- import { CookieStorage } from './storage/cookie';
14
- import { FetchTransport } from './transports/fetch';
15
- import { LocalStorage } from './storage/local-storage';
16
- import { getCookieName } from './utils/cookie-name';
17
- import { getQueryParams } from './utils/query-params';
18
- import { XHRTransport } from './transports/xhr';
19
- import { SendBeaconTransport } from './transports/send-beacon';
20
- import { SessionManager } from './session-manager';
21
-
22
- export const getDefaultConfig = () => {
23
- const cookieStorage = new MemoryStorage<UserSession>();
24
- return {
25
- cookieExpiration: 365,
26
- cookieSameSite: 'Lax',
27
- cookieSecure: false,
28
- cookieStorage,
29
- disableCookies: false,
30
- domain: '',
31
- sessionManager: new SessionManager(cookieStorage, ''),
32
- sessionTimeout: 30 * 60 * 1000,
33
- storageProvider: new MemoryStorage<Event[]>(),
34
- trackingOptions: {
35
- city: true,
36
- country: true,
37
- carrier: true,
38
- deviceManufacturer: true,
39
- deviceModel: true,
40
- dma: true,
41
- ipAddress: true,
42
- language: true,
43
- osName: true,
44
- osVersion: true,
45
- platform: true,
46
- region: true,
47
- versionName: true,
48
- },
49
- transportProvider: new FetchTransport(),
50
- };
51
- };
52
-
53
- export class ReactNativeConfig extends Config implements IReactNativeConfig {
54
- appVersion?: string;
55
- cookieExpiration: number;
56
- cookieSameSite: string;
57
- cookieSecure: boolean;
58
- cookieStorage: Storage<UserSession>;
59
- disableCookies: boolean;
60
- domain: string;
61
- partnerId?: string;
62
- sessionTimeout: number;
63
- trackingOptions: TrackingOptions;
64
- sessionManager: ISessionManager;
65
-
66
- constructor(apiKey: string, userId?: string, options?: ReactNativeOptions) {
67
- const defaultConfig = getDefaultConfig();
68
- super({
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 ?? createTransport(options?.transport),
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[]>> => {
190
- let eventsStorage = overrides?.storageProvider;
191
- if (!eventsStorage || !(await eventsStorage.isEnabled())) {
192
- eventsStorage = new LocalStorage();
193
- if (!(await eventsStorage.isEnabled())) {
194
- eventsStorage = new MemoryStorage();
195
- }
196
- }
197
- return eventsStorage;
198
- };
199
-
200
- export const createDeviceId = (idFromCookies?: string, idFromOptions?: string, idFromQueryParams?: string) => {
201
- return idFromOptions || idFromQueryParams || idFromCookies || UUID();
202
- };
203
-
204
- export const createTransport = (transport?: TransportType) => {
205
- if (transport === TransportType.XHR) {
206
- return new XHRTransport();
207
- }
208
- if (transport === TransportType.SendBeacon) {
209
- return new SendBeaconTransport();
210
- }
211
- return getDefaultConfig().transportProvider;
212
- };
213
-
214
- export const getTopLevelDomain = async (url?: string) => {
215
- if (!(await new CookieStorage<string>().isEnabled()) || (!url && typeof location === 'undefined')) {
216
- return '';
217
- }
218
-
219
- const host = url ?? location.hostname;
220
- const parts = host.split('.');
221
- const levels = [];
222
- const storageKey = 'AMP_TLDTEST';
223
-
224
- for (let i = parts.length - 2; i >= 0; --i) {
225
- levels.push(parts.slice(i).join('.'));
226
- }
227
- for (let i = 0; i < levels.length; i++) {
228
- const domain = levels[i];
229
- const options = { domain: '.' + domain };
230
- const storage = new CookieStorage<number>(options);
231
- await storage.set(storageKey, 1);
232
- const value = await storage.get(storageKey);
233
- if (value) {
234
- await storage.remove(storageKey);
235
- return '.' + domain;
236
- }
237
- }
238
-
239
- return '';
240
- };
package/src/constants.ts DELETED
@@ -1,14 +0,0 @@
1
- export const UTM_SOURCE = 'utm_source';
2
- export const UTM_MEDIUM = 'utm_medium';
3
- export const UTM_CAMPAIGN = 'utm_campaign';
4
- export const UTM_TERM = 'utm_term';
5
- export const UTM_CONTENT = 'utm_content';
6
-
7
- export const UTMZ_SOURCE = 'utmcsr';
8
- export const UTMZ_MEDIUM = 'utmcmd';
9
- export const UTMZ_CAMPAIGN = 'utmccn';
10
- export const UTMZ_TERM = 'utmctr';
11
- export const UTMZ_CONTENT = 'utmcct';
12
-
13
- export const GCLID = 'gclid';
14
- export const FBCLID = 'fbclid';
@@ -1,54 +0,0 @@
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 DELETED
@@ -1,24 +0,0 @@
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
- setTransport,
18
- setUserId,
19
- track,
20
- } from './react-native-client';
21
- export { runQueuedFunctions } from './utils/snippet-helper';
22
- export { Revenue, Identify } from '@amplitude/analytics-core';
23
- import * as Types from '@amplitude/analytics-types';
24
- export { Types };
@@ -1,106 +0,0 @@
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
- os_name: string;
16
- os_version: string;
17
- device_brand: string;
18
- device_manufacturer: string;
19
- device_model: 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?.os_name || this.uaResult.browser.name;
71
- const osVersion = nativeContext?.os_version || this.uaResult.browser.version;
72
- const deviceVendor = nativeContext?.device_manufacturer || this.uaResult.device.vendor;
73
- const deviceModel = nativeContext?.device_model || 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
- }