@amplitude/analytics-react-native 0.0.1-dev.0
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 +0 -0
- package/amplitude-react-native.podspec +21 -0
- package/android/build.gradle +61 -0
- package/android/gradle.properties +3 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/amplitude/reactnative/AmplitudeReactNativeModule.kt +36 -0
- package/android/src/main/java/com/amplitude/reactnative/AmplitudeReactNativePackage.java +28 -0
- package/android/src/main/java/com/amplitude/reactnative/AndroidContextProvider.kt +415 -0
- package/android/src/main/java/com/amplitude/reactnative/AndroidLogger.kt +56 -0
- package/android/src/main/java/com/amplitude/reactnative/Utils.kt +34 -0
- package/ios/AmplitudeReactNative-Bridging-Header.h +5 -0
- package/ios/AmplitudeReactNative.m +7 -0
- package/ios/AmplitudeReactNative.swift +29 -0
- package/ios/AmplitudeReactNative.xcodeproj/project.pbxproj +293 -0
- package/ios/AppleContextProvider.swift +219 -0
- package/lib/commonjs/attribution/campaign-parser.js +74 -0
- package/lib/commonjs/attribution/campaign-parser.js.map +1 -0
- package/lib/commonjs/attribution/campaign-tracker.js +133 -0
- package/lib/commonjs/attribution/campaign-tracker.js.map +1 -0
- package/lib/commonjs/attribution/constants.js +47 -0
- package/lib/commonjs/attribution/constants.js.map +1 -0
- package/lib/commonjs/config.js +257 -0
- package/lib/commonjs/config.js.map +1 -0
- package/lib/commonjs/constants.js +31 -0
- package/lib/commonjs/constants.js.map +1 -0
- package/lib/commonjs/cookie-migration/index.js +74 -0
- package/lib/commonjs/cookie-migration/index.js.map +1 -0
- package/lib/commonjs/index.js +141 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/plugins/context.js +136 -0
- package/lib/commonjs/plugins/context.js.map +1 -0
- package/lib/commonjs/react-native-client.js +362 -0
- package/lib/commonjs/react-native-client.js.map +1 -0
- package/lib/commonjs/session-manager.js +114 -0
- package/lib/commonjs/session-manager.js.map +1 -0
- package/lib/commonjs/storage/cookie.js +124 -0
- package/lib/commonjs/storage/cookie.js.map +1 -0
- package/lib/commonjs/storage/local-storage.js +79 -0
- package/lib/commonjs/storage/local-storage.js.map +1 -0
- package/lib/commonjs/storage/utm-cookie.js +42 -0
- package/lib/commonjs/storage/utm-cookie.js.map +1 -0
- package/lib/commonjs/transports/fetch.js +34 -0
- package/lib/commonjs/transports/fetch.js.map +1 -0
- package/lib/commonjs/transports/send-beacon.js +43 -0
- package/lib/commonjs/transports/send-beacon.js.map +1 -0
- package/lib/commonjs/transports/xhr.js +54 -0
- package/lib/commonjs/transports/xhr.js.map +1 -0
- package/lib/commonjs/typings/browser-snippet.d.js +6 -0
- package/lib/commonjs/typings/browser-snippet.d.js.map +1 -0
- package/lib/commonjs/typings/ua-parser.d.js +2 -0
- package/lib/commonjs/typings/ua-parser.d.js.map +1 -0
- package/lib/commonjs/utils/cookie-name.js +23 -0
- package/lib/commonjs/utils/cookie-name.js.map +1 -0
- package/lib/commonjs/utils/language.js +18 -0
- package/lib/commonjs/utils/language.js.map +1 -0
- package/lib/commonjs/utils/platform.js +21 -0
- package/lib/commonjs/utils/platform.js.map +1 -0
- package/lib/commonjs/utils/query-params.js +36 -0
- package/lib/commonjs/utils/query-params.js.map +1 -0
- package/lib/commonjs/utils/snippet-helper.js +56 -0
- package/lib/commonjs/utils/snippet-helper.js.map +1 -0
- package/lib/commonjs/version.js +9 -0
- package/lib/commonjs/version.js.map +1 -0
- package/lib/module/attribution/campaign-parser.js +62 -0
- package/lib/module/attribution/campaign-parser.js.map +1 -0
- package/lib/module/attribution/campaign-tracker.js +120 -0
- package/lib/module/attribution/campaign-tracker.js.map +1 -0
- package/lib/module/attribution/constants.js +26 -0
- package/lib/module/attribution/constants.js.map +1 -0
- package/lib/module/config.js +217 -0
- package/lib/module/config.js.map +1 -0
- package/lib/module/constants.js +13 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/cookie-migration/index.js +56 -0
- package/lib/module/cookie-migration/index.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/plugins/context.js +118 -0
- package/lib/module/plugins/context.js.map +1 -0
- package/lib/module/react-native-client.js +329 -0
- package/lib/module/react-native-client.js.map +1 -0
- package/lib/module/session-manager.js +104 -0
- package/lib/module/session-manager.js.map +1 -0
- package/lib/module/storage/cookie.js +114 -0
- package/lib/module/storage/cookie.js.map +1 -0
- package/lib/module/storage/local-storage.js +67 -0
- package/lib/module/storage/local-storage.js.map +1 -0
- package/lib/module/storage/utm-cookie.js +32 -0
- package/lib/module/storage/utm-cookie.js.map +1 -0
- package/lib/module/transports/fetch.js +24 -0
- package/lib/module/transports/fetch.js.map +1 -0
- package/lib/module/transports/send-beacon.js +33 -0
- package/lib/module/transports/send-beacon.js.map +1 -0
- package/lib/module/transports/xhr.js +44 -0
- package/lib/module/transports/xhr.js.map +1 -0
- package/lib/module/typings/browser-snippet.d.js +2 -0
- package/lib/module/typings/browser-snippet.d.js.map +1 -0
- package/lib/module/typings/ua-parser.d.js +2 -0
- package/lib/module/typings/ua-parser.d.js.map +1 -0
- package/lib/module/utils/cookie-name.js +10 -0
- package/lib/module/utils/cookie-name.js.map +1 -0
- package/lib/module/utils/language.js +9 -0
- package/lib/module/utils/language.js.map +1 -0
- package/lib/module/utils/platform.js +8 -0
- package/lib/module/utils/platform.js.map +1 -0
- package/lib/module/utils/query-params.js +26 -0
- package/lib/module/utils/query-params.js.map +1 -0
- package/lib/module/utils/snippet-helper.js +41 -0
- package/lib/module/utils/snippet-helper.js.map +1 -0
- package/lib/module/version.js +2 -0
- package/lib/module/version.js.map +1 -0
- package/lib/typescript/attribution/campaign-parser.d.ts +10 -0
- package/lib/typescript/attribution/campaign-parser.d.ts.map +1 -0
- package/lib/typescript/attribution/campaign-tracker.d.ts +72 -0
- package/lib/typescript/attribution/campaign-tracker.d.ts.map +1 -0
- package/lib/typescript/attribution/constants.d.ts +17 -0
- package/lib/typescript/attribution/constants.d.ts.map +1 -0
- package/lib/typescript/config.d.ts +90 -0
- package/lib/typescript/config.d.ts.map +1 -0
- package/lib/typescript/constants.d.ts +13 -0
- package/lib/typescript/constants.d.ts.map +1 -0
- package/lib/typescript/cookie-migration/index.d.ts +5 -0
- package/lib/typescript/cookie-migration/index.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +6 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/plugins/context.d.ts +31 -0
- package/lib/typescript/plugins/context.d.ts.map +1 -0
- package/lib/typescript/react-native-client.d.ts +209 -0
- package/lib/typescript/react-native-client.d.ts.map +1 -0
- package/lib/typescript/session-manager.d.ts +28 -0
- package/lib/typescript/session-manager.d.ts.map +1 -0
- package/lib/typescript/storage/cookie.d.ts +12 -0
- package/lib/typescript/storage/cookie.d.ts.map +1 -0
- package/lib/typescript/storage/local-storage.d.ts +10 -0
- package/lib/typescript/storage/local-storage.d.ts.map +1 -0
- package/lib/typescript/storage/utm-cookie.d.ts +6 -0
- package/lib/typescript/storage/utm-cookie.d.ts.map +1 -0
- package/lib/typescript/transports/fetch.d.ts +6 -0
- package/lib/typescript/transports/fetch.d.ts.map +1 -0
- package/lib/typescript/transports/send-beacon.d.ts +6 -0
- package/lib/typescript/transports/send-beacon.d.ts.map +1 -0
- package/lib/typescript/transports/xhr.d.ts +7 -0
- package/lib/typescript/transports/xhr.d.ts.map +1 -0
- package/lib/typescript/utils/cookie-name.d.ts +3 -0
- package/lib/typescript/utils/cookie-name.d.ts.map +1 -0
- package/lib/typescript/utils/language.d.ts +2 -0
- package/lib/typescript/utils/language.d.ts.map +1 -0
- package/lib/typescript/utils/platform.d.ts +3 -0
- package/lib/typescript/utils/platform.d.ts.map +1 -0
- package/lib/typescript/utils/query-params.d.ts +2 -0
- package/lib/typescript/utils/query-params.d.ts.map +1 -0
- package/lib/typescript/utils/snippet-helper.d.ts +16 -0
- package/lib/typescript/utils/snippet-helper.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 +93 -0
- package/src/attribution/campaign-parser.ts +78 -0
- package/src/attribution/campaign-tracker.ts +112 -0
- package/src/attribution/constants.ts +32 -0
- package/src/config.ts +210 -0
- package/src/constants.ts +14 -0
- package/src/cookie-migration/index.ts +54 -0
- package/src/index.ts +23 -0
- package/src/plugins/context.ts +106 -0
- package/src/react-native-client.ts +349 -0
- package/src/session-manager.ts +81 -0
- package/src/storage/cookie.ts +95 -0
- package/src/storage/local-storage.ts +67 -0
- package/src/storage/utm-cookie.ts +27 -0
- package/src/transports/fetch.ts +23 -0
- package/src/transports/send-beacon.ts +34 -0
- package/src/transports/xhr.ts +36 -0
- package/src/typings/browser-snippet.d.ts +7 -0
- package/src/typings/ua-parser.d.ts +4 -0
- package/src/utils/cookie-name.ts +9 -0
- package/src/utils/language.ts +7 -0
- package/src/utils/platform.ts +9 -0
- package/src/utils/query-params.ts +21 -0
- package/src/utils/snippet-helper.ts +35 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { InstanceProxy, QueueProxy } from '@amplitude/analytics-types';
|
|
2
|
+
/**
|
|
3
|
+
* Applies the proxied functions on the proxied amplitude snippet to an instance of the real object.
|
|
4
|
+
* @ignore
|
|
5
|
+
*/
|
|
6
|
+
export declare const runQueuedFunctions: (instance: object, queue: QueueProxy) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Applies the proxied functions on the proxied object to an instance of the real object.
|
|
9
|
+
* Used to convert proxied Identify and Revenue objects.
|
|
10
|
+
*/
|
|
11
|
+
export declare const convertProxyObjectToRealObject: <T>(instance: T, queue: QueueProxy) => T;
|
|
12
|
+
/**
|
|
13
|
+
* Check if the param is snippet proxy
|
|
14
|
+
*/
|
|
15
|
+
export declare const isInstanceProxy: (instance: unknown) => instance is InstanceProxy;
|
|
16
|
+
//# sourceMappingURL=snippet-helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snippet-helper.d.ts","sourceRoot":"","sources":["../../../src/utils/snippet-helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,aAAa,EAAE,UAAU,EAAU,MAAM,4BAA4B,CAAC;AAEhG;;;GAGG;AACH,eAAO,MAAM,kBAAkB,aAAc,MAAM,4BAElD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,8BAA8B,0CAY1C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,aAAc,OAAO,8BAGhD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,gBAAgB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@amplitude/analytics-react-native",
|
|
3
|
+
"version": "0.0.1-dev.0",
|
|
4
|
+
"description": "Official React Native SDK",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"analytics",
|
|
7
|
+
"amplitude",
|
|
8
|
+
"react-native",
|
|
9
|
+
"ios",
|
|
10
|
+
"android"
|
|
11
|
+
],
|
|
12
|
+
"author": "Amplitude Inc",
|
|
13
|
+
"homepage": "https://github.com/amplitude/Amplitude-TypeScript",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"main": "lib/commonjs/index",
|
|
16
|
+
"module": "lib/module/index",
|
|
17
|
+
"types": "lib/typescript/index.d.ts",
|
|
18
|
+
"react-native": "src/index",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/amplitude/Amplitude-TypeScript.git"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "yarn version-file && bob build",
|
|
28
|
+
"fix": "yarn fix:eslint & yarn fix:prettier",
|
|
29
|
+
"fix:eslint": "eslint '{src,test}/**/*.ts' --fix",
|
|
30
|
+
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
|
|
31
|
+
"lint": "yarn lint:eslint & yarn lint:prettier",
|
|
32
|
+
"lint:eslint": "eslint '{src,test}/**/*.ts'",
|
|
33
|
+
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
|
|
34
|
+
"test": "yarn test:web && yarn test:mobile",
|
|
35
|
+
"test:web": "jest --setupFiles './test/mock/setup-web.ts'",
|
|
36
|
+
"test:mobile": "jest --setupFiles './test/mock/setup-mobile.ts'",
|
|
37
|
+
"typecheck": "tsc -p ./tsconfig.json",
|
|
38
|
+
"version": "yarn build",
|
|
39
|
+
"version-file": "node -p \"'export const VERSION = \\'' + require('./package.json').version + '\\';'\" > src/version.ts",
|
|
40
|
+
"typescript": "tsc --noEmit",
|
|
41
|
+
"prepare": "yarn version-file && bob build",
|
|
42
|
+
"example": "yarn --cwd example",
|
|
43
|
+
"pods": "cd example && pod-install --quiet"
|
|
44
|
+
},
|
|
45
|
+
"source": "src/index",
|
|
46
|
+
"files": [
|
|
47
|
+
"src",
|
|
48
|
+
"lib",
|
|
49
|
+
"android",
|
|
50
|
+
"ios",
|
|
51
|
+
"amplitude-react-native.podspec",
|
|
52
|
+
"!lib/typescript/example",
|
|
53
|
+
"!android/build",
|
|
54
|
+
"!ios/build",
|
|
55
|
+
"!**/test"
|
|
56
|
+
],
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/amplitude/Amplitude-TypeScript/issues"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@amplitude/analytics-connector": "1.4.4",
|
|
62
|
+
"@amplitude/analytics-core": "^0.5.0",
|
|
63
|
+
"@amplitude/analytics-types": "^0.4.0",
|
|
64
|
+
"@amplitude/ua-parser-js": "^0.7.31",
|
|
65
|
+
"tslib": "^2.3.1",
|
|
66
|
+
"@react-native-async-storage/async-storage": "^1.17.7"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@types/react": "^16.9.19",
|
|
70
|
+
"@types/react-native": "0.62.13",
|
|
71
|
+
"react": "16.13.1",
|
|
72
|
+
"react-native": "0.63.4",
|
|
73
|
+
"react-native-builder-bob": "^0.17.1"
|
|
74
|
+
},
|
|
75
|
+
"peerDependencies": {
|
|
76
|
+
"react": "*",
|
|
77
|
+
"react-native": "*"
|
|
78
|
+
},
|
|
79
|
+
"react-native-builder-bob": {
|
|
80
|
+
"source": "src",
|
|
81
|
+
"output": "lib",
|
|
82
|
+
"targets": [
|
|
83
|
+
"commonjs",
|
|
84
|
+
"module",
|
|
85
|
+
[
|
|
86
|
+
"typescript",
|
|
87
|
+
{
|
|
88
|
+
"project": "tsconfig.build.json"
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
]
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
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 cookieStorage = await createCookieStorage(options);
|
|
141
|
+
const cookieName = getCookieName(apiKey);
|
|
142
|
+
const cookies = await cookieStorage.get(cookieName);
|
|
143
|
+
const queryParams = getQueryParams();
|
|
144
|
+
const sessionManager = await new SessionManager(cookieStorage, apiKey).load();
|
|
145
|
+
|
|
146
|
+
return new ReactNativeConfig(apiKey, userId ?? cookies?.userId, {
|
|
147
|
+
...options,
|
|
148
|
+
cookieStorage,
|
|
149
|
+
sessionManager,
|
|
150
|
+
deviceId: createDeviceId(cookies?.deviceId, options?.deviceId, queryParams.deviceId),
|
|
151
|
+
optOut: options?.optOut ?? Boolean(cookies?.optOut),
|
|
152
|
+
sessionId: (await cookieStorage.get(cookieName))?.sessionId ?? options?.sessionId,
|
|
153
|
+
storageProvider: await createEventsStorage(options),
|
|
154
|
+
trackingOptions: { ...defaultConfig.trackingOptions, ...options?.trackingOptions },
|
|
155
|
+
transportProvider: options?.transportProvider ?? createTransport(options?.transport),
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const createCookieStorage = async (
|
|
160
|
+
overrides?: ReactNativeOptions,
|
|
161
|
+
baseConfig = getDefaultConfig(),
|
|
162
|
+
): Promise<Storage<UserSession>> => {
|
|
163
|
+
const options = { ...baseConfig, ...overrides };
|
|
164
|
+
const cookieStorage = overrides?.cookieStorage;
|
|
165
|
+
if (!cookieStorage || !(await cookieStorage.isEnabled())) {
|
|
166
|
+
return createFlexibleStorage<UserSession>(options);
|
|
167
|
+
}
|
|
168
|
+
return cookieStorage;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export const createFlexibleStorage = async <T>(options: ReactNativeOptions): Promise<Storage<T>> => {
|
|
172
|
+
let storage: Storage<T> = new CookieStorage({
|
|
173
|
+
domain: options.domain,
|
|
174
|
+
expirationDays: options.cookieExpiration,
|
|
175
|
+
sameSite: options.cookieSameSite,
|
|
176
|
+
secure: options.cookieSecure,
|
|
177
|
+
});
|
|
178
|
+
if (options.disableCookies || !(await storage.isEnabled())) {
|
|
179
|
+
storage = new LocalStorage();
|
|
180
|
+
if (!(await storage.isEnabled())) {
|
|
181
|
+
storage = new MemoryStorage();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return storage;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export const createEventsStorage = async (overrides?: ReactNativeOptions): Promise<Storage<Event[]>> => {
|
|
188
|
+
let eventsStorage = overrides?.storageProvider;
|
|
189
|
+
if (!eventsStorage || !(await eventsStorage.isEnabled())) {
|
|
190
|
+
eventsStorage = new LocalStorage();
|
|
191
|
+
if (!(await eventsStorage.isEnabled())) {
|
|
192
|
+
eventsStorage = new MemoryStorage();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return eventsStorage;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export const createDeviceId = (idFromCookies?: string, idFromOptions?: string, idFromQueryParams?: string) => {
|
|
199
|
+
return idFromOptions || idFromQueryParams || idFromCookies || UUID();
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export const createTransport = (transport?: TransportType) => {
|
|
203
|
+
if (transport === TransportType.XHR) {
|
|
204
|
+
return new XHRTransport();
|
|
205
|
+
}
|
|
206
|
+
if (transport === TransportType.SendBeacon) {
|
|
207
|
+
return new SendBeaconTransport();
|
|
208
|
+
}
|
|
209
|
+
return getDefaultConfig().transportProvider;
|
|
210
|
+
};
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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';
|
|
@@ -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,23 @@
|
|
|
1
|
+
export {
|
|
2
|
+
add,
|
|
3
|
+
getDeviceId,
|
|
4
|
+
getSessionId,
|
|
5
|
+
getUserId,
|
|
6
|
+
groupIdentify,
|
|
7
|
+
identify,
|
|
8
|
+
init,
|
|
9
|
+
logEvent,
|
|
10
|
+
remove,
|
|
11
|
+
revenue,
|
|
12
|
+
setDeviceId,
|
|
13
|
+
setGroup,
|
|
14
|
+
setOptOut,
|
|
15
|
+
setSessionId,
|
|
16
|
+
setTransport,
|
|
17
|
+
setUserId,
|
|
18
|
+
track,
|
|
19
|
+
} from './react-native-client';
|
|
20
|
+
export { runQueuedFunctions } from './utils/snippet-helper';
|
|
21
|
+
export { Revenue, Identify } from '@amplitude/analytics-core';
|
|
22
|
+
import * as Types from '@amplitude/analytics-types';
|
|
23
|
+
export { Types };
|