@datacanva/react-native-tracker 0.1.1
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 +20 -0
- package/README.md +33 -0
- package/lib/module/getDeviceInfo.js +26 -0
- package/lib/module/getDeviceInfo.js.map +1 -0
- package/lib/module/getLanguages.js +17 -0
- package/lib/module/getLanguages.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/tracker.js +60 -0
- package/lib/module/tracker.js.map +1 -0
- package/lib/module/useAppStateTracker.js +42 -0
- package/lib/module/useAppStateTracker.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/getDeviceInfo.d.ts +5 -0
- package/lib/typescript/src/getDeviceInfo.d.ts.map +1 -0
- package/lib/typescript/src/getLanguages.d.ts +3 -0
- package/lib/typescript/src/getLanguages.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/tracker.d.ts +26 -0
- package/lib/typescript/src/tracker.d.ts.map +1 -0
- package/lib/typescript/src/useAppStateTracker.d.ts +8 -0
- package/lib/typescript/src/useAppStateTracker.d.ts.map +1 -0
- package/package.json +160 -0
- package/src/getDeviceInfo.ts +49 -0
- package/src/getLanguages.ts +14 -0
- package/src/index.ts +2 -0
- package/src/tracker.ts +82 -0
- package/src/useAppStateTracker.ts +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lively Impact Technology Limited
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @datacanva/react-native-tracker
|
|
2
|
+
|
|
3
|
+
Datacanva web tracker API for react native.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @datacanva/react-native-tracker
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { multiply } from "@datacanva/react-native-tracker";
|
|
15
|
+
|
|
16
|
+
// ...
|
|
17
|
+
|
|
18
|
+
const result = await multiply(3, 7);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Contributing
|
|
22
|
+
|
|
23
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
24
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
25
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
MIT
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const getDeviceInfo = async exclude => {
|
|
4
|
+
try {
|
|
5
|
+
const entry = async (key, getter) => {
|
|
6
|
+
if (!exclude?.includes(key)) try {
|
|
7
|
+
return [key, `${await getter()}`];
|
|
8
|
+
} catch (e) {}
|
|
9
|
+
return ["", ""];
|
|
10
|
+
};
|
|
11
|
+
const {
|
|
12
|
+
default: DeviceInfo
|
|
13
|
+
} = await import("react-native-device-info");
|
|
14
|
+
const entries = await Promise.all([
|
|
15
|
+
// Identifiers
|
|
16
|
+
entry("mobile_unique_id", DeviceInfo.getUniqueId), entry("mobile_android_id", DeviceInfo.getAndroidId), entry("mobile_android_app_set_id", async () => (await DeviceInfo.getAppSetId()).id),
|
|
17
|
+
// Device/system info
|
|
18
|
+
entry("mobile_device_id", DeviceInfo.getDeviceId), entry("mobile_device_type", DeviceInfo.getDeviceType), entry("mobile_manufacturer", DeviceInfo.getManufacturer), entry("mobile_brand", DeviceInfo.getBrand), entry("mobile_model", DeviceInfo.getModel), entry("mobile_system_name", DeviceInfo.getSystemName), entry("mobile_system_version", DeviceInfo.getSystemVersion), entry("mobile_system_build_id", DeviceInfo.getBuildId), entry("mobile_is_emulator", DeviceInfo.isEmulator), entry("mobile_carrier", DeviceInfo.getCarrier),
|
|
19
|
+
// Application info
|
|
20
|
+
entry("mobile_app_bundle_id", DeviceInfo.getBundleId), entry("mobile_app_version", DeviceInfo.getVersion), entry("mobile_app_build_number", DeviceInfo.getBuildNumber), entry("mobile_app_readable_version", DeviceInfo.getReadableVersion), entry("mobile_app_installer_package", DeviceInfo.getInstallerPackageName), entry("mobile_app_instance_id", DeviceInfo.getInstanceId), entry("mobile_app_update_time", DeviceInfo.getLastUpdateTime)]);
|
|
21
|
+
return Object.fromEntries(entries.filter(e => e[0] && !exclude?.includes(e[0])));
|
|
22
|
+
} catch (e) {}
|
|
23
|
+
return {};
|
|
24
|
+
};
|
|
25
|
+
export default getDeviceInfo;
|
|
26
|
+
//# sourceMappingURL=getDeviceInfo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getDeviceInfo","exclude","entry","key","getter","includes","e","default","DeviceInfo","entries","Promise","all","getUniqueId","getAndroidId","getAppSetId","id","getDeviceId","getDeviceType","getManufacturer","getBrand","getModel","getSystemName","getSystemVersion","getBuildId","isEmulator","getCarrier","getBundleId","getVersion","getBuildNumber","getReadableVersion","getInstallerPackageName","getInstanceId","getLastUpdateTime","Object","fromEntries","filter"],"sourceRoot":"..\\..\\src","sources":["getDeviceInfo.ts"],"mappings":";;AAEA,MAAMA,aAAa,GAAG,MAAOC,OAAkB,IAAK;EAClD,IAAI;IACF,MAAMC,KAAK,GAAG,MAAAA,CAAOC,GAAW,EAAEC,MAA4B,KAAK;MACjE,IAAI,CAACH,OAAO,EAAEI,QAAQ,CAACF,GAAG,CAAC,EACzB,IAAI;QACF,OAAO,CAACA,GAAG,EAAE,GAAG,MAAMC,MAAM,CAAC,CAAC,EAAE,CAAC;MACnC,CAAC,CAAC,OAAOE,CAAC,EAAE,CAAC;MACf,OAAO,CAAC,EAAE,EAAE,EAAE,CAAC;IACjB,CAAC;IAED,MAAM;MAAEC,OAAO,EAAEC;IAAW,CAAC,GAAG,MAAM,MAAM,CAAC,0BAA0B,CAAC;IACxE,MAAMC,OAAO,GAAG,MAAMC,OAAO,CAACC,GAAG,CAAC;IAChC;IACAT,KAAK,CAAC,kBAAkB,EAAEM,UAAU,CAACI,WAAW,CAAC,EACjDV,KAAK,CAAC,mBAAmB,EAAEM,UAAU,CAACK,YAAY,CAAC,EACnDX,KAAK,CACH,2BAA2B,EAC3B,YAAY,CAAC,MAAMM,UAAU,CAACM,WAAW,CAAC,CAAC,EAAEC,EAC/C,CAAC;IACD;IACAb,KAAK,CAAC,kBA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AC,CAAC,OAAOA,CAAC,EAAE,CAAC;EACb,OAAO,CAAC,CAAC;AACX,CAAC;AAED,eAAeN,aAAa","ignoreList":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const getLanguages = async () => {
|
|
4
|
+
try {
|
|
5
|
+
const {
|
|
6
|
+
getLocales
|
|
7
|
+
} = await import("react-native-localize");
|
|
8
|
+
const locales = getLocales().map(e => e.languageTag);
|
|
9
|
+
return {
|
|
10
|
+
language: `${locales[0]}`,
|
|
11
|
+
languages: locales.join(",")
|
|
12
|
+
};
|
|
13
|
+
} catch (e) {}
|
|
14
|
+
return {};
|
|
15
|
+
};
|
|
16
|
+
export default getLanguages;
|
|
17
|
+
//# sourceMappingURL=getLanguages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getLanguages","getLocales","locales","map","e","languageTag","language","languages","join"],"sourceRoot":"..\\..\\src","sources":["getLanguages.ts"],"mappings":";;AAAA,MAAMA,YAAY,GAAG,MAAAA,CAAA,KAA6C;EAChE,IAAI;IACF,MAAM;MAAEC;IAAW,CAAC,GAAG,MAAM,MAAM,CAAC,uBAAuB,CAAC;IAC5D,MAAMC,OAAO,GAAGD,UAAU,CAAC,CAAC,CAACE,GAAG,CAACC,CAAC,IAAIA,CAAC,CAACC,WAAW,CAAC;IAEpD,OAAO;MACLC,QAAQ,EAAE,GAAGJ,OAAO,CAAC,CAAC,CAAC,EAAE;MACzBK,SAAS,EAAEL,OAAO,CAACM,IAAI,CAAC,GAAG;IAC7B,CAAC;EACH,CAAC,CAAC,OAAOJ,CAAC,EAAE,CAAC;EACb,OAAO,CAAC,CAAC;AACX,CAAC;AAED,eAAeJ,YAAY","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["default","Tracker","useAppStateTracker"],"sourceRoot":"..\\..\\src","sources":["index.ts"],"mappings":";;AAAA,SAASA,OAAO,IAAIC,OAAO,QAAQ,cAAW;AAC9C,SAASD,OAAO,IAAIE,kBAAkB,QAAQ,yBAAsB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import getDeviceInfo from "./getDeviceInfo.js";
|
|
4
|
+
import getLanguages from "./getLanguages.js";
|
|
5
|
+
export default class Tracker {
|
|
6
|
+
constructor(baseUrl, trackerId, options) {
|
|
7
|
+
this.baseUrl = baseUrl;
|
|
8
|
+
this.trackerId = trackerId;
|
|
9
|
+
this.options = options;
|
|
10
|
+
this.state = {
|
|
11
|
+
visitId: "",
|
|
12
|
+
visitDepth: 0,
|
|
13
|
+
prevUrl: "",
|
|
14
|
+
currentUrlStartTime: Date.now()
|
|
15
|
+
};
|
|
16
|
+
this.uuidSupplier = options?.uuidSupplier || (async () => {
|
|
17
|
+
try {
|
|
18
|
+
const {
|
|
19
|
+
default: uuid
|
|
20
|
+
} = await import("react-native-uuid");
|
|
21
|
+
return uuid.v4();
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.error(e);
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
this.uuidSupplier().then(e => this.state.visitId = e);
|
|
28
|
+
}
|
|
29
|
+
async getUrl(attributes) {
|
|
30
|
+
const queryString = new URLSearchParams({
|
|
31
|
+
r: Math.random().toString(36).substring(2),
|
|
32
|
+
visit_id: this.state.visitId,
|
|
33
|
+
visit_depth: `${this.state.visitDepth++}`,
|
|
34
|
+
...(this.options?.defaultAttributes && this.options.defaultAttributes()),
|
|
35
|
+
...(!this.options?.disableLanguages && (await getLanguages())),
|
|
36
|
+
...(!this.options?.disableDeviceInfo && (await getDeviceInfo(this.options?.excludeDeviceInfo))),
|
|
37
|
+
...attributes
|
|
38
|
+
}).toString();
|
|
39
|
+
return new URL(`tracker/${this.trackerId}/collect?${queryString}`, this.baseUrl).href;
|
|
40
|
+
}
|
|
41
|
+
async track(attributes) {
|
|
42
|
+
await fetch(await this.getUrl(attributes), {
|
|
43
|
+
credentials: "include"
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async trackNavigation(currentRoutingKey) {
|
|
47
|
+
const now = Date.now();
|
|
48
|
+
const current_url = currentRoutingKey;
|
|
49
|
+
const previous_url = `${this.state.prevUrl}`;
|
|
50
|
+
const previous_url_duration_ms = `${now - this.state.currentUrlStartTime}`;
|
|
51
|
+
this.state.prevUrl = `${current_url}`;
|
|
52
|
+
this.state.currentUrlStartTime = now;
|
|
53
|
+
this.track({
|
|
54
|
+
current_url,
|
|
55
|
+
previous_url,
|
|
56
|
+
previous_url_duration_ms
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=tracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getDeviceInfo","getLanguages","Tracker","constructor","baseUrl","trackerId","options","state","visitId","visitDepth","prevUrl","currentUrlStartTime","Date","now","uuidSupplier","default","uuid","v4","e","console","error","then","getUrl","attributes","queryString","URLSearchParams","r","Math","random","toString","substring","visit_id","visit_depth","defaultAttributes","disableLanguages","disableDeviceInfo","excludeDeviceInfo","URL","href","track","fetch","credentials","trackNavigation","currentRoutingKey","current_url","previous_url","previous_url_duration_ms"],"sourceRoot":"..\\..\\src","sources":["tracker.ts"],"mappings":";;AAAA,OAAOA,aAAa,MAAM,oBAAiB;AAC3C,OAAOC,YAAY,MAAM,mBAAgB;AAiBzC,eAAe,MAAMC,OAAO,CAAC;EAI3BC,WAAWA,CACDC,OAAe,EACfC,SAAiB,EACjBC,OAAiB,EACzB;IAAA,KAHQF,OAAe,GAAfA,OAAe;IAAA,KACfC,SAAiB,GAAjBA,SAAiB;IAAA,KACjBC,OAAiB,GAAjBA,OAAiB;IAEzB,IAAI,CAACC,KAAK,GAAG;MACXC,OAAO,EAAE,EAAE;MACXC,UAAU,EAAE,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,CAAC,CAAC;IACb,OAAO,IAAIQ,GAAG,CACZ,WAAW,IAAI,CAAChC,SAAS,YAAYmB,WAAW,EAAE,EAClD,IAAI,CAACpB,OACP,CAAC,CAACkC,IAAI;EACR;EAEA,MAAaC,KAAKA,CAAChB,UAAmC,EAAE;IACtD,MAAMiB,KAAK,CAAC,MAAM,IAAI,CAAClB,MAAM,CAACC,UAAU,CAAC,EAAE;MAAEkB,WAAW,EAAE;IAAU,CAAC,CAAC;EACxE;EAEA,MAAaC,eAAeA,CAACC,iBAAyB,EAAE;IACtD,MAAM9B,GAAG,GAAGD,IAAI,CAACC,GAAG,CAAC,CAAC;IACtB,MAAM+B,WAAW,GAAGD,iBAAiB;IACrC,MAAME,YAAY,GAAG,GAAG,IAAI,CAACtC,KAAK,CAACG,OAAO,EAAE;IAC5C,MAAMoC,wBAAwB,GAAG,GAAGjC,GAAG,GAAG,IAAI,CAACN,KAAK,CAACI,mBAAmB,EAAE;IAC1E,IAAI,CAACJ,KAAK,CAACG,OAAO,GAAG,GAAGkC,WAAW,EAAE;IACrC,IAAI,CAACrC,KAAK,CAACI,mBAAmB,GAAGE,GAAG;IACpC,IAAI,CAAC0B,KAAK,CAAC;MACTK,WAAW;MACXC,YAAY;MACZC;IACF,CAAC,CAAC;EACJ;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
import { AppState } from "react-native";
|
|
5
|
+
const useAppStateTracker = (tracker, options) => {
|
|
6
|
+
const mountedRef = useRef(false);
|
|
7
|
+
const appState = useRef(AppState.currentState);
|
|
8
|
+
|
|
9
|
+
// on mount
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (!mountedRef.current) {
|
|
12
|
+
mountedRef.current = true;
|
|
13
|
+
tracker.track({
|
|
14
|
+
action: "land",
|
|
15
|
+
...(options?.commonAttributes && options.commonAttributes()),
|
|
16
|
+
...(options?.landingAttributes && options.landingAttributes())
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
// subscribe to app state change event
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const subscription = AppState.addEventListener("change", nextAppState => {
|
|
24
|
+
if (appState.current !== "active" && nextAppState === "active") tracker.track({
|
|
25
|
+
action: "land",
|
|
26
|
+
...(options?.commonAttributes && options.commonAttributes()),
|
|
27
|
+
...(options?.landingAttributes && options.landingAttributes())
|
|
28
|
+
});
|
|
29
|
+
if (appState.current !== "background" && nextAppState === "background") tracker.track({
|
|
30
|
+
action: "leave",
|
|
31
|
+
...(options?.commonAttributes && options.commonAttributes()),
|
|
32
|
+
...(options?.leavingAttributes && options.leavingAttributes())
|
|
33
|
+
});
|
|
34
|
+
appState.current = nextAppState;
|
|
35
|
+
});
|
|
36
|
+
return () => {
|
|
37
|
+
subscription.remove();
|
|
38
|
+
};
|
|
39
|
+
}, []);
|
|
40
|
+
};
|
|
41
|
+
export default useAppStateTracker;
|
|
42
|
+
//# sourceMappingURL=useAppStateTracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEffect","useRef","AppState","useAppStateTracker","tracker","options","mountedRef","appState","currentState","current","track","action","commonAttributes","landingAttributes","subscription","addEventListener","nextAppState","leavingAttributes","remove"],"sourceRoot":"..\\..\\src","sources":["useAppStateTracker.ts"],"mappings":";;AAAA,SAASA,SAAS,EAAEC,MAAM,QAAQ,OAAO;AACzC,SAASC,QAAQ,QAAQ,cAAc;AAGvC,MAAMC,kBAAkB,GAAGA,CACzBC,OAAgB,EAChBC,OAIC,KACE;EACH,MAAMC,UAAU,GAAGL,MAAM,CAAC,KAAK,CAAC;EAChC,MAAMM,QAAQ,GAAGN,MAAM,CAACC,QAAQ,CAACM,YAAY,CAAC;;EAE9C;EACAR,SAAS,CAAC,MAAM;IACd,IAAI,CAACM,UAAU,CAACG,OAAO,EAAE;MACvBH,UAAU,CAACG,O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
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getDeviceInfo.d.ts","sourceRoot":"","sources":["../../../src/getDeviceInfo.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,aAAa,GAAU,UAAU,MAAM,EAAE;;EA4C9C,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getLanguages.d.ts","sourceRoot":"","sources":["../../../src/getLanguages.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,YAAY,QAAa,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAW5D,CAAC;AAEF,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type Options = {
|
|
2
|
+
defaultAttributes?: () => Record<string, string>;
|
|
3
|
+
disableLanguages?: boolean;
|
|
4
|
+
disableDeviceInfo?: boolean;
|
|
5
|
+
excludeDeviceInfo?: string[];
|
|
6
|
+
uuidSupplier?: () => Promise<string>;
|
|
7
|
+
};
|
|
8
|
+
type TrackerState = {
|
|
9
|
+
visitId: string;
|
|
10
|
+
visitDepth: number;
|
|
11
|
+
prevUrl: string;
|
|
12
|
+
currentUrlStartTime: number;
|
|
13
|
+
};
|
|
14
|
+
export default class Tracker {
|
|
15
|
+
private baseUrl;
|
|
16
|
+
private trackerId;
|
|
17
|
+
private options?;
|
|
18
|
+
state: TrackerState;
|
|
19
|
+
uuidSupplier: () => Promise<string>;
|
|
20
|
+
constructor(baseUrl: string, trackerId: string, options?: Options | undefined);
|
|
21
|
+
getUrl(attributes?: Record<string, string>): Promise<string>;
|
|
22
|
+
track(attributes?: Record<string, string>): Promise<void>;
|
|
23
|
+
trackNavigation(currentRoutingKey: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=tracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../../../src/tracker.ts"],"names":[],"mappings":"AAGA,KAAK,OAAO,GAAG;IACb,iBAAiB,CAAC,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjD,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,YAAY,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CACtC,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,mBAAmB,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,OAAO;IAKxB,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,OAAO,CAAC;IANlB,KAAK,EAAE,YAAY,CAAC;IACpB,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;gBAG1B,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,OAAO,YAAA;IAsBd,MAAM,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAiB1C,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAIzC,eAAe,CAAC,iBAAiB,EAAE,MAAM;CAavD"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type Tracker from "./tracker";
|
|
2
|
+
declare const useAppStateTracker: (tracker: Tracker, options?: {
|
|
3
|
+
landingAttributes?: () => Record<string, string>;
|
|
4
|
+
leavingAttributes?: () => Record<string, string>;
|
|
5
|
+
commonAttributes?: () => Record<string, string>;
|
|
6
|
+
}) => void;
|
|
7
|
+
export default useAppStateTracker;
|
|
8
|
+
//# sourceMappingURL=useAppStateTracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAppStateTracker.d.ts","sourceRoot":"","sources":["../../../src/useAppStateTracker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AAErC,QAAA,MAAM,kBAAkB,GACtB,SAAS,OAAO,EAChB,UAAU;IACR,iBAAiB,CAAC,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjD,iBAAiB,CAAC,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjD,gBAAgB,CAAC,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjD,SA0CF,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@datacanva/react-native-tracker",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Datacanva web tracker API for react native.",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"source": "./src/index.ts",
|
|
10
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
11
|
+
"default": "./lib/module/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"lib",
|
|
18
|
+
"android",
|
|
19
|
+
"ios",
|
|
20
|
+
"cpp",
|
|
21
|
+
"*.podspec",
|
|
22
|
+
"react-native.config.js",
|
|
23
|
+
"!ios/build",
|
|
24
|
+
"!android/build",
|
|
25
|
+
"!android/gradle",
|
|
26
|
+
"!android/gradlew",
|
|
27
|
+
"!android/gradlew.bat",
|
|
28
|
+
"!android/local.properties",
|
|
29
|
+
"!**/__tests__",
|
|
30
|
+
"!**/__fixtures__",
|
|
31
|
+
"!**/__mocks__",
|
|
32
|
+
"!**/.*"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"example": "yarn workspace @datacanva/react-native-tracker-example",
|
|
36
|
+
"clean": "del-cli lib",
|
|
37
|
+
"prepare": "bob build",
|
|
38
|
+
"typecheck": "tsc",
|
|
39
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
40
|
+
"test": "jest",
|
|
41
|
+
"release": "release-it --only-version"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"react-native",
|
|
45
|
+
"ios",
|
|
46
|
+
"android"
|
|
47
|
+
],
|
|
48
|
+
"author": "datacanva <info@livelyimpact.com>",
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"registry": "https://registry.npmjs.org/",
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
56
|
+
"@eslint/compat": "^1.3.2",
|
|
57
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
58
|
+
"@eslint/js": "^9.35.0",
|
|
59
|
+
"@react-native/babel-preset": "0.83.0",
|
|
60
|
+
"@react-native/eslint-config": "0.83.0",
|
|
61
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
62
|
+
"@types/jest": "^29.5.14",
|
|
63
|
+
"@types/react": "^19.1.12",
|
|
64
|
+
"commitlint": "^19.8.1",
|
|
65
|
+
"del-cli": "^6.0.0",
|
|
66
|
+
"eslint": "^9.35.0",
|
|
67
|
+
"eslint-config-prettier": "^10.1.8",
|
|
68
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
69
|
+
"jest": "^29.7.0",
|
|
70
|
+
"prettier": "^2.8.8",
|
|
71
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
72
|
+
"react": "19.2.0",
|
|
73
|
+
"react-native": "0.83.2",
|
|
74
|
+
"react-native-builder-bob": "^0.40.13",
|
|
75
|
+
"react-native-device-info": "^15.0.2",
|
|
76
|
+
"react-native-localize": "^3.7.0",
|
|
77
|
+
"react-native-uuid": "^2.0.3",
|
|
78
|
+
"release-it": "^19.0.4",
|
|
79
|
+
"turbo": "^2.5.6",
|
|
80
|
+
"typescript": "^5.9.2"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"react": "*",
|
|
84
|
+
"react-native": "*",
|
|
85
|
+
"react-native-device-info": "*",
|
|
86
|
+
"react-native-localize": "*",
|
|
87
|
+
"react-native-uuid": "*"
|
|
88
|
+
},
|
|
89
|
+
"peerDependenciesMeta": {
|
|
90
|
+
"react-native-device-info": {
|
|
91
|
+
"optional": true
|
|
92
|
+
},
|
|
93
|
+
"react-native-localize": {
|
|
94
|
+
"optional": true
|
|
95
|
+
},
|
|
96
|
+
"react-native-uuid": {
|
|
97
|
+
"optional": true
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"workspaces": [
|
|
101
|
+
"example"
|
|
102
|
+
],
|
|
103
|
+
"packageManager": "yarn@4.11.0",
|
|
104
|
+
"react-native-builder-bob": {
|
|
105
|
+
"source": "src",
|
|
106
|
+
"output": "lib",
|
|
107
|
+
"targets": [
|
|
108
|
+
[
|
|
109
|
+
"module",
|
|
110
|
+
{
|
|
111
|
+
"esm": true
|
|
112
|
+
}
|
|
113
|
+
],
|
|
114
|
+
[
|
|
115
|
+
"typescript",
|
|
116
|
+
{
|
|
117
|
+
"project": "tsconfig.build.json"
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
]
|
|
121
|
+
},
|
|
122
|
+
"jest": {
|
|
123
|
+
"preset": "react-native",
|
|
124
|
+
"modulePathIgnorePatterns": [
|
|
125
|
+
"<rootDir>/example/node_modules",
|
|
126
|
+
"<rootDir>/lib/"
|
|
127
|
+
]
|
|
128
|
+
},
|
|
129
|
+
"commitlint": {
|
|
130
|
+
"extends": [
|
|
131
|
+
"@commitlint/config-conventional"
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
"release-it": {
|
|
135
|
+
"git": {
|
|
136
|
+
"commitMessage": "chore: release ${version}",
|
|
137
|
+
"tagName": "v${version}"
|
|
138
|
+
},
|
|
139
|
+
"npm": {
|
|
140
|
+
"publish": true
|
|
141
|
+
},
|
|
142
|
+
"plugins": {
|
|
143
|
+
"@release-it/conventional-changelog": {
|
|
144
|
+
"preset": {
|
|
145
|
+
"name": "angular"
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
"create-react-native-library": {
|
|
151
|
+
"type": "library",
|
|
152
|
+
"languages": "js",
|
|
153
|
+
"tools": [
|
|
154
|
+
"eslint",
|
|
155
|
+
"jest",
|
|
156
|
+
"release-it"
|
|
157
|
+
],
|
|
158
|
+
"version": "0.57.2"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
type V = string | number | boolean;
|
|
2
|
+
|
|
3
|
+
const getDeviceInfo = async (exclude?: string[]) => {
|
|
4
|
+
try {
|
|
5
|
+
const entry = async (key: string, getter: () => Promise<V> | V) => {
|
|
6
|
+
if (!exclude?.includes(key))
|
|
7
|
+
try {
|
|
8
|
+
return [key, `${await getter()}`] as const;
|
|
9
|
+
} catch (e) {}
|
|
10
|
+
return ["", ""] as const;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const { default: DeviceInfo } = await import("react-native-device-info");
|
|
14
|
+
const entries = await Promise.all([
|
|
15
|
+
// Identifiers
|
|
16
|
+
entry("mobile_unique_id", DeviceInfo.getUniqueId),
|
|
17
|
+
entry("mobile_android_id", DeviceInfo.getAndroidId),
|
|
18
|
+
entry(
|
|
19
|
+
"mobile_android_app_set_id",
|
|
20
|
+
async () => (await DeviceInfo.getAppSetId()).id,
|
|
21
|
+
),
|
|
22
|
+
// Device/system info
|
|
23
|
+
entry("mobile_device_id", DeviceInfo.getDeviceId),
|
|
24
|
+
entry("mobile_device_type", DeviceInfo.getDeviceType),
|
|
25
|
+
entry("mobile_manufacturer", DeviceInfo.getManufacturer),
|
|
26
|
+
entry("mobile_brand", DeviceInfo.getBrand),
|
|
27
|
+
entry("mobile_model", DeviceInfo.getModel),
|
|
28
|
+
entry("mobile_system_name", DeviceInfo.getSystemName),
|
|
29
|
+
entry("mobile_system_version", DeviceInfo.getSystemVersion),
|
|
30
|
+
entry("mobile_system_build_id", DeviceInfo.getBuildId),
|
|
31
|
+
entry("mobile_is_emulator", DeviceInfo.isEmulator),
|
|
32
|
+
entry("mobile_carrier", DeviceInfo.getCarrier),
|
|
33
|
+
// Application info
|
|
34
|
+
entry("mobile_app_bundle_id", DeviceInfo.getBundleId),
|
|
35
|
+
entry("mobile_app_version", DeviceInfo.getVersion),
|
|
36
|
+
entry("mobile_app_build_number", DeviceInfo.getBuildNumber),
|
|
37
|
+
entry("mobile_app_readable_version", DeviceInfo.getReadableVersion),
|
|
38
|
+
entry("mobile_app_installer_package", DeviceInfo.getInstallerPackageName),
|
|
39
|
+
entry("mobile_app_instance_id", DeviceInfo.getInstanceId),
|
|
40
|
+
entry("mobile_app_update_time", DeviceInfo.getLastUpdateTime),
|
|
41
|
+
]);
|
|
42
|
+
return Object.fromEntries(
|
|
43
|
+
entries.filter(e => e[0] && !exclude?.includes(e[0])),
|
|
44
|
+
);
|
|
45
|
+
} catch (e) {}
|
|
46
|
+
return {};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export default getDeviceInfo;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const getLanguages = async (): Promise<Record<string, string>> => {
|
|
2
|
+
try {
|
|
3
|
+
const { getLocales } = await import("react-native-localize");
|
|
4
|
+
const locales = getLocales().map(e => e.languageTag);
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
language: `${locales[0]}`,
|
|
8
|
+
languages: locales.join(","),
|
|
9
|
+
};
|
|
10
|
+
} catch (e) {}
|
|
11
|
+
return {};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default getLanguages;
|
package/src/index.ts
ADDED
package/src/tracker.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import getDeviceInfo from "./getDeviceInfo";
|
|
2
|
+
import getLanguages from "./getLanguages";
|
|
3
|
+
|
|
4
|
+
type Options = {
|
|
5
|
+
defaultAttributes?: () => Record<string, string>;
|
|
6
|
+
disableLanguages?: boolean;
|
|
7
|
+
disableDeviceInfo?: boolean;
|
|
8
|
+
excludeDeviceInfo?: string[];
|
|
9
|
+
uuidSupplier?: () => Promise<string>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type TrackerState = {
|
|
13
|
+
visitId: string;
|
|
14
|
+
visitDepth: number;
|
|
15
|
+
prevUrl: string;
|
|
16
|
+
currentUrlStartTime: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default class Tracker {
|
|
20
|
+
state: TrackerState;
|
|
21
|
+
uuidSupplier: () => Promise<string>;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
private baseUrl: string,
|
|
25
|
+
private trackerId: string,
|
|
26
|
+
private options?: Options,
|
|
27
|
+
) {
|
|
28
|
+
this.state = {
|
|
29
|
+
visitId: "",
|
|
30
|
+
visitDepth: 0,
|
|
31
|
+
prevUrl: "",
|
|
32
|
+
currentUrlStartTime: Date.now(),
|
|
33
|
+
};
|
|
34
|
+
this.uuidSupplier =
|
|
35
|
+
options?.uuidSupplier ||
|
|
36
|
+
(async () => {
|
|
37
|
+
try {
|
|
38
|
+
const { default: uuid } = await import("react-native-uuid");
|
|
39
|
+
return uuid.v4();
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.error(e);
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
this.uuidSupplier().then(e => (this.state.visitId = e));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async getUrl(attributes?: Record<string, string>) {
|
|
49
|
+
const queryString = new URLSearchParams({
|
|
50
|
+
r: Math.random().toString(36).substring(2),
|
|
51
|
+
visit_id: this.state.visitId,
|
|
52
|
+
visit_depth: `${this.state.visitDepth++}`,
|
|
53
|
+
...(this.options?.defaultAttributes && this.options.defaultAttributes()),
|
|
54
|
+
...(!this.options?.disableLanguages && (await getLanguages())),
|
|
55
|
+
...(!this.options?.disableDeviceInfo &&
|
|
56
|
+
(await getDeviceInfo(this.options?.excludeDeviceInfo))),
|
|
57
|
+
...attributes,
|
|
58
|
+
}).toString();
|
|
59
|
+
return new URL(
|
|
60
|
+
`tracker/${this.trackerId}/collect?${queryString}`,
|
|
61
|
+
this.baseUrl,
|
|
62
|
+
).href;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public async track(attributes?: Record<string, string>) {
|
|
66
|
+
await fetch(await this.getUrl(attributes), { credentials: "include" });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async trackNavigation(currentRoutingKey: string) {
|
|
70
|
+
const now = Date.now();
|
|
71
|
+
const current_url = currentRoutingKey;
|
|
72
|
+
const previous_url = `${this.state.prevUrl}`;
|
|
73
|
+
const previous_url_duration_ms = `${now - this.state.currentUrlStartTime}`;
|
|
74
|
+
this.state.prevUrl = `${current_url}`;
|
|
75
|
+
this.state.currentUrlStartTime = now;
|
|
76
|
+
this.track({
|
|
77
|
+
current_url,
|
|
78
|
+
previous_url,
|
|
79
|
+
previous_url_duration_ms,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import { AppState } from "react-native";
|
|
3
|
+
import type Tracker from "./tracker";
|
|
4
|
+
|
|
5
|
+
const useAppStateTracker = (
|
|
6
|
+
tracker: Tracker,
|
|
7
|
+
options?: {
|
|
8
|
+
landingAttributes?: () => Record<string, string>;
|
|
9
|
+
leavingAttributes?: () => Record<string, string>;
|
|
10
|
+
commonAttributes?: () => Record<string, string>;
|
|
11
|
+
},
|
|
12
|
+
) => {
|
|
13
|
+
const mountedRef = useRef(false);
|
|
14
|
+
const appState = useRef(AppState.currentState);
|
|
15
|
+
|
|
16
|
+
// on mount
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!mountedRef.current) {
|
|
19
|
+
mountedRef.current = true;
|
|
20
|
+
|
|
21
|
+
tracker.track({
|
|
22
|
+
action: "land",
|
|
23
|
+
...(options?.commonAttributes && options.commonAttributes()),
|
|
24
|
+
...(options?.landingAttributes && options.landingAttributes()),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}, []);
|
|
28
|
+
|
|
29
|
+
// subscribe to app state change event
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const subscription = AppState.addEventListener("change", nextAppState => {
|
|
32
|
+
if (appState.current !== "active" && nextAppState === "active")
|
|
33
|
+
tracker.track({
|
|
34
|
+
action: "land",
|
|
35
|
+
...(options?.commonAttributes && options.commonAttributes()),
|
|
36
|
+
...(options?.landingAttributes && options.landingAttributes()),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (appState.current !== "background" && nextAppState === "background")
|
|
40
|
+
tracker.track({
|
|
41
|
+
action: "leave",
|
|
42
|
+
...(options?.commonAttributes && options.commonAttributes()),
|
|
43
|
+
...(options?.leavingAttributes && options.leavingAttributes()),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
appState.current = nextAppState;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return () => {
|
|
50
|
+
subscription.remove();
|
|
51
|
+
};
|
|
52
|
+
}, []);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default useAppStateTracker;
|