@posted-sdk/react-native 1.0.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 +149 -0
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/reportInstall.js +50 -0
- package/lib/module/reportInstall.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/deviceId.js +46 -0
- package/lib/module/utils/deviceId.js.map +1 -0
- package/lib/module/utils/deviceInfo.js +64 -0
- package/lib/module/utils/deviceInfo.js.map +1 -0
- package/lib/module/utils/fingerprint.js +44 -0
- package/lib/module/utils/fingerprint.js.map +1 -0
- package/lib/module/utils/installReferrer.js +19 -0
- package/lib/module/utils/installReferrer.js.map +1 -0
- package/lib/module/utils/optionalModules.js +48 -0
- package/lib/module/utils/optionalModules.js.map +1 -0
- package/lib/module/utils/storage.js +28 -0
- package/lib/module/utils/storage.js.map +1 -0
- package/lib/module/version.js +5 -0
- package/lib/module/version.js.map +1 -0
- package/lib/typescript/package.json +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/reportInstall.d.ts +3 -0
- package/lib/typescript/src/reportInstall.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +11 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/utils/deviceId.d.ts +3 -0
- package/lib/typescript/src/utils/deviceId.d.ts.map +1 -0
- package/lib/typescript/src/utils/deviceInfo.d.ts +9 -0
- package/lib/typescript/src/utils/deviceInfo.d.ts.map +1 -0
- package/lib/typescript/src/utils/fingerprint.d.ts +17 -0
- package/lib/typescript/src/utils/fingerprint.d.ts.map +1 -0
- package/lib/typescript/src/utils/installReferrer.d.ts +6 -0
- package/lib/typescript/src/utils/installReferrer.d.ts.map +1 -0
- package/lib/typescript/src/utils/optionalModules.d.ts +41 -0
- package/lib/typescript/src/utils/optionalModules.d.ts.map +1 -0
- package/lib/typescript/src/utils/storage.d.ts +7 -0
- package/lib/typescript/src/utils/storage.d.ts.map +1 -0
- package/lib/typescript/src/version.d.ts +3 -0
- package/lib/typescript/src/version.d.ts.map +1 -0
- package/package.json +153 -0
- package/src/index.ts +3 -0
- package/src/reportInstall.ts +53 -0
- package/src/types.ts +11 -0
- package/src/utils/deviceId.ts +53 -0
- package/src/utils/deviceInfo.ts +98 -0
- package/src/utils/fingerprint.ts +53 -0
- package/src/utils/installReferrer.ts +19 -0
- package/src/utils/optionalModules.ts +77 -0
- package/src/utils/storage.ts +35 -0
- package/src/version.ts +2 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native packages the SDK can read device data from. All are optional peer
|
|
3
|
+
* dependencies: Expo apps use the Expo packages, bare React Native apps can use
|
|
4
|
+
* react-native-device-info instead. A missing package resolves to null.
|
|
5
|
+
*
|
|
6
|
+
* Metro needs a string literal in each require() at bundle time.
|
|
7
|
+
*/
|
|
8
|
+
export type ExpoDeviceModule = {
|
|
9
|
+
modelName?: string | null;
|
|
10
|
+
manufacturer?: string | null;
|
|
11
|
+
osVersion?: string | null;
|
|
12
|
+
};
|
|
13
|
+
export type ReactNativeDeviceInfoModule = {
|
|
14
|
+
getModel?: () => string;
|
|
15
|
+
getSystemVersion?: () => string;
|
|
16
|
+
getManufacturerSync?: () => string;
|
|
17
|
+
getManufacturer?: () => Promise<string>;
|
|
18
|
+
getUserAgent?: () => Promise<string>;
|
|
19
|
+
getInstallReferrer?: () => Promise<string>;
|
|
20
|
+
};
|
|
21
|
+
type ExpoConstantsModule = {
|
|
22
|
+
getWebViewUserAgentAsync?: () => Promise<string | null>;
|
|
23
|
+
};
|
|
24
|
+
type ExpoLocalizationModule = {
|
|
25
|
+
getLocales: () => {
|
|
26
|
+
languageTag: string;
|
|
27
|
+
}[];
|
|
28
|
+
getCalendars: () => {
|
|
29
|
+
timeZone?: string | null;
|
|
30
|
+
}[];
|
|
31
|
+
};
|
|
32
|
+
type ExpoApplicationModule = {
|
|
33
|
+
getInstallReferrerAsync: () => Promise<string>;
|
|
34
|
+
};
|
|
35
|
+
export declare const expoDevice: ExpoDeviceModule | null;
|
|
36
|
+
export declare const reactNativeDeviceInfo: ReactNativeDeviceInfoModule | null;
|
|
37
|
+
export declare const expoConstants: ExpoConstantsModule | null;
|
|
38
|
+
export declare const expoLocalization: ExpoLocalizationModule | null;
|
|
39
|
+
export declare const expoApplication: ExpoApplicationModule | null;
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=optionalModules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optionalModules.d.ts","sourceRoot":"","sources":["../../../../src/utils/optionalModules.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG;IACxC,QAAQ,CAAC,EAAE,MAAM,MAAM,CAAC;IACxB,gBAAgB,CAAC,EAAE,MAAM,MAAM,CAAC;IAChC,mBAAmB,CAAC,EAAE,MAAM,MAAM,CAAC;IACnC,eAAe,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,YAAY,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IACrC,kBAAkB,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CAC5C,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,wBAAwB,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CACzD,CAAC;AAEF,KAAK,sBAAsB,GAAG;IAC5B,UAAU,EAAE,MAAM;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC5C,YAAY,EAAE,MAAM;QAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,EAAE,CAAC;CACpD,CAAC;AAEF,KAAK,qBAAqB,GAAG;IAC3B,uBAAuB,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CAChD,CAAC;AAEF,eAAO,MAAM,UAAU,yBAMnB,CAAC;AAEL,eAAO,MAAM,qBAAqB,oCAO9B,CAAC;AAEL,eAAO,MAAM,aAAa,4BAOtB,CAAC;AAEL,eAAO,MAAM,gBAAgB,+BAMzB,CAAC;AAEL,eAAO,MAAM,eAAe,8BAMxB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PostedStorage } from "../types";
|
|
2
|
+
export declare enum StorageKeys {
|
|
3
|
+
DEVICE_ID_KEY = "Posted_deviceId",
|
|
4
|
+
INSTALL_REPORTED_FLAG_KEY = "Posted_installReported"
|
|
5
|
+
}
|
|
6
|
+
export declare const resolveStorage: (userStorage?: PostedStorage) => PostedStorage;
|
|
7
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../../src/utils/storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAO9C,oBAAY,WAAW;IACrB,aAAa,oBAAY;IACzB,yBAAyB,2BAAwB;CAClD;AAYD,eAAO,MAAM,cAAc,GAAI,cAAc,aAAa,KAAG,aAY5D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,UAAU,CAAC;AAC/B,eAAO,MAAM,gBAAgB,uBAA4B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@posted-sdk/react-native",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Reports app installs to posted-sdk for ad attribution — React Native package",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"react-native": "./src/index.ts",
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
12
|
+
"default": "./lib/module/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"lib",
|
|
19
|
+
"!**/__tests__",
|
|
20
|
+
"!**/__fixtures__",
|
|
21
|
+
"!**/__mocks__",
|
|
22
|
+
"!**/.*"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "jest",
|
|
26
|
+
"typecheck": "tsc",
|
|
27
|
+
"docs": "typedoc",
|
|
28
|
+
"lint": "eslint . --cache --cache-location .eslintcache",
|
|
29
|
+
"clean": "del-cli lib",
|
|
30
|
+
"prepare": "bob build",
|
|
31
|
+
"prepack": "cp ../../README.md README.md",
|
|
32
|
+
"postpack": "rm README.md",
|
|
33
|
+
"release": "release-it"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"react-native",
|
|
37
|
+
"ios",
|
|
38
|
+
"android"
|
|
39
|
+
],
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/thepostedapp/posted-sdk-react-native.git",
|
|
43
|
+
"directory": "packages/react-native-posted-sdk"
|
|
44
|
+
},
|
|
45
|
+
"author": "postedapp <yuriy@postedapp.com>",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/thepostedapp/posted-sdk-react-native/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/thepostedapp/posted-sdk-react-native#readme",
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public",
|
|
52
|
+
"registry": "https://registry.npmjs.org/"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@react-native-async-storage/async-storage": "^2.2.0",
|
|
56
|
+
"@react-native/babel-preset": "0.83.4",
|
|
57
|
+
"@release-it/conventional-changelog": "^9.0.2",
|
|
58
|
+
"@types/jest": "^29.5.5",
|
|
59
|
+
"del-cli": "^5.1.0",
|
|
60
|
+
"expo-application": "~55.0.19",
|
|
61
|
+
"expo-constants": "~55.0.11",
|
|
62
|
+
"expo-device": "~55.0.12",
|
|
63
|
+
"expo-localization": "~55.0.19",
|
|
64
|
+
"jest": "^29.7.0",
|
|
65
|
+
"react": "19.2.0",
|
|
66
|
+
"react-native": "0.83.4",
|
|
67
|
+
"react-native-builder-bob": "0.40.13",
|
|
68
|
+
"react-native-device-info": "^15.0.2",
|
|
69
|
+
"release-it": "^17.10.0",
|
|
70
|
+
"typedoc": "^0.28.20",
|
|
71
|
+
"typescript": "^5.8.3"
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"@react-native-async-storage/async-storage": ">=2.0.0",
|
|
75
|
+
"expo-application": ">=6.1.5",
|
|
76
|
+
"expo-constants": ">=17.1.7",
|
|
77
|
+
"expo-device": ">=7.0.0",
|
|
78
|
+
"expo-localization": ">=15.0.0",
|
|
79
|
+
"react-native": ">=0.72",
|
|
80
|
+
"react-native-device-info": ">=10.0.0"
|
|
81
|
+
},
|
|
82
|
+
"peerDependenciesMeta": {
|
|
83
|
+
"@react-native-async-storage/async-storage": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"expo-application": {
|
|
87
|
+
"optional": true
|
|
88
|
+
},
|
|
89
|
+
"expo-constants": {
|
|
90
|
+
"optional": true
|
|
91
|
+
},
|
|
92
|
+
"expo-localization": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"expo-device": {
|
|
96
|
+
"optional": true
|
|
97
|
+
},
|
|
98
|
+
"react-native-device-info": {
|
|
99
|
+
"optional": true
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"jest": {
|
|
103
|
+
"preset": "react-native",
|
|
104
|
+
"modulePathIgnorePatterns": [
|
|
105
|
+
"<rootDir>/lib/"
|
|
106
|
+
]
|
|
107
|
+
},
|
|
108
|
+
"release-it": {
|
|
109
|
+
"hooks": {
|
|
110
|
+
"after:bump": "node -e \"const fs=require('fs'); const f='./src/version.ts'; const c=fs.readFileSync(f,'utf8').replace(/version = [\\\"'].*[\\\"'];/, 'version = \\\"${version}\\\";'); fs.writeFileSync(f,c);\""
|
|
111
|
+
},
|
|
112
|
+
"git": {
|
|
113
|
+
"commitMessage": "chore: release ${version}",
|
|
114
|
+
"tagName": "v${version}"
|
|
115
|
+
},
|
|
116
|
+
"npm": {
|
|
117
|
+
"publish": false
|
|
118
|
+
},
|
|
119
|
+
"github": {
|
|
120
|
+
"release": true
|
|
121
|
+
},
|
|
122
|
+
"plugins": {
|
|
123
|
+
"@release-it/conventional-changelog": {
|
|
124
|
+
"preset": {
|
|
125
|
+
"name": "angular"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
"react-native-builder-bob": {
|
|
131
|
+
"source": "src",
|
|
132
|
+
"output": "lib",
|
|
133
|
+
"targets": [
|
|
134
|
+
[
|
|
135
|
+
"module",
|
|
136
|
+
{
|
|
137
|
+
"esm": true
|
|
138
|
+
}
|
|
139
|
+
],
|
|
140
|
+
[
|
|
141
|
+
"typescript",
|
|
142
|
+
{
|
|
143
|
+
"project": "tsconfig.build.json"
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
]
|
|
147
|
+
},
|
|
148
|
+
"create-react-native-library": {
|
|
149
|
+
"languages": "js",
|
|
150
|
+
"type": "library",
|
|
151
|
+
"version": "0.53.0"
|
|
152
|
+
}
|
|
153
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Config } from "./types";
|
|
2
|
+
import { prepareDeviceIdForApi } from "./utils/deviceId";
|
|
3
|
+
import { getProbabilisticFingerprint } from "./utils/fingerprint";
|
|
4
|
+
import { getInstallReferrerClickId } from "./utils/installReferrer";
|
|
5
|
+
import { StorageKeys, resolveStorage } from "./utils/storage";
|
|
6
|
+
import { SDK_HEADER_VALUE } from "./version";
|
|
7
|
+
|
|
8
|
+
const INSTALL_API_URL = "https://attr.postedapp.com/api/install";
|
|
9
|
+
|
|
10
|
+
let inFlight: Promise<void> | null = null;
|
|
11
|
+
|
|
12
|
+
export const reportInstall = (config: Config): Promise<void> => {
|
|
13
|
+
inFlight ??= send(config).finally(() => {
|
|
14
|
+
inFlight = null;
|
|
15
|
+
});
|
|
16
|
+
return inFlight;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const send = async ({ apiKey, appID, storage: userStorage }: Config) => {
|
|
20
|
+
try {
|
|
21
|
+
const storage = resolveStorage(userStorage);
|
|
22
|
+
if (await storage.getItem(StorageKeys.INSTALL_REPORTED_FLAG_KEY)) return;
|
|
23
|
+
|
|
24
|
+
const [clickId, fingerprint, deviceId] = await Promise.all([
|
|
25
|
+
getInstallReferrerClickId(),
|
|
26
|
+
getProbabilisticFingerprint(),
|
|
27
|
+
prepareDeviceIdForApi(storage),
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
const response = await fetch(INSTALL_API_URL, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: {
|
|
33
|
+
"Content-Type": "application/json",
|
|
34
|
+
Authorization: `Bearer ${apiKey}`,
|
|
35
|
+
"X-App-ID": appID,
|
|
36
|
+
"X-SDK": SDK_HEADER_VALUE,
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
...fingerprint,
|
|
40
|
+
device_id: deviceId,
|
|
41
|
+
...(clickId ? { clickId } : {}),
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
console.warn(`🔗[Posted:INSTALL_ERROR] Failed to report install: ${response.status}`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
await storage.setItem(StorageKeys.INSTALL_REPORTED_FLAG_KEY, "true");
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error("🔗[Posted:INSTALL_ERROR] Error reporting install:", error);
|
|
52
|
+
}
|
|
53
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Config = {
|
|
2
|
+
appID: string;
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Defaults to `@react-native-async-storage/async-storage` when installed. */
|
|
5
|
+
storage?: PostedStorage;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export interface PostedStorage {
|
|
9
|
+
getItem(key: string): Promise<string | null> | string | null;
|
|
10
|
+
setItem(key: string, value: string): Promise<void> | void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/* eslint-disable no-bitwise */
|
|
2
|
+
import type { PostedStorage } from "../types";
|
|
3
|
+
import { StorageKeys } from "./storage";
|
|
4
|
+
|
|
5
|
+
const generateUUID = () => {
|
|
6
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
7
|
+
const r = (Math.random() * 16) | 0;
|
|
8
|
+
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
|
9
|
+
return v.toString(16);
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const saveDeviceId = async (storage: PostedStorage, id: string) => {
|
|
14
|
+
await storage.setItem(StorageKeys.DEVICE_ID_KEY, id);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const getDeviceId = async (storage: PostedStorage) => {
|
|
18
|
+
return await storage.getItem(StorageKeys.DEVICE_ID_KEY);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
let cachedDeviceId: string | null = null;
|
|
22
|
+
let pendingDeviceIdPromise: Promise<string> | null = null;
|
|
23
|
+
|
|
24
|
+
export const prepareDeviceIdForApi = async (storage: PostedStorage) => {
|
|
25
|
+
if (cachedDeviceId) {
|
|
26
|
+
return cachedDeviceId;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (pendingDeviceIdPromise) {
|
|
30
|
+
return pendingDeviceIdPromise;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const getOrGenerate = async () => {
|
|
34
|
+
const existingId = await getDeviceId(storage);
|
|
35
|
+
if (existingId) {
|
|
36
|
+
cachedDeviceId = existingId;
|
|
37
|
+
return existingId;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const newId = generateUUID();
|
|
41
|
+
await saveDeviceId(storage, newId);
|
|
42
|
+
cachedDeviceId = newId;
|
|
43
|
+
return newId;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
pendingDeviceIdPromise = getOrGenerate();
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
return await pendingDeviceIdPromise;
|
|
50
|
+
} finally {
|
|
51
|
+
pendingDeviceIdPromise = null;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { expoConstants, expoDevice, reactNativeDeviceInfo } from "./optionalModules";
|
|
2
|
+
|
|
3
|
+
type DeviceInfo = {
|
|
4
|
+
model: string;
|
|
5
|
+
manufacturer: string;
|
|
6
|
+
osVersion: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const UNKNOWN = "unknown";
|
|
10
|
+
|
|
11
|
+
const normalizeValue = (value: unknown): string => {
|
|
12
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === "number") {
|
|
16
|
+
return String(value);
|
|
17
|
+
}
|
|
18
|
+
return UNKNOWN;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const firstKnown = (...candidates: (() => unknown)[]): string => {
|
|
22
|
+
for (const fn of candidates) {
|
|
23
|
+
const value = normalizeValue(fn());
|
|
24
|
+
if (value !== UNKNOWN) {
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return UNKNOWN;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const firstKnownAsync = async (
|
|
33
|
+
...candidates: (() => unknown | Promise<unknown>)[]
|
|
34
|
+
): Promise<string> => {
|
|
35
|
+
for (const fn of candidates) {
|
|
36
|
+
const value = normalizeValue(await fn());
|
|
37
|
+
if (value !== UNKNOWN) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return UNKNOWN;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const getModel = (): string => {
|
|
46
|
+
return firstKnown(
|
|
47
|
+
() => expoDevice?.modelName,
|
|
48
|
+
() => reactNativeDeviceInfo?.getModel?.(),
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const getOsVersion = (): string => {
|
|
53
|
+
return firstKnown(
|
|
54
|
+
() => expoDevice?.osVersion,
|
|
55
|
+
() => reactNativeDeviceInfo?.getSystemVersion?.(),
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const getManufacturer = async (): Promise<string> => {
|
|
60
|
+
return firstKnownAsync(
|
|
61
|
+
() => expoDevice?.manufacturer,
|
|
62
|
+
async () => {
|
|
63
|
+
try {
|
|
64
|
+
return await reactNativeDeviceInfo?.getManufacturer?.();
|
|
65
|
+
} catch {
|
|
66
|
+
return UNKNOWN;
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
() => reactNativeDeviceInfo?.getManufacturerSync?.(),
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const assertDeviceInfoLibraryAvailable = (): void => {
|
|
74
|
+
if (!expoDevice && !reactNativeDeviceInfo) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
'[react-native-posted-sdk] No device info library found. Install either "expo-device" or "react-native-device-info".',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const getDeviceInfo = async (): Promise<DeviceInfo> => {
|
|
82
|
+
assertDeviceInfoLibraryAvailable();
|
|
83
|
+
|
|
84
|
+
const manufacturer = await getManufacturer();
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
model: getModel(),
|
|
88
|
+
manufacturer,
|
|
89
|
+
osVersion: getOsVersion(),
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const getUserAgent = async (): Promise<string> => {
|
|
94
|
+
return firstKnownAsync(
|
|
95
|
+
() => expoConstants?.getWebViewUserAgentAsync?.().catch(() => null),
|
|
96
|
+
() => reactNativeDeviceInfo?.getUserAgent?.().catch(() => null),
|
|
97
|
+
);
|
|
98
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Dimensions, PixelRatio, Platform } from "react-native";
|
|
2
|
+
|
|
3
|
+
import { getDeviceInfo, getUserAgent } from "./deviceInfo";
|
|
4
|
+
import { expoLocalization } from "./optionalModules";
|
|
5
|
+
|
|
6
|
+
const intlOptions = (): Intl.ResolvedDateTimeFormatOptions | null => {
|
|
7
|
+
try {
|
|
8
|
+
return Intl.DateTimeFormat().resolvedOptions();
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type ProbabilisticFingerprint = {
|
|
15
|
+
platform: string;
|
|
16
|
+
model: string;
|
|
17
|
+
manufacturer: string;
|
|
18
|
+
systemVersion: string;
|
|
19
|
+
screenWidth: number;
|
|
20
|
+
screenHeight: number;
|
|
21
|
+
scale: number;
|
|
22
|
+
locale: { languageTag: string }[];
|
|
23
|
+
timezone: string | null | undefined;
|
|
24
|
+
userAgent: string;
|
|
25
|
+
timestamp: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// No clipboard read: it shows an iOS paste banner and install matching doesn't use it.
|
|
29
|
+
export const getProbabilisticFingerprint = async (): Promise<ProbabilisticFingerprint> => {
|
|
30
|
+
const { width, height } = Dimensions.get("screen");
|
|
31
|
+
const localeLanguageTags = expoLocalization
|
|
32
|
+
? expoLocalization.getLocales().map((locale) => ({ languageTag: locale.languageTag }))
|
|
33
|
+
: [{ languageTag: intlOptions()?.locale ?? "unknown" }];
|
|
34
|
+
|
|
35
|
+
const { model, manufacturer, osVersion: systemVersion } = await getDeviceInfo();
|
|
36
|
+
const userAgent = await getUserAgent();
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
platform: Platform.OS,
|
|
40
|
+
model,
|
|
41
|
+
manufacturer,
|
|
42
|
+
systemVersion,
|
|
43
|
+
screenWidth: width,
|
|
44
|
+
screenHeight: height,
|
|
45
|
+
scale: PixelRatio.get(),
|
|
46
|
+
locale: localeLanguageTags,
|
|
47
|
+
timezone: expoLocalization
|
|
48
|
+
? expoLocalization.getCalendars()[0]?.timeZone
|
|
49
|
+
: intlOptions()?.timeZone,
|
|
50
|
+
userAgent,
|
|
51
|
+
timestamp: Date.now(),
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { expoApplication, reactNativeDeviceInfo } from "./optionalModules";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The click id carried through the Play Store install referrer (`click_id=…`),
|
|
5
|
+
* or null — always null on iOS, where the App Store passes nothing through.
|
|
6
|
+
*/
|
|
7
|
+
export const getInstallReferrerClickId = async (): Promise<string | null> => {
|
|
8
|
+
let referrer: string | null = null;
|
|
9
|
+
try {
|
|
10
|
+
referrer =
|
|
11
|
+
(await (expoApplication?.getInstallReferrerAsync() ??
|
|
12
|
+
reactNativeDeviceInfo?.getInstallReferrer?.())) ?? null;
|
|
13
|
+
} catch {
|
|
14
|
+
referrer = null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const match = decodeURIComponent(referrer ?? "").match(/(?:^|&)click_id=([^&]+)/);
|
|
18
|
+
return match?.[1] || null;
|
|
19
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native packages the SDK can read device data from. All are optional peer
|
|
3
|
+
* dependencies: Expo apps use the Expo packages, bare React Native apps can use
|
|
4
|
+
* react-native-device-info instead. A missing package resolves to null.
|
|
5
|
+
*
|
|
6
|
+
* Metro needs a string literal in each require() at bundle time.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type ExpoDeviceModule = {
|
|
10
|
+
modelName?: string | null;
|
|
11
|
+
manufacturer?: string | null;
|
|
12
|
+
osVersion?: string | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ReactNativeDeviceInfoModule = {
|
|
16
|
+
getModel?: () => string;
|
|
17
|
+
getSystemVersion?: () => string;
|
|
18
|
+
getManufacturerSync?: () => string;
|
|
19
|
+
getManufacturer?: () => Promise<string>;
|
|
20
|
+
getUserAgent?: () => Promise<string>;
|
|
21
|
+
getInstallReferrer?: () => Promise<string>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type ExpoConstantsModule = {
|
|
25
|
+
getWebViewUserAgentAsync?: () => Promise<string | null>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type ExpoLocalizationModule = {
|
|
29
|
+
getLocales: () => { languageTag: string }[];
|
|
30
|
+
getCalendars: () => { timeZone?: string | null }[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type ExpoApplicationModule = {
|
|
34
|
+
getInstallReferrerAsync: () => Promise<string>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const expoDevice = (() => {
|
|
38
|
+
try {
|
|
39
|
+
return require("expo-device") as ExpoDeviceModule;
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
})();
|
|
44
|
+
|
|
45
|
+
export const reactNativeDeviceInfo = (() => {
|
|
46
|
+
try {
|
|
47
|
+
const module = require("react-native-device-info");
|
|
48
|
+
return (module.default ?? module) as ReactNativeDeviceInfoModule;
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
|
|
54
|
+
export const expoConstants = (() => {
|
|
55
|
+
try {
|
|
56
|
+
const module = require("expo-constants");
|
|
57
|
+
return (module.default ?? module) as ExpoConstantsModule;
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
})();
|
|
62
|
+
|
|
63
|
+
export const expoLocalization = (() => {
|
|
64
|
+
try {
|
|
65
|
+
return require("expo-localization") as ExpoLocalizationModule;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
})();
|
|
70
|
+
|
|
71
|
+
export const expoApplication = (() => {
|
|
72
|
+
try {
|
|
73
|
+
return require("expo-application") as ExpoApplicationModule;
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
})();
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { PostedStorage } from "../types";
|
|
2
|
+
|
|
3
|
+
// Key names are persisted on existing installs — never rename them.
|
|
4
|
+
const STORAGE_KEY_PREFIX = "Posted_";
|
|
5
|
+
const DEVICE_ID = `${STORAGE_KEY_PREFIX}deviceId`;
|
|
6
|
+
const INSTALL_REPORTED_FLAG = `${STORAGE_KEY_PREFIX}installReported`;
|
|
7
|
+
|
|
8
|
+
export enum StorageKeys {
|
|
9
|
+
DEVICE_ID_KEY = DEVICE_ID,
|
|
10
|
+
INSTALL_REPORTED_FLAG_KEY = INSTALL_REPORTED_FLAG,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let DefaultAsyncStorage: PostedStorage | null = null;
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const storageModule = require("@react-native-async-storage/async-storage");
|
|
17
|
+
|
|
18
|
+
DefaultAsyncStorage = storageModule.default || storageModule;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
DefaultAsyncStorage = null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const resolveStorage = (userStorage?: PostedStorage): PostedStorage => {
|
|
24
|
+
if (userStorage) {
|
|
25
|
+
return userStorage;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (DefaultAsyncStorage) {
|
|
29
|
+
return DefaultAsyncStorage;
|
|
30
|
+
}
|
|
31
|
+
throw new Error(
|
|
32
|
+
"🔗[Posted] No storage implementation provided.\n" +
|
|
33
|
+
'Please install "@react-native-async-storage/async-storage" OR pass a custom "storage" in the config',
|
|
34
|
+
);
|
|
35
|
+
};
|
package/src/version.ts
ADDED