@amplitude/analytics-react-native 0.0.1-dev.4 → 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.
- package/README.md +150 -0
- package/android/build.gradle +7 -9
- package/android/src/main/java/com/amplitude/reactnative/AmplitudeReactNativeModule.kt +5 -5
- package/ios/AmplitudeReactNative.swift +4 -4
- package/lib/commonjs/attribution/campaign-tracker.js +1 -1
- package/lib/commonjs/attribution/campaign-tracker.js.map +1 -1
- package/lib/commonjs/config.js +2 -22
- package/lib/commonjs/config.js.map +1 -1
- package/lib/commonjs/index.js +2 -16
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/plugins/context.js +4 -4
- package/lib/commonjs/plugins/context.js.map +1 -1
- package/lib/commonjs/plugins/identity.js +40 -0
- package/lib/commonjs/plugins/identity.js.map +1 -0
- package/lib/commonjs/react-native-client.js +23 -57
- package/lib/commonjs/react-native-client.js.map +1 -1
- package/lib/commonjs/storage/cookie.js +1 -6
- package/lib/commonjs/storage/cookie.js.map +1 -1
- package/lib/commonjs/utils/analytics-connector.js +15 -0
- package/lib/commonjs/utils/analytics-connector.js.map +1 -0
- package/lib/commonjs/utils/query-params.js +1 -6
- package/lib/commonjs/utils/query-params.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/attribution/campaign-tracker.js +1 -1
- package/lib/module/attribution/campaign-tracker.js.map +1 -1
- package/lib/module/config.js +1 -15
- package/lib/module/config.js.map +1 -1
- package/lib/module/index.js +3 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/plugins/context.js +4 -4
- package/lib/module/plugins/context.js.map +1 -1
- package/lib/module/plugins/identity.js +29 -0
- package/lib/module/plugins/identity.js.map +1 -0
- package/lib/module/react-native-client.js +21 -55
- package/lib/module/react-native-client.js.map +1 -1
- package/lib/module/storage/cookie.js +1 -6
- package/lib/module/storage/cookie.js.map +1 -1
- package/lib/module/utils/analytics-connector.js +5 -0
- package/lib/module/utils/analytics-connector.js.map +1 -0
- package/lib/module/utils/query-params.js +1 -6
- package/lib/module/utils/query-params.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/{src/attribution → attribution}/campaign-parser.d.ts +0 -0
- package/lib/typescript/attribution/campaign-parser.d.ts.map +1 -0
- package/lib/typescript/{src/attribution → attribution}/campaign-tracker.d.ts +0 -0
- package/lib/typescript/attribution/campaign-tracker.d.ts.map +1 -0
- package/lib/typescript/{src/attribution → attribution}/constants.d.ts +0 -0
- package/lib/typescript/attribution/constants.d.ts.map +1 -0
- package/lib/typescript/{src/config.d.ts → config.d.ts} +1 -4
- package/lib/typescript/config.d.ts.map +1 -0
- package/lib/typescript/{src/cookie-migration → cookie-migration}/index.d.ts +0 -0
- package/lib/typescript/cookie-migration/index.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +4 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/{src/plugins → plugins}/context.d.ts +5 -5
- package/lib/typescript/plugins/context.d.ts.map +1 -0
- package/lib/typescript/plugins/identity.d.ts +9 -0
- package/lib/typescript/plugins/identity.d.ts.map +1 -0
- package/lib/typescript/{src/react-native-client.d.ts → react-native-client.d.ts} +7 -26
- package/lib/typescript/react-native-client.d.ts.map +1 -0
- package/lib/typescript/{src/session-manager.d.ts → session-manager.d.ts} +0 -0
- package/lib/typescript/session-manager.d.ts.map +1 -0
- package/lib/typescript/{src/storage → storage}/cookie.d.ts +0 -0
- package/lib/typescript/storage/cookie.d.ts.map +1 -0
- package/lib/typescript/{src/storage → storage}/local-storage.d.ts +0 -0
- package/lib/typescript/storage/local-storage.d.ts.map +1 -0
- package/lib/typescript/{src/storage → storage}/utm-cookie.d.ts +0 -0
- package/lib/typescript/storage/utm-cookie.d.ts.map +1 -0
- package/lib/typescript/{src/transports → transports}/fetch.d.ts +0 -0
- package/lib/typescript/transports/fetch.d.ts.map +1 -0
- package/lib/typescript/utils/analytics-connector.d.ts +3 -0
- package/lib/typescript/utils/analytics-connector.d.ts.map +1 -0
- package/lib/typescript/{src/utils → utils}/cookie-name.d.ts +0 -0
- package/lib/typescript/utils/cookie-name.d.ts.map +1 -0
- package/lib/typescript/{src/utils → utils}/language.d.ts +0 -0
- package/lib/typescript/utils/language.d.ts.map +1 -0
- package/lib/typescript/{src/utils → utils}/platform.d.ts +0 -0
- package/lib/typescript/utils/platform.d.ts.map +1 -0
- package/lib/typescript/{src/utils → utils}/query-params.d.ts +0 -0
- package/lib/typescript/utils/query-params.d.ts.map +1 -0
- package/lib/typescript/version.d.ts +2 -0
- package/lib/typescript/version.d.ts.map +1 -0
- package/package.json +1 -2
- package/lib/commonjs/constants.js +0 -31
- package/lib/commonjs/constants.js.map +0 -1
- package/lib/commonjs/transports/send-beacon.js +0 -43
- package/lib/commonjs/transports/send-beacon.js.map +0 -1
- package/lib/commonjs/transports/xhr.js +0 -54
- package/lib/commonjs/transports/xhr.js.map +0 -1
- package/lib/commonjs/utils/snippet-helper.js +0 -56
- package/lib/commonjs/utils/snippet-helper.js.map +0 -1
- package/lib/module/constants.js +0 -13
- package/lib/module/constants.js.map +0 -1
- package/lib/module/transports/send-beacon.js +0 -33
- package/lib/module/transports/send-beacon.js.map +0 -1
- package/lib/module/transports/xhr.js +0 -44
- package/lib/module/transports/xhr.js.map +0 -1
- package/lib/module/utils/snippet-helper.js +0 -41
- package/lib/module/utils/snippet-helper.js.map +0 -1
- package/lib/typescript/src/attribution/campaign-parser.d.ts.map +0 -1
- package/lib/typescript/src/attribution/campaign-tracker.d.ts.map +0 -1
- package/lib/typescript/src/attribution/constants.d.ts.map +0 -1
- package/lib/typescript/src/config.d.ts.map +0 -1
- package/lib/typescript/src/constants.d.ts +0 -13
- package/lib/typescript/src/constants.d.ts.map +0 -1
- package/lib/typescript/src/cookie-migration/index.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -6
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/lib/typescript/src/plugins/context.d.ts.map +0 -1
- package/lib/typescript/src/react-native-client.d.ts.map +0 -1
- package/lib/typescript/src/session-manager.d.ts.map +0 -1
- package/lib/typescript/src/storage/cookie.d.ts.map +0 -1
- package/lib/typescript/src/storage/local-storage.d.ts.map +0 -1
- package/lib/typescript/src/storage/utm-cookie.d.ts.map +0 -1
- package/lib/typescript/src/transports/fetch.d.ts.map +0 -1
- package/lib/typescript/src/transports/send-beacon.d.ts +0 -6
- package/lib/typescript/src/transports/send-beacon.d.ts.map +0 -1
- package/lib/typescript/src/transports/xhr.d.ts +0 -7
- package/lib/typescript/src/transports/xhr.d.ts.map +0 -1
- package/lib/typescript/src/utils/cookie-name.d.ts.map +0 -1
- package/lib/typescript/src/utils/language.d.ts.map +0 -1
- package/lib/typescript/src/utils/platform.d.ts.map +0 -1
- package/lib/typescript/src/utils/query-params.d.ts.map +0 -1
- package/lib/typescript/src/utils/snippet-helper.d.ts +0 -16
- package/lib/typescript/src/utils/snippet-helper.d.ts.map +0 -1
- package/lib/typescript/src/version.d.ts +0 -2
- package/lib/typescript/src/version.d.ts.map +0 -1
- package/src/attribution/campaign-parser.ts +0 -78
- package/src/attribution/campaign-tracker.ts +0 -112
- package/src/attribution/constants.ts +0 -32
- package/src/config.ts +0 -240
- package/src/constants.ts +0 -14
- package/src/cookie-migration/index.ts +0 -54
- package/src/index.ts +0 -24
- package/src/plugins/context.ts +0 -106
- package/src/react-native-client.ts +0 -366
- package/src/session-manager.ts +0 -81
- package/src/storage/cookie.ts +0 -100
- package/src/storage/local-storage.ts +0 -67
- package/src/storage/utm-cookie.ts +0 -27
- package/src/transports/fetch.ts +0 -23
- package/src/transports/send-beacon.ts +0 -34
- package/src/transports/xhr.ts +0 -36
- package/src/typings/browser-snippet.d.ts +0 -7
- package/src/typings/ua-parser.d.ts +0 -4
- package/src/utils/cookie-name.ts +0 -9
- package/src/utils/language.ts +0 -7
- package/src/utils/platform.ts +0 -9
- package/src/utils/query-params.ts +0 -21
- package/src/utils/snippet-helper.ts +0 -35
- package/src/version.ts +0 -1
|
@@ -1,366 +0,0 @@
|
|
|
1
|
-
import { AmplitudeCore, Destination, Identify, Revenue, returnWrapper } from '@amplitude/analytics-core';
|
|
2
|
-
import {
|
|
3
|
-
ReactNativeConfig,
|
|
4
|
-
Campaign,
|
|
5
|
-
EventOptions,
|
|
6
|
-
Identify as IIdentify,
|
|
7
|
-
Result,
|
|
8
|
-
Revenue as IRevenue,
|
|
9
|
-
TransportType,
|
|
10
|
-
ReactNativeOptions,
|
|
11
|
-
AdditionalReactNativeOptions,
|
|
12
|
-
AttributionReactNativeOptions,
|
|
13
|
-
} from '@amplitude/analytics-types';
|
|
14
|
-
import { convertProxyObjectToRealObject, isInstanceProxy } from './utils/snippet-helper';
|
|
15
|
-
import { Context } from './plugins/context';
|
|
16
|
-
import { useReactNativeConfig, createTransport, createDeviceId, createFlexibleStorage } from './config';
|
|
17
|
-
import { parseOldCookies } from './cookie-migration';
|
|
18
|
-
import { CampaignTracker } from './attribution/campaign-tracker';
|
|
19
|
-
import { isNative } from './utils/platform';
|
|
20
|
-
|
|
21
|
-
export class AmplitudeReactNative extends AmplitudeCore<ReactNativeConfig> {
|
|
22
|
-
async init(apiKey: string, userId?: string, options?: ReactNativeOptions & AdditionalReactNativeOptions) {
|
|
23
|
-
// Step 1: Read cookies stored by old SDK
|
|
24
|
-
const oldCookies = await parseOldCookies(apiKey, options);
|
|
25
|
-
|
|
26
|
-
// Step 2: Create react native config
|
|
27
|
-
const reactNativeOptions = await useReactNativeConfig(apiKey, userId || oldCookies.userId, {
|
|
28
|
-
...options,
|
|
29
|
-
deviceId: oldCookies.deviceId ?? options?.deviceId,
|
|
30
|
-
sessionId: oldCookies.sessionId ?? options?.sessionId,
|
|
31
|
-
optOut: options?.optOut ?? oldCookies.optOut,
|
|
32
|
-
lastEventTime: oldCookies.lastEventTime,
|
|
33
|
-
});
|
|
34
|
-
await super.init(undefined, undefined, reactNativeOptions);
|
|
35
|
-
|
|
36
|
-
// Step 3: Manage session
|
|
37
|
-
let isNewSession = false;
|
|
38
|
-
if (
|
|
39
|
-
!this.config.sessionId ||
|
|
40
|
-
(this.config.lastEventTime && Date.now() - this.config.lastEventTime > this.config.sessionTimeout)
|
|
41
|
-
) {
|
|
42
|
-
// Either
|
|
43
|
-
// 1) No previous session; or
|
|
44
|
-
// 2) Previous session expired
|
|
45
|
-
this.config.sessionId = Date.now();
|
|
46
|
-
isNewSession = true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Step 4: Install plugins
|
|
50
|
-
// Do not track any events before this
|
|
51
|
-
await this.add(new Context());
|
|
52
|
-
await this.add(new Destination());
|
|
53
|
-
|
|
54
|
-
// Step 5: Track attributions
|
|
55
|
-
await this.runAttributionStrategy(options?.attribution, isNewSession);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async runAttributionStrategy(attributionConfig?: AttributionReactNativeOptions, isNewSession = false) {
|
|
59
|
-
if (isNative()) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
const track = this.track.bind(this);
|
|
63
|
-
const onNewCampaign = this.setSessionId.bind(this, Date.now());
|
|
64
|
-
|
|
65
|
-
const storage = await createFlexibleStorage<Campaign>(this.config);
|
|
66
|
-
const campaignTracker = new CampaignTracker(this.config.apiKey, {
|
|
67
|
-
...attributionConfig,
|
|
68
|
-
storage,
|
|
69
|
-
track,
|
|
70
|
-
onNewCampaign,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
await campaignTracker.send(isNewSession);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
getUserId() {
|
|
77
|
-
return this.config.userId;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
setUserId(userId: string | undefined) {
|
|
81
|
-
this.config.userId = userId;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
getDeviceId() {
|
|
85
|
-
return this.config.deviceId;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
setDeviceId(deviceId: string) {
|
|
89
|
-
this.config.deviceId = deviceId;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
regenerateDeviceId() {
|
|
93
|
-
const deviceId = createDeviceId();
|
|
94
|
-
this.setDeviceId(deviceId);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getSessionId() {
|
|
98
|
-
return this.config.sessionId;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
setSessionId(sessionId: number) {
|
|
102
|
-
this.config.sessionId = sessionId;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
setOptOut(optOut: boolean) {
|
|
106
|
-
this.config.optOut = optOut;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
setTransport(transport: TransportType) {
|
|
110
|
-
this.config.transportProvider = createTransport(transport);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
identify(identify: IIdentify, eventOptions?: EventOptions): Promise<Result> {
|
|
114
|
-
if (isInstanceProxy(identify)) {
|
|
115
|
-
const queue = identify._q;
|
|
116
|
-
identify._q = [];
|
|
117
|
-
identify = convertProxyObjectToRealObject(new Identify(), queue);
|
|
118
|
-
}
|
|
119
|
-
return super.identify(identify, eventOptions);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
groupIdentify(
|
|
123
|
-
groupType: string,
|
|
124
|
-
groupName: string | string[],
|
|
125
|
-
identify: IIdentify,
|
|
126
|
-
eventOptions?: EventOptions,
|
|
127
|
-
): Promise<Result> {
|
|
128
|
-
if (isInstanceProxy(identify)) {
|
|
129
|
-
const queue = identify._q;
|
|
130
|
-
identify._q = [];
|
|
131
|
-
identify = convertProxyObjectToRealObject(new Identify(), queue);
|
|
132
|
-
}
|
|
133
|
-
return super.groupIdentify(groupType, groupName, identify, eventOptions);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
revenue(revenue: IRevenue, eventOptions?: EventOptions) {
|
|
137
|
-
if (isInstanceProxy(revenue)) {
|
|
138
|
-
const queue = revenue._q;
|
|
139
|
-
revenue._q = [];
|
|
140
|
-
revenue = convertProxyObjectToRealObject(new Revenue(), queue);
|
|
141
|
-
}
|
|
142
|
-
return super.revenue(revenue, eventOptions);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const client = new AmplitudeReactNative();
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Initializes the Amplitude SDK with your apiKey, userId and optional configurations.
|
|
150
|
-
* This method must be called before any other operations.
|
|
151
|
-
*
|
|
152
|
-
* ```typescript
|
|
153
|
-
* await init(API_KEY, USER_ID, options).promise;
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
export const init = returnWrapper(client.init.bind(client));
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Adds a new plugin.
|
|
160
|
-
*
|
|
161
|
-
* ```typescript
|
|
162
|
-
* const plugin = {...};
|
|
163
|
-
* amplitude.add(plugin);
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
export const add = returnWrapper(client.add.bind(client));
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Removes a plugin.
|
|
170
|
-
*
|
|
171
|
-
* ```typescript
|
|
172
|
-
* amplitude.remove('myPlugin');
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
|
-
export const remove = returnWrapper(client.remove.bind(client));
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Tracks user-defined event, with specified type, optional event properties and optional overwrites.
|
|
179
|
-
*
|
|
180
|
-
* ```typescript
|
|
181
|
-
* // event tracking with event type only
|
|
182
|
-
* track('Page Load');
|
|
183
|
-
*
|
|
184
|
-
* // event tracking with event type and additional event properties
|
|
185
|
-
* track('Page Load', { loadTime: 1000 });
|
|
186
|
-
*
|
|
187
|
-
* // event tracking with event type, additional event properties, and overwritten event options
|
|
188
|
-
* track('Page Load', { loadTime: 1000 }, { sessionId: -1 });
|
|
189
|
-
*
|
|
190
|
-
* // alternatively, this tracking method is awaitable
|
|
191
|
-
* const result = await track('Page Load').promise;
|
|
192
|
-
* console.log(result.event); // {...}
|
|
193
|
-
* console.log(result.code); // 200
|
|
194
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
export const track = returnWrapper(client.track.bind(client));
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Alias for track()
|
|
201
|
-
*/
|
|
202
|
-
export const logEvent = returnWrapper(client.logEvent.bind(client));
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Sends an identify event containing user property operations
|
|
206
|
-
*
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const id = new Identify();
|
|
209
|
-
* id.set('colors', ['rose', 'gold']);
|
|
210
|
-
* identify(id);
|
|
211
|
-
*
|
|
212
|
-
* // alternatively, this tracking method is awaitable
|
|
213
|
-
* const result = await identify(id).promise;
|
|
214
|
-
* console.log(result.event); // {...}
|
|
215
|
-
* console.log(result.code); // 200
|
|
216
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
219
|
-
export const identify = returnWrapper(client.identify.bind(client));
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Sends a group identify event containing group property operations.
|
|
223
|
-
*
|
|
224
|
-
* ```typescript
|
|
225
|
-
* const id = new Identify();
|
|
226
|
-
* id.set('skills', ['js', 'ts']);
|
|
227
|
-
* const groupType = 'org';
|
|
228
|
-
* const groupName = 'engineering';
|
|
229
|
-
* groupIdentify(groupType, groupName, id);
|
|
230
|
-
*
|
|
231
|
-
* // alternatively, this tracking method is awaitable
|
|
232
|
-
* const result = await groupIdentify(groupType, groupName, id).promise;
|
|
233
|
-
* console.log(result.event); // {...}
|
|
234
|
-
* console.log(result.code); // 200
|
|
235
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
236
|
-
* ```
|
|
237
|
-
*/
|
|
238
|
-
export const groupIdentify = returnWrapper(client.groupIdentify.bind(client));
|
|
239
|
-
export const setGroup = returnWrapper(client.setGroup.bind(client));
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Sends a revenue event containing revenue property operations.
|
|
243
|
-
*
|
|
244
|
-
* ```typescript
|
|
245
|
-
* const rev = new Revenue();
|
|
246
|
-
* rev.setRevenue(100);
|
|
247
|
-
* revenue(rev);
|
|
248
|
-
*
|
|
249
|
-
* // alternatively, this tracking method is awaitable
|
|
250
|
-
* const result = await revenue(rev).promise;
|
|
251
|
-
* console.log(result.event); // {...}
|
|
252
|
-
* console.log(result.code); // 200
|
|
253
|
-
* console.log(result.message); // "Event tracked successfully"
|
|
254
|
-
* ```
|
|
255
|
-
*/
|
|
256
|
-
export const revenue = returnWrapper(client.revenue.bind(client));
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Returns current user ID.
|
|
260
|
-
*
|
|
261
|
-
* ```typescript
|
|
262
|
-
* const userId = getUserId();
|
|
263
|
-
* ```
|
|
264
|
-
*/
|
|
265
|
-
export const getUserId = client.getUserId.bind(client);
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Sets a new user ID.
|
|
269
|
-
*
|
|
270
|
-
* ```typescript
|
|
271
|
-
* setUserId('userId');
|
|
272
|
-
* ```
|
|
273
|
-
*/
|
|
274
|
-
export const setUserId = client.setUserId.bind(client);
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Returns current device ID.
|
|
278
|
-
*
|
|
279
|
-
* ```typescript
|
|
280
|
-
* const deviceId = getDeviceId();
|
|
281
|
-
* ```
|
|
282
|
-
*/
|
|
283
|
-
export const getDeviceId = client.getDeviceId.bind(client);
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Sets a new device ID.
|
|
287
|
-
* When setting a custom device ID, make sure the value is sufficiently unique.
|
|
288
|
-
* A uuid is recommended.
|
|
289
|
-
*
|
|
290
|
-
* ```typescript
|
|
291
|
-
* setDeviceId('deviceId');
|
|
292
|
-
* ```
|
|
293
|
-
*/
|
|
294
|
-
export const setDeviceId = client.setDeviceId.bind(client);
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
|
|
298
|
-
* are doing. This can be used in conjunction with `setUserId(undefined)` to anonymize users after they log out.
|
|
299
|
-
* With an `unefined` userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
|
|
300
|
-
*
|
|
301
|
-
* ```typescript
|
|
302
|
-
* regenerateDeviceId();
|
|
303
|
-
* ```
|
|
304
|
-
*/
|
|
305
|
-
export const regenerateDeviceId = client.regenerateDeviceId.bind(client);
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Returns current session ID.
|
|
309
|
-
*
|
|
310
|
-
* ```typescript
|
|
311
|
-
* const sessionId = getSessionId();
|
|
312
|
-
* ```
|
|
313
|
-
*/
|
|
314
|
-
export const getSessionId = client.getSessionId.bind(client);
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Sets a new session ID.
|
|
318
|
-
* When settign a custom session ID, make sure the value is in milliseconds since epoch (Unix Timestamp).
|
|
319
|
-
*
|
|
320
|
-
* ```typescript
|
|
321
|
-
* setSessionId(Date.now());
|
|
322
|
-
* ```
|
|
323
|
-
*/
|
|
324
|
-
export const setSessionId = client.setSessionId.bind(client);
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Sets a new optOut config value. This toggles event tracking on/off.
|
|
328
|
-
*
|
|
329
|
-
*```typescript
|
|
330
|
-
* // Stops tracking
|
|
331
|
-
* setOptOut(true);
|
|
332
|
-
*
|
|
333
|
-
* // Starts/resumes tracking
|
|
334
|
-
* setOptOut(false);
|
|
335
|
-
* ```
|
|
336
|
-
*/
|
|
337
|
-
export const setOptOut = client.setOptOut.bind(client);
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Sets the network transport type for events.
|
|
341
|
-
*
|
|
342
|
-
* ```typescript
|
|
343
|
-
* // Use Fetch API
|
|
344
|
-
* setTransport('fetch');
|
|
345
|
-
*
|
|
346
|
-
* // Use XMLHttpRequest API
|
|
347
|
-
* setTransport('xhr');
|
|
348
|
-
*
|
|
349
|
-
* // Use navigator.sendBeacon API
|
|
350
|
-
* setTransport('beacon');
|
|
351
|
-
* ```
|
|
352
|
-
*/
|
|
353
|
-
export const setTransport = client.setTransport.bind(client);
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Flush and send all the events which haven't been sent.
|
|
357
|
-
*
|
|
358
|
-
*```typescript
|
|
359
|
-
* // Send all the unsent events
|
|
360
|
-
* flush();
|
|
361
|
-
*
|
|
362
|
-
* // alternatively, this tracking method is awaitable
|
|
363
|
-
* await flush().promise;
|
|
364
|
-
* ```
|
|
365
|
-
*/
|
|
366
|
-
export const flush = returnWrapper(client.flush.bind(client));
|
package/src/session-manager.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { UserSession, Storage, SessionManager as ISessionManager } from '@amplitude/analytics-types';
|
|
2
|
-
import { getCookieName as getStorageKey } from './utils/cookie-name';
|
|
3
|
-
|
|
4
|
-
export class SessionManager implements ISessionManager {
|
|
5
|
-
storageKey: string;
|
|
6
|
-
cache: UserSession;
|
|
7
|
-
isSessionCacheValid = true;
|
|
8
|
-
|
|
9
|
-
constructor(private storage: Storage<UserSession>, apiKey: string) {
|
|
10
|
-
this.storageKey = getStorageKey(apiKey);
|
|
11
|
-
this.cache = { optOut: false };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* load() must be called immediately after instantation
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* await new SessionManager(...).load();
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
async load() {
|
|
22
|
-
this.cache = (await this.storage.get(this.storageKey)) ?? {
|
|
23
|
-
optOut: false,
|
|
24
|
-
};
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
setSession(session: Partial<UserSession>) {
|
|
29
|
-
this.cache = { ...this.cache, ...session };
|
|
30
|
-
void this.storage.set(this.storageKey, this.cache);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getSessionId() {
|
|
34
|
-
this.isSessionCacheValid = true;
|
|
35
|
-
void this.storage.get(this.storageKey).then((userSession) => {
|
|
36
|
-
// Checks if session id has been set since the last get
|
|
37
|
-
if (this.isSessionCacheValid) {
|
|
38
|
-
this.cache.sessionId = userSession?.sessionId;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
return this.cache.sessionId;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
setSessionId(sessionId: number) {
|
|
45
|
-
// Flags session id has been set
|
|
46
|
-
this.isSessionCacheValid = false;
|
|
47
|
-
this.setSession({ sessionId });
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
getDeviceId(): string | undefined {
|
|
51
|
-
return this.cache.deviceId;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
setDeviceId(deviceId: string): void {
|
|
55
|
-
this.setSession({ deviceId });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
getUserId(): string | undefined {
|
|
59
|
-
return this.cache.userId;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
setUserId(userId: string): void {
|
|
63
|
-
this.setSession({ userId });
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getLastEventTime() {
|
|
67
|
-
return this.cache.lastEventTime;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
setLastEventTime(lastEventTime: number) {
|
|
71
|
-
this.setSession({ lastEventTime });
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
getOptOut(): boolean {
|
|
75
|
-
return this.cache.optOut;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
setOptOut(optOut: boolean): void {
|
|
79
|
-
this.setSession({ optOut });
|
|
80
|
-
}
|
|
81
|
-
}
|
package/src/storage/cookie.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { Storage, CookieStorageOptions } from '@amplitude/analytics-types';
|
|
2
|
-
import { isNative } from '../utils/platform';
|
|
3
|
-
|
|
4
|
-
export class CookieStorage<T> implements Storage<T> {
|
|
5
|
-
options: CookieStorageOptions;
|
|
6
|
-
|
|
7
|
-
constructor(options?: CookieStorageOptions) {
|
|
8
|
-
this.options = { ...options };
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async isEnabled(): Promise<boolean> {
|
|
12
|
-
if (isNative()) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
/* istanbul ignore if */
|
|
16
|
-
if (typeof window === 'undefined') {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const random = String(Date.now());
|
|
21
|
-
const testStrorage = new CookieStorage<string>();
|
|
22
|
-
const testKey = 'AMP_TEST';
|
|
23
|
-
try {
|
|
24
|
-
await testStrorage.set(testKey, random);
|
|
25
|
-
const value = await testStrorage.get(testKey);
|
|
26
|
-
return value === random;
|
|
27
|
-
} catch {
|
|
28
|
-
/* istanbul ignore next */
|
|
29
|
-
return false;
|
|
30
|
-
} finally {
|
|
31
|
-
await testStrorage.remove(testKey);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async get(key: string): Promise<T | undefined> {
|
|
36
|
-
let value = await this.getRaw(key);
|
|
37
|
-
if (!value) {
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
try {
|
|
42
|
-
value = decodeURIComponent(atob(value));
|
|
43
|
-
} catch {
|
|
44
|
-
// value not encoded
|
|
45
|
-
}
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
47
|
-
return JSON.parse(value);
|
|
48
|
-
} catch {
|
|
49
|
-
/* istanbul ignore next */
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async getRaw(key: string): Promise<string | undefined> {
|
|
55
|
-
const cookie = window.document.cookie.split('; ');
|
|
56
|
-
const match = cookie.find((c) => c.indexOf(key + '=') === 0);
|
|
57
|
-
if (!match) {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
return match.substring(key.length + 1);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async set(key: string, value: T | null): Promise<void> {
|
|
64
|
-
try {
|
|
65
|
-
const expirationDays = this.options.expirationDays ?? 0;
|
|
66
|
-
const expires = value !== null ? expirationDays : -1;
|
|
67
|
-
let expireDate: Date | undefined = undefined;
|
|
68
|
-
if (expires) {
|
|
69
|
-
const date = new Date();
|
|
70
|
-
date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
|
|
71
|
-
expireDate = date;
|
|
72
|
-
}
|
|
73
|
-
let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;
|
|
74
|
-
if (expireDate) {
|
|
75
|
-
str += `; expires=${expireDate.toUTCString()}`;
|
|
76
|
-
}
|
|
77
|
-
str += '; path=/';
|
|
78
|
-
if (this.options.domain) {
|
|
79
|
-
str += `; domain=${this.options.domain}`;
|
|
80
|
-
}
|
|
81
|
-
if (this.options.secure) {
|
|
82
|
-
str += '; Secure';
|
|
83
|
-
}
|
|
84
|
-
if (this.options.sameSite) {
|
|
85
|
-
str += `; SameSite=${this.options.sameSite}`;
|
|
86
|
-
}
|
|
87
|
-
window.document.cookie = str;
|
|
88
|
-
} catch {
|
|
89
|
-
//
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async remove(key: string): Promise<void> {
|
|
94
|
-
await this.set(key, null);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async reset(): Promise<void> {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { Storage } from '@amplitude/analytics-types';
|
|
2
|
-
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
3
|
-
|
|
4
|
-
export class LocalStorage<T> implements Storage<T> {
|
|
5
|
-
async isEnabled(): Promise<boolean> {
|
|
6
|
-
/* istanbul ignore if */
|
|
7
|
-
if (typeof window === 'undefined') {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const random = String(Date.now());
|
|
12
|
-
const testStorage = new LocalStorage<string>();
|
|
13
|
-
const testKey = 'AMP_TEST';
|
|
14
|
-
try {
|
|
15
|
-
await testStorage.set(testKey, random);
|
|
16
|
-
const value = await testStorage.get(testKey);
|
|
17
|
-
return value === random;
|
|
18
|
-
} catch {
|
|
19
|
-
/* istanbul ignore next */
|
|
20
|
-
return false;
|
|
21
|
-
} finally {
|
|
22
|
-
await testStorage.remove(testKey);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async get(key: string): Promise<T | undefined> {
|
|
27
|
-
try {
|
|
28
|
-
const value = await this.getRaw(key);
|
|
29
|
-
if (!value) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
33
|
-
return JSON.parse(value);
|
|
34
|
-
} catch {
|
|
35
|
-
/* istanbul ignore next */
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async getRaw(key: string): Promise<string | undefined> {
|
|
41
|
-
return (await AsyncStorage.getItem(key)) || undefined;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async set(key: string, value: T): Promise<void> {
|
|
45
|
-
try {
|
|
46
|
-
await AsyncStorage.setItem(key, JSON.stringify(value));
|
|
47
|
-
} catch {
|
|
48
|
-
//
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async remove(key: string): Promise<void> {
|
|
53
|
-
try {
|
|
54
|
-
await AsyncStorage.removeItem(key);
|
|
55
|
-
} catch {
|
|
56
|
-
//
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async reset(): Promise<void> {
|
|
61
|
-
try {
|
|
62
|
-
await AsyncStorage.clear();
|
|
63
|
-
} catch {
|
|
64
|
-
//
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { CookieStorage } from './cookie';
|
|
2
|
-
|
|
3
|
-
export class UTMCookie extends CookieStorage<Record<string, string | undefined>> {
|
|
4
|
-
async get(key: string): Promise<Record<string, string | undefined> | undefined> {
|
|
5
|
-
try {
|
|
6
|
-
const value = await this.getRaw(key);
|
|
7
|
-
if (!value) {
|
|
8
|
-
return undefined;
|
|
9
|
-
}
|
|
10
|
-
const entries = value.split('.').splice(-1)[0].split('|');
|
|
11
|
-
return entries.reduce<Record<string, string | undefined>>((acc, curr) => {
|
|
12
|
-
const [key, value = ''] = curr.split('=', 2);
|
|
13
|
-
if (!value) {
|
|
14
|
-
return acc;
|
|
15
|
-
}
|
|
16
|
-
acc[key] = decodeURIComponent(value);
|
|
17
|
-
return acc;
|
|
18
|
-
}, {});
|
|
19
|
-
} catch {
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async set(): Promise<void> {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
}
|
package/src/transports/fetch.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { BaseTransport } from '@amplitude/analytics-core';
|
|
2
|
-
import { Payload, Response, Transport } from '@amplitude/analytics-types';
|
|
3
|
-
|
|
4
|
-
export class FetchTransport extends BaseTransport implements Transport {
|
|
5
|
-
async send(serverUrl: string, payload: Payload): Promise<Response | null> {
|
|
6
|
-
/* istanbul ignore if */
|
|
7
|
-
if (typeof fetch === 'undefined') {
|
|
8
|
-
throw new Error('FetchTransport is not supported');
|
|
9
|
-
}
|
|
10
|
-
const options: RequestInit = {
|
|
11
|
-
headers: {
|
|
12
|
-
'Content-Type': 'application/json',
|
|
13
|
-
Accept: '*/*',
|
|
14
|
-
},
|
|
15
|
-
body: JSON.stringify(payload),
|
|
16
|
-
method: 'POST',
|
|
17
|
-
};
|
|
18
|
-
const response = await fetch(serverUrl, options);
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
20
|
-
const responsePayload: Record<string, any> = await response.json();
|
|
21
|
-
return this.buildResponse(responsePayload);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { BaseTransport } from '@amplitude/analytics-core';
|
|
2
|
-
import { Payload, Response, Transport } from '@amplitude/analytics-types';
|
|
3
|
-
|
|
4
|
-
export class SendBeaconTransport extends BaseTransport implements Transport {
|
|
5
|
-
async send(serverUrl: string, payload: Payload): Promise<Response | null> {
|
|
6
|
-
return new Promise((resolve, reject) => {
|
|
7
|
-
/* istanbul ignore if */
|
|
8
|
-
if (
|
|
9
|
-
typeof window === 'undefined' ||
|
|
10
|
-
typeof window.navigator === 'undefined' ||
|
|
11
|
-
typeof window.navigator.sendBeacon === 'undefined'
|
|
12
|
-
) {
|
|
13
|
-
throw new Error('SendBeaconTransport is not supported');
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const data = JSON.stringify(payload);
|
|
17
|
-
const success = window.navigator.sendBeacon(serverUrl, JSON.stringify(payload));
|
|
18
|
-
if (success) {
|
|
19
|
-
return resolve(
|
|
20
|
-
this.buildResponse({
|
|
21
|
-
code: 200,
|
|
22
|
-
events_ingested: payload.events.length,
|
|
23
|
-
payload_size_bytes: data.length,
|
|
24
|
-
server_upload_time: Date.now(),
|
|
25
|
-
}),
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
return resolve(this.buildResponse({ code: 500 }));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|