@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,78 +0,0 @@
|
|
|
1
|
-
import { UTMCookie } from '../storage/utm-cookie';
|
|
2
|
-
import { getQueryParams } from '../utils/query-params';
|
|
3
|
-
import {
|
|
4
|
-
UTM_CAMPAIGN,
|
|
5
|
-
UTM_CONTENT,
|
|
6
|
-
UTM_MEDIUM,
|
|
7
|
-
UTM_SOURCE,
|
|
8
|
-
UTM_TERM,
|
|
9
|
-
UTMZ_SOURCE,
|
|
10
|
-
UTMZ_MEDIUM,
|
|
11
|
-
UTMZ_CAMPAIGN,
|
|
12
|
-
UTMZ_TERM,
|
|
13
|
-
UTMZ_CONTENT,
|
|
14
|
-
GCLID,
|
|
15
|
-
FBCLID,
|
|
16
|
-
BASE_CAMPAIGN,
|
|
17
|
-
} from './constants';
|
|
18
|
-
import {
|
|
19
|
-
Campaign,
|
|
20
|
-
CampaignParser as ICampaignParser,
|
|
21
|
-
ClickIdParameters,
|
|
22
|
-
ReferrerParameters,
|
|
23
|
-
UTMParameters,
|
|
24
|
-
} from '@amplitude/analytics-types';
|
|
25
|
-
|
|
26
|
-
export class CampaignParser implements ICampaignParser {
|
|
27
|
-
utmCookieStorage = new UTMCookie();
|
|
28
|
-
|
|
29
|
-
async parse(): Promise<Campaign> {
|
|
30
|
-
return {
|
|
31
|
-
...BASE_CAMPAIGN,
|
|
32
|
-
...(await this.getUtmParam()),
|
|
33
|
-
...this.getReferrer(),
|
|
34
|
-
...this.getClickIds(),
|
|
35
|
-
} as Campaign;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async getUtmParam(): Promise<UTMParameters> {
|
|
39
|
-
const params = getQueryParams();
|
|
40
|
-
const cookies = ((await this.utmCookieStorage.isEnabled()) && (await this.utmCookieStorage.get('__utmz'))) || {};
|
|
41
|
-
|
|
42
|
-
const utmSource = params[UTM_SOURCE] || cookies[UTMZ_SOURCE];
|
|
43
|
-
const utmMedium = params[UTM_MEDIUM] || cookies[UTMZ_MEDIUM];
|
|
44
|
-
const utmCampaign = params[UTM_CAMPAIGN] || cookies[UTMZ_CAMPAIGN];
|
|
45
|
-
const utmTerm = params[UTM_TERM] || cookies[UTMZ_TERM];
|
|
46
|
-
const utmContent = params[UTM_CONTENT] || cookies[UTMZ_CONTENT];
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
utm_source: utmSource,
|
|
50
|
-
utm_medium: utmMedium,
|
|
51
|
-
utm_campaign: utmCampaign,
|
|
52
|
-
utm_term: utmTerm,
|
|
53
|
-
utm_content: utmContent,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
getReferrer(): ReferrerParameters {
|
|
58
|
-
const data: ReferrerParameters = {
|
|
59
|
-
referrer: undefined,
|
|
60
|
-
referring_domain: undefined,
|
|
61
|
-
};
|
|
62
|
-
try {
|
|
63
|
-
data.referrer = document.referrer || undefined;
|
|
64
|
-
data.referring_domain = data.referrer?.split('/')[2] ?? undefined;
|
|
65
|
-
} catch {
|
|
66
|
-
// nothing to track
|
|
67
|
-
}
|
|
68
|
-
return data;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
getClickIds(): ClickIdParameters {
|
|
72
|
-
const params = getQueryParams();
|
|
73
|
-
return {
|
|
74
|
-
[GCLID]: params[GCLID],
|
|
75
|
-
[FBCLID]: params[FBCLID],
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { createIdentifyEvent, Identify } from '@amplitude/analytics-core';
|
|
2
|
-
import {
|
|
3
|
-
Storage,
|
|
4
|
-
Campaign,
|
|
5
|
-
CampaignParser as ICampaignParser,
|
|
6
|
-
CampaignTracker as ICampaignTracker,
|
|
7
|
-
CampaignTrackFunction,
|
|
8
|
-
CampaignTrackerOptions,
|
|
9
|
-
BaseEvent,
|
|
10
|
-
} from '@amplitude/analytics-types';
|
|
11
|
-
import { getCookieName as getStorageKey } from '../utils/cookie-name';
|
|
12
|
-
import { CampaignParser } from './campaign-parser';
|
|
13
|
-
import { BASE_CAMPAIGN, EMPTY_VALUE, MKTG } from './constants';
|
|
14
|
-
|
|
15
|
-
export class CampaignTracker implements ICampaignTracker {
|
|
16
|
-
storage: Storage<Campaign>;
|
|
17
|
-
storageKey: string;
|
|
18
|
-
parser: ICampaignParser;
|
|
19
|
-
track: CampaignTrackFunction;
|
|
20
|
-
onNewCampaign: (campaign: Campaign) => unknown;
|
|
21
|
-
|
|
22
|
-
disabled: boolean;
|
|
23
|
-
trackNewCampaigns: boolean;
|
|
24
|
-
trackPageViews: boolean;
|
|
25
|
-
excludeReferrers: string[];
|
|
26
|
-
initialEmptyValue: string;
|
|
27
|
-
|
|
28
|
-
constructor(apiKey: string, options: CampaignTrackerOptions) {
|
|
29
|
-
this.storage = options.storage;
|
|
30
|
-
this.storageKey = getStorageKey(apiKey, MKTG);
|
|
31
|
-
this.parser = new CampaignParser();
|
|
32
|
-
this.track = options.track;
|
|
33
|
-
this.onNewCampaign = options.onNewCampaign;
|
|
34
|
-
|
|
35
|
-
this.disabled = Boolean(options.disabled);
|
|
36
|
-
this.trackNewCampaigns = Boolean(options.trackNewCampaigns);
|
|
37
|
-
this.trackPageViews = options.trackPageViews === undefined || Boolean(options.trackPageViews);
|
|
38
|
-
this.excludeReferrers = options.excludeReferrers ?? [];
|
|
39
|
-
if (typeof location !== 'undefined') {
|
|
40
|
-
this.excludeReferrers.unshift(location.hostname);
|
|
41
|
-
}
|
|
42
|
-
this.initialEmptyValue = options.initialEmptyValue ?? EMPTY_VALUE;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
isNewCampaign(currentCampaign: Campaign, previousCampaign: Campaign) {
|
|
46
|
-
const isReferrerExcluded = Boolean(
|
|
47
|
-
currentCampaign.referring_domain && this.excludeReferrers.includes(currentCampaign.referring_domain),
|
|
48
|
-
);
|
|
49
|
-
const hasNewUtm =
|
|
50
|
-
previousCampaign.utm_campaign !== currentCampaign.utm_campaign ||
|
|
51
|
-
previousCampaign.utm_source !== currentCampaign.utm_source ||
|
|
52
|
-
previousCampaign.utm_medium !== currentCampaign.utm_medium ||
|
|
53
|
-
previousCampaign.utm_term !== currentCampaign.utm_term ||
|
|
54
|
-
previousCampaign.utm_content !== currentCampaign.utm_content;
|
|
55
|
-
const hasNewReferrer = previousCampaign.referring_domain !== currentCampaign.referring_domain;
|
|
56
|
-
|
|
57
|
-
return !isReferrerExcluded && (hasNewUtm || hasNewReferrer);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async saveCampaignToStorage(campaign: Campaign): Promise<void> {
|
|
61
|
-
await this.storage.set(this.storageKey, campaign);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async getCampaignFromStorage(): Promise<Campaign> {
|
|
65
|
-
return (await this.storage.get(this.storageKey)) || { ...BASE_CAMPAIGN };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
createCampaignEvent(campaign: Campaign) {
|
|
69
|
-
const campaignParameters: Campaign = {
|
|
70
|
-
// This object definition allows undefined keys to be iterated on
|
|
71
|
-
// in .reduce() to build indentify object
|
|
72
|
-
...BASE_CAMPAIGN,
|
|
73
|
-
...campaign,
|
|
74
|
-
};
|
|
75
|
-
const identifyEvent = Object.entries(campaignParameters).reduce((identify, [key, value]) => {
|
|
76
|
-
identify.setOnce(`initial_${key}`, value || this.initialEmptyValue);
|
|
77
|
-
if (value) {
|
|
78
|
-
return identify.set(key, value);
|
|
79
|
-
}
|
|
80
|
-
return identify.unset(key);
|
|
81
|
-
}, new Identify());
|
|
82
|
-
|
|
83
|
-
const pageViewEvent: BaseEvent = {
|
|
84
|
-
event_type: 'Page View',
|
|
85
|
-
event_properties: {
|
|
86
|
-
page_title: /* istanbul ignore next */ (typeof document !== 'undefined' && document.title) || '',
|
|
87
|
-
page_location: /* istanbul ignore next */ (typeof location !== 'undefined' && location.href) || '',
|
|
88
|
-
page_path: /* istanbul ignore next */ (typeof location !== 'undefined' && location.pathname) || '',
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
return {
|
|
92
|
-
...createIdentifyEvent(identifyEvent),
|
|
93
|
-
...(this.trackPageViews && pageViewEvent),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async send(isNewSession: boolean) {
|
|
98
|
-
if (this.disabled) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const currentCampaign = await this.parser.parse();
|
|
102
|
-
const previousCampaign = await this.getCampaignFromStorage();
|
|
103
|
-
if (!isNewSession) {
|
|
104
|
-
if (!this.trackNewCampaigns || !this.isNewCampaign(currentCampaign, previousCampaign)) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
this.onNewCampaign(currentCampaign);
|
|
108
|
-
}
|
|
109
|
-
await this.track(this.createCampaignEvent(currentCampaign));
|
|
110
|
-
await this.saveCampaignToStorage(currentCampaign);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Campaign } from '@amplitude/analytics-types';
|
|
2
|
-
|
|
3
|
-
export const UTM_SOURCE = 'utm_source';
|
|
4
|
-
export const UTM_MEDIUM = 'utm_medium';
|
|
5
|
-
export const UTM_CAMPAIGN = 'utm_campaign';
|
|
6
|
-
export const UTM_TERM = 'utm_term';
|
|
7
|
-
export const UTM_CONTENT = 'utm_content';
|
|
8
|
-
|
|
9
|
-
export const UTMZ_SOURCE = 'utmcsr';
|
|
10
|
-
export const UTMZ_MEDIUM = 'utmcmd';
|
|
11
|
-
export const UTMZ_CAMPAIGN = 'utmccn';
|
|
12
|
-
export const UTMZ_TERM = 'utmctr';
|
|
13
|
-
export const UTMZ_CONTENT = 'utmcct';
|
|
14
|
-
|
|
15
|
-
export const GCLID = 'gclid';
|
|
16
|
-
export const FBCLID = 'fbclid';
|
|
17
|
-
|
|
18
|
-
export const EMPTY_VALUE = 'EMPTY';
|
|
19
|
-
|
|
20
|
-
export const BASE_CAMPAIGN: Campaign = {
|
|
21
|
-
utm_source: undefined,
|
|
22
|
-
utm_medium: undefined,
|
|
23
|
-
utm_campaign: undefined,
|
|
24
|
-
utm_term: undefined,
|
|
25
|
-
utm_content: undefined,
|
|
26
|
-
referrer: undefined,
|
|
27
|
-
referring_domain: undefined,
|
|
28
|
-
gclid: undefined,
|
|
29
|
-
fbclid: undefined,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const MKTG = 'MKTG';
|
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 };
|
package/src/plugins/context.ts
DELETED
|
@@ -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
|
-
}
|