@amplitude/analytics-react-native 0.0.1-dev.7 → 0.1.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/LICENSE +21 -0
- package/lib/commonjs/index.js +2 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/plugins/identity.js +1 -1
- package/lib/commonjs/plugins/identity.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/index.js +5 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/plugins/identity.js +1 -1
- package/lib/module/plugins/identity.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/index.d.ts +2 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/version.d.ts +1 -1
- package/lib/typescript/version.d.ts.map +1 -1
- package/package.json +8 -7
- 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 +227 -0
- package/src/cookie-migration/index.ts +54 -0
- package/src/index.ts +24 -0
- package/src/plugins/context.ts +106 -0
- package/src/plugins/identity.ts +21 -0
- package/src/react-native-client.ts +332 -0
- package/src/session-manager.ts +81 -0
- package/src/storage/cookie.ts +97 -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/typings/browser-snippet.d.ts +7 -0
- package/src/typings/ua-parser.d.ts +4 -0
- package/src/utils/analytics-connector.ts +5 -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 +18 -0
- package/src/version.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Amplitude Analytics
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/commonjs/index.js
CHANGED
|
@@ -123,9 +123,9 @@ var _reactNativeClient = require("./react-native-client");
|
|
|
123
123
|
|
|
124
124
|
var _analyticsCore = require("@amplitude/analytics-core");
|
|
125
125
|
|
|
126
|
-
var
|
|
126
|
+
var Types = _interopRequireWildcard(require("@amplitude/analytics-types"));
|
|
127
127
|
|
|
128
|
-
exports.Types =
|
|
128
|
+
exports.Types = Types;
|
|
129
129
|
|
|
130
130
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
131
131
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export {\n add,\n flush,\n getDeviceId,\n getSessionId,\n getUserId,\n groupIdentify,\n identify,\n init,\n logEvent,\n remove,\n revenue,\n setDeviceId,\n setGroup,\n setOptOut,\n setSessionId,\n setUserId,\n track,\n} from './react-native-client';\nexport { Revenue, Identify } from '@amplitude/analytics-core';\
|
|
1
|
+
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export {\n add,\n flush,\n getDeviceId,\n getSessionId,\n getUserId,\n groupIdentify,\n identify,\n init,\n logEvent,\n remove,\n revenue,\n setDeviceId,\n setGroup,\n setOptOut,\n setSessionId,\n setUserId,\n track,\n} from './react-native-client';\nexport { Revenue, Identify } from '@amplitude/analytics-core';\n// Hack - react-native apps have trouble with:\n// export * as Types from '@amplitude/analytics-types\nimport * as Types from '@amplitude/analytics-types';\nexport { Types };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AAmBA;;AAGA"}
|
|
@@ -24,7 +24,7 @@ class IdentityEventSender {
|
|
|
24
24
|
const userProperties = context.user_properties;
|
|
25
25
|
|
|
26
26
|
if (userProperties) {
|
|
27
|
-
this.identityStore.editIdentity().
|
|
27
|
+
this.identityStore.editIdentity().updateUserProperties(userProperties).commit();
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
return context;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["IdentityEventSender","PluginType","BEFORE","getAnalyticsConnector","identityStore","execute","context","userProperties","user_properties","editIdentity","
|
|
1
|
+
{"version":3,"names":["IdentityEventSender","PluginType","BEFORE","getAnalyticsConnector","identityStore","execute","context","userProperties","user_properties","editIdentity","updateUserProperties","commit","setup","_","Promise","resolve","undefined"],"sources":["identity.ts"],"sourcesContent":["import { BeforePlugin, PluginType, Event, Config } from '@amplitude/analytics-types';\nimport { getAnalyticsConnector } from '../utils/analytics-connector';\n\nexport class IdentityEventSender implements BeforePlugin {\n name = 'identity';\n type = PluginType.BEFORE as const;\n\n identityStore = getAnalyticsConnector().identityStore;\n\n async execute(context: Event): Promise<Event> {\n const userProperties = context.user_properties as Record<string, any>;\n if (userProperties) {\n this.identityStore.editIdentity().updateUserProperties(userProperties).commit();\n }\n return context;\n }\n\n setup(_: Config): Promise<undefined> {\n return Promise.resolve(undefined);\n }\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEO,MAAMA,mBAAN,CAAkD;EAAA;IAAA,8BAChD,UADgD;;IAAA,8BAEhDC,0BAAA,CAAWC,MAFqC;;IAAA,uCAIvC,IAAAC,yCAAA,IAAwBC,aAJe;EAAA;;EAM1C,MAAPC,OAAO,CAACC,OAAD,EAAiC;IAC5C,MAAMC,cAAc,GAAGD,OAAO,CAACE,eAA/B;;IACA,IAAID,cAAJ,EAAoB;MAClB,KAAKH,aAAL,CAAmBK,YAAnB,GAAkCC,oBAAlC,CAAuDH,cAAvD,EAAuEI,MAAvE;IACD;;IACD,OAAOL,OAAP;EACD;;EAEDM,KAAK,CAACC,CAAD,EAAgC;IACnC,OAAOC,OAAO,CAACC,OAAR,CAAgBC,SAAhB,CAAP;EACD;;AAhBsD"}
|
package/lib/commonjs/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["VERSION"],"sources":["version.ts"],"sourcesContent":["export const VERSION = '0.
|
|
1
|
+
{"version":3,"names":["VERSION"],"sources":["version.ts"],"sourcesContent":["export const VERSION = '0.1.0';\n"],"mappings":";;;;;;AAAO,MAAMA,OAAO,GAAG,OAAhB"}
|
package/lib/module/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { add, flush, getDeviceId, getSessionId, getUserId, groupIdentify, identify, init, logEvent, remove, revenue, setDeviceId, setGroup, setOptOut, setSessionId, setUserId, track } from './react-native-client';
|
|
2
|
-
export { Revenue, Identify } from '@amplitude/analytics-core';
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
export { Revenue, Identify } from '@amplitude/analytics-core'; // Hack - react-native apps have trouble with:
|
|
3
|
+
// export * as Types from '@amplitude/analytics-types
|
|
4
|
+
|
|
5
|
+
import * as Types from '@amplitude/analytics-types';
|
|
6
|
+
export { Types };
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["add","flush","getDeviceId","getSessionId","getUserId","groupIdentify","identify","init","logEvent","remove","revenue","setDeviceId","setGroup","setOptOut","setSessionId","setUserId","track","Revenue","Identify","Types"],"sources":["index.ts"],"sourcesContent":["export {\n add,\n flush,\n getDeviceId,\n getSessionId,\n getUserId,\n groupIdentify,\n identify,\n init,\n logEvent,\n remove,\n revenue,\n setDeviceId,\n setGroup,\n setOptOut,\n setSessionId,\n setUserId,\n track,\n} from './react-native-client';\nexport { Revenue, Identify } from '@amplitude/analytics-core';\
|
|
1
|
+
{"version":3,"names":["add","flush","getDeviceId","getSessionId","getUserId","groupIdentify","identify","init","logEvent","remove","revenue","setDeviceId","setGroup","setOptOut","setSessionId","setUserId","track","Revenue","Identify","Types"],"sources":["index.ts"],"sourcesContent":["export {\n add,\n flush,\n getDeviceId,\n getSessionId,\n getUserId,\n groupIdentify,\n identify,\n init,\n logEvent,\n remove,\n revenue,\n setDeviceId,\n setGroup,\n setOptOut,\n setSessionId,\n setUserId,\n track,\n} from './react-native-client';\nexport { Revenue, Identify } from '@amplitude/analytics-core';\n// Hack - react-native apps have trouble with:\n// export * as Types from '@amplitude/analytics-types\nimport * as Types from '@amplitude/analytics-types';\nexport { Types };\n"],"mappings":"AAAA,SACEA,GADF,EAEEC,KAFF,EAGEC,WAHF,EAIEC,YAJF,EAKEC,SALF,EAMEC,aANF,EAOEC,QAPF,EAQEC,IARF,EASEC,QATF,EAUEC,MAVF,EAWEC,OAXF,EAYEC,WAZF,EAaEC,QAbF,EAcEC,SAdF,EAeEC,YAfF,EAgBEC,SAhBF,EAiBEC,KAjBF,QAkBO,uBAlBP;AAmBA,SAASC,OAAT,EAAkBC,QAAlB,QAAkC,2BAAlC,C,CACA;AACA;;AACA,OAAO,KAAKC,KAAZ,MAAuB,4BAAvB;AACA,SAASA,KAAT"}
|
|
@@ -15,7 +15,7 @@ export class IdentityEventSender {
|
|
|
15
15
|
const userProperties = context.user_properties;
|
|
16
16
|
|
|
17
17
|
if (userProperties) {
|
|
18
|
-
this.identityStore.editIdentity().
|
|
18
|
+
this.identityStore.editIdentity().updateUserProperties(userProperties).commit();
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
return context;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["PluginType","getAnalyticsConnector","IdentityEventSender","BEFORE","identityStore","execute","context","userProperties","user_properties","editIdentity","
|
|
1
|
+
{"version":3,"names":["PluginType","getAnalyticsConnector","IdentityEventSender","BEFORE","identityStore","execute","context","userProperties","user_properties","editIdentity","updateUserProperties","commit","setup","_","Promise","resolve","undefined"],"sources":["identity.ts"],"sourcesContent":["import { BeforePlugin, PluginType, Event, Config } from '@amplitude/analytics-types';\nimport { getAnalyticsConnector } from '../utils/analytics-connector';\n\nexport class IdentityEventSender implements BeforePlugin {\n name = 'identity';\n type = PluginType.BEFORE as const;\n\n identityStore = getAnalyticsConnector().identityStore;\n\n async execute(context: Event): Promise<Event> {\n const userProperties = context.user_properties as Record<string, any>;\n if (userProperties) {\n this.identityStore.editIdentity().updateUserProperties(userProperties).commit();\n }\n return context;\n }\n\n setup(_: Config): Promise<undefined> {\n return Promise.resolve(undefined);\n }\n}\n"],"mappings":";;AAAA,SAAuBA,UAAvB,QAAwD,4BAAxD;AACA,SAASC,qBAAT,QAAsC,8BAAtC;AAEA,OAAO,MAAMC,mBAAN,CAAkD;EAAA;IAAA,8BAChD,UADgD;;IAAA,8BAEhDF,UAAU,CAACG,MAFqC;;IAAA,uCAIvCF,qBAAqB,GAAGG,aAJe;EAAA;;EAM1C,MAAPC,OAAO,CAACC,OAAD,EAAiC;IAC5C,MAAMC,cAAc,GAAGD,OAAO,CAACE,eAA/B;;IACA,IAAID,cAAJ,EAAoB;MAClB,KAAKH,aAAL,CAAmBK,YAAnB,GAAkCC,oBAAlC,CAAuDH,cAAvD,EAAuEI,MAAvE;IACD;;IACD,OAAOL,OAAP;EACD;;EAEDM,KAAK,CAACC,CAAD,EAAgC;IACnC,OAAOC,OAAO,CAACC,OAAR,CAAgBC,SAAhB,CAAP;EACD;;AAhBsD"}
|
package/lib/module/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.1.0';
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["VERSION"],"sources":["version.ts"],"sourcesContent":["export const VERSION = '0.
|
|
1
|
+
{"version":3,"names":["VERSION"],"sources":["version.ts"],"sourcesContent":["export const VERSION = '0.1.0';\n"],"mappings":"AAAA,OAAO,MAAMA,OAAO,GAAG,OAAhB"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { add, flush, getDeviceId, getSessionId, getUserId, groupIdentify, identify, init, logEvent, remove, revenue, setDeviceId, setGroup, setOptOut, setSessionId, setUserId, track, } from './react-native-client';
|
|
2
2
|
export { Revenue, Identify } from '@amplitude/analytics-core';
|
|
3
|
-
|
|
3
|
+
import * as Types from '@amplitude/analytics-types';
|
|
4
|
+
export { Types };
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,KAAK,EACL,WAAW,EACX,YAAY,EACZ,SAAS,EACT,aAAa,EACb,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,MAAM,EACN,OAAO,EACP,WAAW,EACX,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,SAAS,EACT,KAAK,GACN,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,KAAK,EACL,WAAW,EACX,YAAY,EACZ,SAAS,EACT,aAAa,EACb,QAAQ,EACR,IAAI,EACJ,QAAQ,EACR,MAAM,EACN,OAAO,EACP,WAAW,EACX,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,SAAS,EACT,KAAK,GACN,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAG9D,OAAO,KAAK,KAAK,MAAM,4BAA4B,CAAC;AACpD,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.1.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amplitude/analytics-react-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Official React Native SDK",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"analytics",
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
},
|
|
45
45
|
"source": "src/index",
|
|
46
46
|
"files": [
|
|
47
|
+
"src",
|
|
47
48
|
"lib",
|
|
48
49
|
"android",
|
|
49
50
|
"ios",
|
|
50
51
|
"amplitude-react-native.podspec",
|
|
51
|
-
"!lib/typescript/example",
|
|
52
52
|
"!android/build",
|
|
53
53
|
"!ios/build",
|
|
54
54
|
"!**/test"
|
|
@@ -58,11 +58,11 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@amplitude/analytics-connector": "1.4.4",
|
|
61
|
-
"@amplitude/analytics-core": "^0.6.
|
|
62
|
-
"@amplitude/analytics-types": "^0.
|
|
61
|
+
"@amplitude/analytics-core": "^0.6.3",
|
|
62
|
+
"@amplitude/analytics-types": "^0.6.0",
|
|
63
63
|
"@amplitude/ua-parser-js": "^0.7.31",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
64
|
+
"@react-native-async-storage/async-storage": "^1.17.7",
|
|
65
|
+
"tslib": "^2.3.1"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@types/react": "^16.9.19",
|
|
@@ -88,5 +88,6 @@
|
|
|
88
88
|
}
|
|
89
89
|
]
|
|
90
90
|
]
|
|
91
|
-
}
|
|
91
|
+
},
|
|
92
|
+
"gitHead": "ccece5d22caa0ae00b64cece3165ebc0a81dfc93"
|
|
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 = 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,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Event,
|
|
3
|
+
ReactNativeOptions,
|
|
4
|
+
ReactNativeConfig as IReactNativeConfig,
|
|
5
|
+
Storage,
|
|
6
|
+
TrackingOptions,
|
|
7
|
+
UserSession,
|
|
8
|
+
SessionManager as ISessionManager,
|
|
9
|
+
} from '@amplitude/analytics-types';
|
|
10
|
+
import { Config, MemoryStorage, UUID } from '@amplitude/analytics-core';
|
|
11
|
+
|
|
12
|
+
import { CookieStorage } from './storage/cookie';
|
|
13
|
+
import { FetchTransport } from './transports/fetch';
|
|
14
|
+
import { LocalStorage } from './storage/local-storage';
|
|
15
|
+
import { getCookieName } from './utils/cookie-name';
|
|
16
|
+
import { getQueryParams } from './utils/query-params';
|
|
17
|
+
import { SessionManager } from './session-manager';
|
|
18
|
+
|
|
19
|
+
export const getDefaultConfig = () => {
|
|
20
|
+
const cookieStorage = new MemoryStorage<UserSession>();
|
|
21
|
+
return {
|
|
22
|
+
cookieExpiration: 365,
|
|
23
|
+
cookieSameSite: 'Lax',
|
|
24
|
+
cookieSecure: false,
|
|
25
|
+
cookieStorage,
|
|
26
|
+
disableCookies: false,
|
|
27
|
+
domain: '',
|
|
28
|
+
sessionManager: new SessionManager(cookieStorage, ''),
|
|
29
|
+
sessionTimeout: 30 * 60 * 1000,
|
|
30
|
+
storageProvider: new MemoryStorage<Event[]>(),
|
|
31
|
+
trackingOptions: {
|
|
32
|
+
city: true,
|
|
33
|
+
country: true,
|
|
34
|
+
carrier: true,
|
|
35
|
+
deviceManufacturer: true,
|
|
36
|
+
deviceModel: true,
|
|
37
|
+
dma: true,
|
|
38
|
+
ipAddress: true,
|
|
39
|
+
language: true,
|
|
40
|
+
osName: true,
|
|
41
|
+
osVersion: true,
|
|
42
|
+
platform: true,
|
|
43
|
+
region: true,
|
|
44
|
+
versionName: true,
|
|
45
|
+
},
|
|
46
|
+
transportProvider: new FetchTransport(),
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export class ReactNativeConfig extends Config implements IReactNativeConfig {
|
|
51
|
+
appVersion?: string;
|
|
52
|
+
cookieExpiration: number;
|
|
53
|
+
cookieSameSite: string;
|
|
54
|
+
cookieSecure: boolean;
|
|
55
|
+
cookieStorage: Storage<UserSession>;
|
|
56
|
+
disableCookies: boolean;
|
|
57
|
+
domain: string;
|
|
58
|
+
partnerId?: string;
|
|
59
|
+
sessionTimeout: number;
|
|
60
|
+
trackingOptions: TrackingOptions;
|
|
61
|
+
sessionManager: ISessionManager;
|
|
62
|
+
|
|
63
|
+
constructor(apiKey: string, userId?: string, options?: ReactNativeOptions) {
|
|
64
|
+
const defaultConfig = getDefaultConfig();
|
|
65
|
+
super({
|
|
66
|
+
...options,
|
|
67
|
+
apiKey,
|
|
68
|
+
storageProvider: options?.storageProvider ?? defaultConfig.storageProvider,
|
|
69
|
+
transportProvider: options?.transportProvider ?? defaultConfig.transportProvider,
|
|
70
|
+
});
|
|
71
|
+
this.cookieStorage = options?.cookieStorage ?? defaultConfig.cookieStorage;
|
|
72
|
+
this.sessionManager = options?.sessionManager ?? defaultConfig.sessionManager;
|
|
73
|
+
this.sessionTimeout = options?.sessionTimeout ?? defaultConfig.sessionTimeout;
|
|
74
|
+
|
|
75
|
+
this.appVersion = options?.appVersion;
|
|
76
|
+
this.cookieExpiration = options?.cookieExpiration ?? defaultConfig.cookieExpiration;
|
|
77
|
+
this.cookieSameSite = options?.cookieSameSite ?? defaultConfig.cookieSameSite;
|
|
78
|
+
this.cookieSecure = options?.cookieSecure ?? defaultConfig.cookieSecure;
|
|
79
|
+
this.deviceId = options?.deviceId;
|
|
80
|
+
this.disableCookies = options?.disableCookies ?? defaultConfig.disableCookies;
|
|
81
|
+
this.domain = options?.domain ?? defaultConfig.domain;
|
|
82
|
+
this.lastEventTime = this.lastEventTime ?? options?.lastEventTime;
|
|
83
|
+
this.optOut = Boolean(options?.optOut);
|
|
84
|
+
this.partnerId = options?.partnerId;
|
|
85
|
+
this.sessionId = options?.sessionId;
|
|
86
|
+
this.trackingOptions = options?.trackingOptions ?? defaultConfig.trackingOptions;
|
|
87
|
+
this.userId = userId;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get deviceId() {
|
|
91
|
+
return this.sessionManager.getDeviceId();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
set deviceId(deviceId: string | undefined) {
|
|
95
|
+
this.sessionManager.setDeviceId(deviceId);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
get userId() {
|
|
99
|
+
return this.sessionManager.getUserId();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
set userId(userId: string | undefined) {
|
|
103
|
+
this.sessionManager.setUserId(userId);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get sessionId() {
|
|
107
|
+
return this.sessionManager.getSessionId();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
set sessionId(sessionId: number | undefined) {
|
|
111
|
+
this.sessionManager.setSessionId(sessionId);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get optOut() {
|
|
115
|
+
return this.sessionManager.getOptOut();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
set optOut(optOut: boolean) {
|
|
119
|
+
this.sessionManager?.setOptOut(Boolean(optOut));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get lastEventTime() {
|
|
123
|
+
return this.sessionManager.getLastEventTime();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
set lastEventTime(lastEventTime: number | undefined) {
|
|
127
|
+
this.sessionManager.setLastEventTime(lastEventTime);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const useReactNativeConfig = async (
|
|
132
|
+
apiKey: string,
|
|
133
|
+
userId?: string,
|
|
134
|
+
options?: ReactNativeOptions,
|
|
135
|
+
): Promise<IReactNativeConfig> => {
|
|
136
|
+
const defaultConfig = getDefaultConfig();
|
|
137
|
+
const domain = options?.domain ?? (await getTopLevelDomain());
|
|
138
|
+
const cookieStorage = await createCookieStorage({ ...options, domain });
|
|
139
|
+
const cookieName = getCookieName(apiKey);
|
|
140
|
+
const cookies = await cookieStorage.get(cookieName);
|
|
141
|
+
const queryParams = getQueryParams();
|
|
142
|
+
const sessionManager = await new SessionManager(cookieStorage, apiKey).load();
|
|
143
|
+
|
|
144
|
+
return new ReactNativeConfig(apiKey, userId ?? cookies?.userId, {
|
|
145
|
+
...options,
|
|
146
|
+
cookieStorage,
|
|
147
|
+
sessionManager,
|
|
148
|
+
deviceId: createDeviceId(cookies?.deviceId, options?.deviceId, queryParams.deviceId),
|
|
149
|
+
domain,
|
|
150
|
+
optOut: options?.optOut ?? Boolean(cookies?.optOut),
|
|
151
|
+
sessionId: (await cookieStorage.get(cookieName))?.sessionId ?? options?.sessionId,
|
|
152
|
+
storageProvider: await createEventsStorage(options),
|
|
153
|
+
trackingOptions: { ...defaultConfig.trackingOptions, ...options?.trackingOptions },
|
|
154
|
+
transportProvider: options?.transportProvider ?? new FetchTransport(),
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export const createCookieStorage = async (
|
|
159
|
+
overrides?: ReactNativeOptions,
|
|
160
|
+
baseConfig = getDefaultConfig(),
|
|
161
|
+
): Promise<Storage<UserSession>> => {
|
|
162
|
+
const options = { ...baseConfig, ...overrides };
|
|
163
|
+
const cookieStorage = overrides?.cookieStorage;
|
|
164
|
+
if (!cookieStorage || !(await cookieStorage.isEnabled())) {
|
|
165
|
+
return createFlexibleStorage<UserSession>(options);
|
|
166
|
+
}
|
|
167
|
+
return cookieStorage;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const createFlexibleStorage = async <T>(options: ReactNativeOptions): Promise<Storage<T>> => {
|
|
171
|
+
let storage: Storage<T> = new CookieStorage({
|
|
172
|
+
domain: options.domain,
|
|
173
|
+
expirationDays: options.cookieExpiration,
|
|
174
|
+
sameSite: options.cookieSameSite,
|
|
175
|
+
secure: options.cookieSecure,
|
|
176
|
+
});
|
|
177
|
+
if (options.disableCookies || !(await storage.isEnabled())) {
|
|
178
|
+
storage = new LocalStorage();
|
|
179
|
+
if (!(await storage.isEnabled())) {
|
|
180
|
+
storage = new MemoryStorage();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return storage;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export const createEventsStorage = async (overrides?: ReactNativeOptions): Promise<Storage<Event[]>> => {
|
|
187
|
+
let eventsStorage = overrides?.storageProvider;
|
|
188
|
+
if (!eventsStorage || !(await eventsStorage.isEnabled())) {
|
|
189
|
+
eventsStorage = new LocalStorage();
|
|
190
|
+
if (!(await eventsStorage.isEnabled())) {
|
|
191
|
+
eventsStorage = new MemoryStorage();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return eventsStorage;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
export const createDeviceId = (idFromCookies?: string, idFromOptions?: string, idFromQueryParams?: string) => {
|
|
198
|
+
return idFromOptions || idFromQueryParams || idFromCookies || UUID();
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export const getTopLevelDomain = async (url?: string) => {
|
|
202
|
+
if (!(await new CookieStorage<string>().isEnabled()) || (!url && typeof location === 'undefined')) {
|
|
203
|
+
return '';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const host = url ?? location.hostname;
|
|
207
|
+
const parts = host.split('.');
|
|
208
|
+
const levels = [];
|
|
209
|
+
const storageKey = 'AMP_TLDTEST';
|
|
210
|
+
|
|
211
|
+
for (let i = parts.length - 2; i >= 0; --i) {
|
|
212
|
+
levels.push(parts.slice(i).join('.'));
|
|
213
|
+
}
|
|
214
|
+
for (let i = 0; i < levels.length; i++) {
|
|
215
|
+
const domain = levels[i];
|
|
216
|
+
const options = { domain: '.' + domain };
|
|
217
|
+
const storage = new CookieStorage<number>(options);
|
|
218
|
+
await storage.set(storageKey, 1);
|
|
219
|
+
const value = await storage.get(storageKey);
|
|
220
|
+
if (value) {
|
|
221
|
+
await storage.remove(storageKey);
|
|
222
|
+
return '.' + domain;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return '';
|
|
227
|
+
};
|