@snowplow/react-native-tracker 1.4.0 → 2.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/.editorconfig +15 -0
- package/.eslintignore +6 -0
- package/.gitattributes +3 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/actions/setup/action.yml +28 -0
- package/.github/workflows/build.yml +150 -0
- package/.github/workflows/deploy.yml +98 -0
- package/.github/workflows/e2e-android.yml +121 -0
- package/.github/workflows/e2e-ios.yml +90 -0
- package/.github/workflows/snyk.yml +21 -0
- package/.gitignore +70 -0
- package/.npmignore +60 -0
- package/.nvmrc +1 -0
- package/.watchmanconfig +1 -0
- package/.yarnrc +3 -0
- package/CHANGELOG +166 -0
- package/CONTRIBUTING.md +80 -0
- package/README.md +23 -110
- package/android/build.gradle +93 -37
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +1 -2
- package/android/gradle.properties +5 -2
- package/android/gradlew +172 -110
- package/android/gradlew.bat +24 -19
- package/android/src/main/AndroidManifest.xml +1 -3
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/snowplow/reactnativetracker/ReactNativeTrackerModule.kt +704 -0
- package/android/src/main/java/com/snowplow/reactnativetracker/ReactNativeTrackerPackage.kt +17 -0
- package/android/src/main/java/com/snowplow/reactnativetracker/util/ConfigUtil.kt +271 -0
- package/android/src/main/java/com/snowplow/reactnativetracker/util/EventUtil.kt +213 -0
- package/android/src/main/java/com/snowplow/reactnativetracker/util/TrackerVersion.kt +5 -0
- package/babel.config.js +3 -0
- package/example/.watchmanconfig +1 -0
- package/example/Gemfile +6 -0
- package/example/android/app/build.gradle +129 -0
- package/example/android/app/debug.keystore +0 -0
- package/example/android/app/proguard-rules.pro +10 -0
- package/example/android/app/src/androidTest/java/com/reactnativetrackerexample/DetoxTest.java +36 -0
- package/example/android/app/src/androidTest/java/com/reactnativetrackerexample/DetoxTestAppJUnitRunner.java +21 -0
- package/example/android/app/src/debug/AndroidManifest.xml +13 -0
- package/example/android/app/src/debug/java/com/reactnativetrackerexample/ReactNativeFlipper.java +75 -0
- package/example/android/app/src/main/AndroidManifest.xml +26 -0
- package/example/android/app/src/main/java/com/reactnativetrackerexample/MainActivity.java +32 -0
- package/example/android/app/src/main/java/com/reactnativetrackerexample/MainApplication.java +62 -0
- package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +36 -0
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/values/strings.xml +3 -0
- package/example/android/app/src/main/res/values/styles.xml +9 -0
- package/example/android/app/src/release/java/com/reactnativetrackerexample/ReactNativeFlipper.java +20 -0
- package/example/android/build.gradle +31 -0
- package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/example/android/gradle/wrapper/gradle-wrapper.properties +6 -0
- package/example/android/gradle.properties +44 -0
- package/example/android/gradlew +244 -0
- package/example/android/gradlew.bat +92 -0
- package/example/android/settings.gradle +4 -0
- package/example/app.json +4 -0
- package/example/babel.config.js +17 -0
- package/example/detox.config.js +92 -0
- package/example/e2e/config.json +10 -0
- package/example/e2e/emitEvents.e2e.detox.js +108 -0
- package/example/e2e/environment.js +23 -0
- package/example/e2e/helpers/microCommands.js +153 -0
- package/example/e2e/helpers/microHelpers.js +346 -0
- package/example/e2e/helpers/microHelpers.test.js +308 -0
- package/example/e2e/helpers/schemas.js +62 -0
- package/example/e2e/jest.config.js +16 -0
- package/example/e2e/setup.js +4 -0
- package/example/e2e/testEvents.micro.test.js +300 -0
- package/example/index.js +5 -0
- package/example/ios/.xcode.env +11 -0
- package/example/ios/Podfile +64 -0
- package/example/ios/Podfile.lock +737 -0
- package/example/ios/ReactNativeTrackerExample/AppDelegate.h +6 -0
- package/example/ios/ReactNativeTrackerExample/AppDelegate.mm +26 -0
- package/example/ios/ReactNativeTrackerExample/Images.xcassets/AppIcon.appiconset/Contents.json +53 -0
- package/example/ios/ReactNativeTrackerExample/Images.xcassets/Contents.json +6 -0
- package/example/ios/ReactNativeTrackerExample/Info.plist +55 -0
- package/example/ios/ReactNativeTrackerExample/LaunchScreen.storyboard +47 -0
- package/example/ios/ReactNativeTrackerExample/main.m +10 -0
- package/example/ios/ReactNativeTrackerExample-Bridging-Header.h +3 -0
- package/example/ios/ReactNativeTrackerExample.xcodeproj/project.pbxproj +722 -0
- package/{ios/RNSnowplowTracker.xcodeproj/xcshareddata/xcschemes/RNSnowplowTracker.xcscheme → example/ios/ReactNativeTrackerExample.xcodeproj/xcshareddata/xcschemes/ReactNativeTrackerExample.xcscheme} +29 -21
- package/example/ios/ReactNativeTrackerExample.xcworkspace/contents.xcworkspacedata +10 -0
- package/example/ios/ReactNativeTrackerExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/example/ios/ReactNativeTrackerExampleTests/Info.plist +24 -0
- package/example/ios/ReactNativeTrackerExampleTests/ReactNativeTrackerExampleTests.m +66 -0
- package/example/metro.config.js +45 -0
- package/example/package.json +54 -0
- package/example/react-native.config.js +10 -0
- package/example/src/App.js +557 -0
- package/example/tsconfig.json +5 -0
- package/example/yarn.lock +7379 -0
- package/ios/ReactNativeTracker-Bridging-Header.h +2 -0
- package/ios/ReactNativeTracker.mm +165 -0
- package/ios/ReactNativeTracker.swift +757 -0
- package/ios/{RNSnowplowTracker.xcodeproj → ReactNativeTracker.xcodeproj}/project.pbxproj +55 -79
- package/ios/Util/ConfigUtils.swift +206 -0
- package/ios/Util/TrackerVersion.swift +3 -0
- package/ios/Util/Utilities.swift +37 -0
- package/lefthook.yml +11 -0
- package/lib/commonjs/api.js +475 -0
- package/lib/commonjs/api.js.map +1 -0
- package/lib/commonjs/configurations.js +209 -0
- package/lib/commonjs/configurations.js.map +1 -0
- package/lib/commonjs/constants.js +85 -0
- package/lib/commonjs/constants.js.map +1 -0
- package/lib/commonjs/events.js +256 -0
- package/lib/commonjs/events.js.map +1 -0
- package/lib/commonjs/index.js +141 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/jsCore.js +393 -0
- package/lib/commonjs/jsCore.js.map +1 -0
- package/lib/commonjs/native.js +29 -0
- package/lib/commonjs/native.js.map +1 -0
- package/lib/commonjs/subject.js +248 -0
- package/lib/commonjs/subject.js.map +1 -0
- package/lib/commonjs/tracker.js +221 -0
- package/lib/commonjs/tracker.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils.js +85 -0
- package/lib/commonjs/utils.js.map +1 -0
- package/lib/commonjs/webViewInterface.js +79 -0
- package/lib/commonjs/webViewInterface.js.map +1 -0
- package/lib/module/api.js +439 -0
- package/lib/module/api.js.map +1 -0
- package/lib/module/configurations.js +197 -0
- package/lib/module/configurations.js.map +1 -0
- package/lib/module/constants.js +80 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/events.js +240 -0
- package/lib/module/events.js.map +1 -0
- package/lib/module/index.js +129 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/jsCore.js +390 -0
- package/lib/module/jsCore.js.map +1 -0
- package/lib/module/native.js +25 -0
- package/lib/module/native.js.map +1 -0
- package/lib/module/subject.js +235 -0
- package/lib/module/subject.js.map +1 -0
- package/lib/module/tracker.js +209 -0
- package/lib/module/tracker.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils.js +79 -0
- package/lib/module/utils.js.map +1 -0
- package/lib/module/webViewInterface.js +75 -0
- package/lib/module/webViewInterface.js.map +1 -0
- package/lib/typescript/__tests__/api.test.d.ts +2 -0
- package/lib/typescript/__tests__/api.test.d.ts.map +1 -0
- package/lib/typescript/__tests__/configurations.test.d.ts +2 -0
- package/lib/typescript/__tests__/configurations.test.d.ts.map +1 -0
- package/lib/typescript/__tests__/events.test.d.ts +2 -0
- package/lib/typescript/__tests__/events.test.d.ts.map +1 -0
- package/lib/typescript/__tests__/index.test.d.ts +2 -0
- package/lib/typescript/__tests__/index.test.d.ts.map +1 -0
- package/lib/typescript/__tests__/jsCore.test.d.ts +2 -0
- package/lib/typescript/__tests__/jsCore.test.d.ts.map +1 -0
- package/lib/typescript/__tests__/utils.test.d.ts +2 -0
- package/lib/typescript/__tests__/utils.test.d.ts.map +1 -0
- package/lib/typescript/api.d.ts +226 -0
- package/lib/typescript/api.d.ts.map +1 -0
- package/lib/typescript/configurations.d.ts +73 -0
- package/lib/typescript/configurations.d.ts.map +1 -0
- package/lib/typescript/constants.d.ts +59 -0
- package/lib/typescript/constants.d.ts.map +1 -0
- package/lib/typescript/events.d.ts +101 -0
- package/lib/typescript/events.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +27 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/jsCore.d.ts +140 -0
- package/lib/typescript/jsCore.d.ts.map +1 -0
- package/lib/typescript/native.d.ts +4 -0
- package/lib/typescript/native.d.ts.map +1 -0
- package/lib/typescript/subject.d.ts +91 -0
- package/lib/typescript/subject.d.ts.map +1 -0
- package/lib/typescript/tracker.d.ts +93 -0
- package/lib/typescript/tracker.d.ts.map +1 -0
- package/{dist/index.d.ts → lib/typescript/types.d.ts} +188 -75
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/utils.d.ts +35 -0
- package/lib/typescript/utils.d.ts.map +1 -0
- package/lib/typescript/webViewInterface.d.ts +15 -0
- package/lib/typescript/webViewInterface.d.ts.map +1 -0
- package/package.json +134 -52
- package/scripts/bootstrap.js +29 -0
- package/snowplow-react-native-tracker.podspec +42 -0
- package/src/__mocks__/react-native.js +7 -0
- package/src/__tests__/api.test.ts +34 -0
- package/src/__tests__/configurations.test.ts +620 -0
- package/src/__tests__/events.test.ts +749 -0
- package/src/__tests__/index.test.ts +27 -0
- package/src/__tests__/jsCore.test.ts +169 -0
- package/src/__tests__/utils.test.ts +23 -0
- package/src/api.ts +535 -0
- package/src/configurations.ts +337 -0
- package/src/constants.ts +102 -0
- package/src/events.ts +330 -0
- package/src/index.ts +189 -0
- package/src/jsCore.ts +553 -0
- package/src/native.ts +34 -0
- package/src/subject.ts +315 -0
- package/src/tracker.ts +330 -0
- package/src/types.ts +1087 -0
- package/src/utils.ts +78 -0
- package/src/webViewInterface.ts +105 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +28 -0
- package/turbo.json +34 -0
- package/yarn.lock +9333 -0
- package/RNSnowplowTracker.podspec +0 -24
- package/android/src/main/java/com/snowplowanalytics/react/tracker/RNSnowplowTrackerModule.java +0 -750
- package/android/src/main/java/com/snowplowanalytics/react/tracker/RNSnowplowTrackerPackage.java +0 -29
- package/android/src/main/java/com/snowplowanalytics/react/util/ConfigUtil.java +0 -351
- package/android/src/main/java/com/snowplowanalytics/react/util/EventUtil.java +0 -339
- package/android/src/main/java/com/snowplowanalytics/react/util/TrackerVersion.java +0 -7
- package/dist/index.js +0 -2026
- package/dist/index.js.map +0 -1
- package/ios/RNSnowplowTracker.h +0 -34
- package/ios/RNSnowplowTracker.m +0 -911
- package/ios/Util/NSDictionary+RNSP_TypeMethods.h +0 -33
- package/ios/Util/NSDictionary+RNSP_TypeMethods.m +0 -40
- package/ios/Util/RNConfigUtils.h +0 -44
- package/ios/Util/RNConfigUtils.m +0 -209
- package/ios/Util/RNTrackerVersion.h +0 -27
- package/ios/Util/RNTrackerVersion.m +0 -27
- package/ios/Util/RNUtilities.h +0 -32
- package/ios/Util/RNUtilities.m +0 -68
package/src/events.ts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This program is licensed to you under the Apache License Version 2.0,
|
|
5
|
+
* and you may not use this file except in compliance with the Apache License Version 2.0.
|
|
6
|
+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
|
|
7
|
+
*
|
|
8
|
+
* Unless required by applicable law or agreed to in writing,
|
|
9
|
+
* software distributed under the Apache License Version 2.0 is distributed on an
|
|
10
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
'use strict';
|
|
15
|
+
|
|
16
|
+
import { isObject } from './utils';
|
|
17
|
+
import { logMessages } from './constants';
|
|
18
|
+
import type {
|
|
19
|
+
SelfDescribing,
|
|
20
|
+
EventContext,
|
|
21
|
+
ScreenViewProps,
|
|
22
|
+
StructuredProps,
|
|
23
|
+
PageViewProps,
|
|
24
|
+
TimingProps,
|
|
25
|
+
ConsentGrantedProps,
|
|
26
|
+
ConsentWithdrawnProps,
|
|
27
|
+
EcommerceItem,
|
|
28
|
+
EcommerceTransactionProps,
|
|
29
|
+
DeepLinkReceivedProps,
|
|
30
|
+
MessageNotificationProps,
|
|
31
|
+
} from './types';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Validates whether an object is valid self-describing
|
|
35
|
+
*
|
|
36
|
+
* @param sd {Object} - the object to validate
|
|
37
|
+
* @returns - boolean
|
|
38
|
+
*/
|
|
39
|
+
function isValidSD(sd: SelfDescribing | EventContext): boolean {
|
|
40
|
+
return isObject(sd) && typeof sd.schema === 'string' && isObject(sd.data);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Validates whether an object is a valid array of contexts
|
|
45
|
+
*
|
|
46
|
+
* @param contexts {Object} - the object to validate
|
|
47
|
+
* @returns - boolean promise
|
|
48
|
+
*/
|
|
49
|
+
function validateContexts(contexts: EventContext[]): Promise<boolean> {
|
|
50
|
+
const isValid =
|
|
51
|
+
Object.prototype.toString.call(contexts) === '[object Array]' &&
|
|
52
|
+
contexts
|
|
53
|
+
.map((c) => isValidSD(c))
|
|
54
|
+
.reduce((acc, curr) => acc !== false && curr, true);
|
|
55
|
+
|
|
56
|
+
if (!isValid) {
|
|
57
|
+
return Promise.reject(new Error(logMessages.context));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return Promise.resolve(true);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Validates whether an object is valid self describing
|
|
65
|
+
*
|
|
66
|
+
* @param argmap {Object} - the object to validate
|
|
67
|
+
* @returns - boolean promise
|
|
68
|
+
*/
|
|
69
|
+
function validateSelfDesc(argmap: SelfDescribing): Promise<boolean> {
|
|
70
|
+
if (!isValidSD(argmap)) {
|
|
71
|
+
return Promise.reject(new Error(logMessages.selfDesc));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Promise.resolve(true);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Validates a screen view event
|
|
79
|
+
*
|
|
80
|
+
* @param argmap {Object} - the object to validate
|
|
81
|
+
* @returns - boolean promise
|
|
82
|
+
*/
|
|
83
|
+
function validateScreenView(argmap: ScreenViewProps): Promise<boolean> {
|
|
84
|
+
// validate type
|
|
85
|
+
if (!isObject(argmap)) {
|
|
86
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
87
|
+
}
|
|
88
|
+
// validate required props
|
|
89
|
+
if (typeof argmap.name !== 'string') {
|
|
90
|
+
return Promise.reject(new Error(logMessages.screenViewReq));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return Promise.resolve(true);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Validates a structured event
|
|
98
|
+
*
|
|
99
|
+
* @param argmap {Object} - the object to validate
|
|
100
|
+
* @returns - boolean promise
|
|
101
|
+
*/
|
|
102
|
+
function validateStructured(argmap: StructuredProps): Promise<boolean> {
|
|
103
|
+
// validate type
|
|
104
|
+
if (!isObject(argmap)) {
|
|
105
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
106
|
+
}
|
|
107
|
+
// validate required props
|
|
108
|
+
if (
|
|
109
|
+
typeof argmap.category !== 'string' ||
|
|
110
|
+
typeof argmap.action !== 'string'
|
|
111
|
+
) {
|
|
112
|
+
return Promise.reject(new Error(logMessages.structuredReq));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return Promise.resolve(true);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Validates a page-view event
|
|
120
|
+
*
|
|
121
|
+
* @param argmap {Object} - the object to validate
|
|
122
|
+
* @returns - boolean promise
|
|
123
|
+
*/
|
|
124
|
+
function validatePageView(argmap: PageViewProps): Promise<boolean> {
|
|
125
|
+
// validate type
|
|
126
|
+
if (!isObject(argmap)) {
|
|
127
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
128
|
+
}
|
|
129
|
+
// validate required props
|
|
130
|
+
if (typeof argmap.pageUrl !== 'string') {
|
|
131
|
+
return Promise.reject(new Error(logMessages.pageviewReq));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return Promise.resolve(true);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Validates a timing event
|
|
139
|
+
*
|
|
140
|
+
* @param argmap {Object} - the object to validate
|
|
141
|
+
* @returns - boolean promise
|
|
142
|
+
*/
|
|
143
|
+
function validateTiming(argmap: TimingProps): Promise<boolean> {
|
|
144
|
+
// validate type
|
|
145
|
+
if (!isObject(argmap)) {
|
|
146
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
147
|
+
}
|
|
148
|
+
// validate required props
|
|
149
|
+
if (
|
|
150
|
+
typeof argmap.category !== 'string' ||
|
|
151
|
+
typeof argmap.variable !== 'string' ||
|
|
152
|
+
typeof argmap.timing !== 'number'
|
|
153
|
+
) {
|
|
154
|
+
return Promise.reject(new Error(logMessages.timingReq));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return Promise.resolve(true);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Validates a consent-granted event
|
|
162
|
+
*
|
|
163
|
+
* @param argmap {Object} - the object to validate
|
|
164
|
+
* @returns - boolean promise
|
|
165
|
+
*/
|
|
166
|
+
function validateConsentGranted(argmap: ConsentGrantedProps): Promise<boolean> {
|
|
167
|
+
// validate type
|
|
168
|
+
if (!isObject(argmap)) {
|
|
169
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
170
|
+
}
|
|
171
|
+
// validate required props
|
|
172
|
+
if (
|
|
173
|
+
typeof argmap.expiry !== 'string' ||
|
|
174
|
+
typeof argmap.documentId !== 'string' ||
|
|
175
|
+
typeof argmap.version !== 'string'
|
|
176
|
+
) {
|
|
177
|
+
return Promise.reject(new Error(logMessages.consentGReq));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return Promise.resolve(true);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Validates a consent-withdrawn event
|
|
185
|
+
*
|
|
186
|
+
* @param argmap {Object} - the object to validate
|
|
187
|
+
* @returns - boolean promise
|
|
188
|
+
*/
|
|
189
|
+
function validateConsentWithdrawn(
|
|
190
|
+
argmap: ConsentWithdrawnProps
|
|
191
|
+
): Promise<boolean> {
|
|
192
|
+
// validate type
|
|
193
|
+
if (!isObject(argmap)) {
|
|
194
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
195
|
+
}
|
|
196
|
+
// validate required props
|
|
197
|
+
if (
|
|
198
|
+
typeof argmap.all !== 'boolean' ||
|
|
199
|
+
typeof argmap.documentId !== 'string' ||
|
|
200
|
+
typeof argmap.version !== 'string'
|
|
201
|
+
) {
|
|
202
|
+
return Promise.reject(new Error(logMessages.consentWReq));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return Promise.resolve(true);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Validates a deep link received event
|
|
210
|
+
*
|
|
211
|
+
* @param argmap {Object} - the object to validate
|
|
212
|
+
* @returns - boolean promise
|
|
213
|
+
*/
|
|
214
|
+
function validateDeepLinkReceived(
|
|
215
|
+
argmap: DeepLinkReceivedProps
|
|
216
|
+
): Promise<boolean> {
|
|
217
|
+
// validate type
|
|
218
|
+
if (!isObject(argmap)) {
|
|
219
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
220
|
+
}
|
|
221
|
+
// validate required props
|
|
222
|
+
if (typeof argmap.url !== 'string') {
|
|
223
|
+
return Promise.reject(new Error(logMessages.deepLinkReq));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return Promise.resolve(true);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Validates a message notification event
|
|
231
|
+
*
|
|
232
|
+
* @param argmap {Object} - the object to validate
|
|
233
|
+
* @returns - boolean promise
|
|
234
|
+
*/
|
|
235
|
+
function validateMessageNotification(
|
|
236
|
+
argmap: MessageNotificationProps
|
|
237
|
+
): Promise<boolean> {
|
|
238
|
+
// validate type
|
|
239
|
+
if (!isObject(argmap)) {
|
|
240
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
241
|
+
}
|
|
242
|
+
// validate required props
|
|
243
|
+
if (
|
|
244
|
+
typeof argmap.title !== 'string' ||
|
|
245
|
+
typeof argmap.body !== 'string' ||
|
|
246
|
+
typeof argmap.trigger !== 'string' ||
|
|
247
|
+
!['push', 'location', 'calendar', 'timeInterval', 'other'].includes(
|
|
248
|
+
argmap.trigger
|
|
249
|
+
)
|
|
250
|
+
) {
|
|
251
|
+
return Promise.reject(new Error(logMessages.messageNotificationReq));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return Promise.resolve(true);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Validates whether an object is valid ecommerce-item
|
|
259
|
+
*
|
|
260
|
+
* @param item {Object} - the object to validate
|
|
261
|
+
* @returns - boolean
|
|
262
|
+
*/
|
|
263
|
+
function isValidEcomItem(item: EcommerceItem): boolean {
|
|
264
|
+
if (
|
|
265
|
+
isObject(item) &&
|
|
266
|
+
typeof item.sku === 'string' &&
|
|
267
|
+
typeof item.price === 'number' &&
|
|
268
|
+
typeof item.quantity === 'number'
|
|
269
|
+
) {
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Validates an array of ecommerce-items
|
|
277
|
+
*
|
|
278
|
+
* @param items {Object} - the object to validate
|
|
279
|
+
* @returns - boolean promise
|
|
280
|
+
*/
|
|
281
|
+
function validItemsArg(items: EcommerceItem[]): boolean {
|
|
282
|
+
return (
|
|
283
|
+
Object.prototype.toString.call(items) === '[object Array]' &&
|
|
284
|
+
items
|
|
285
|
+
.map((i) => isValidEcomItem(i))
|
|
286
|
+
.reduce((acc, curr) => acc !== false && curr, true)
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Validates an ecommerce-transaction event
|
|
292
|
+
*
|
|
293
|
+
* @param argmap {Object} - the object to validate
|
|
294
|
+
* @returns - boolean promise
|
|
295
|
+
*/
|
|
296
|
+
function validateEcommerceTransaction(
|
|
297
|
+
argmap: EcommerceTransactionProps
|
|
298
|
+
): Promise<boolean> {
|
|
299
|
+
// validate type
|
|
300
|
+
if (!isObject(argmap)) {
|
|
301
|
+
return Promise.reject(new Error(logMessages.evType));
|
|
302
|
+
}
|
|
303
|
+
// validate required props
|
|
304
|
+
if (
|
|
305
|
+
typeof argmap.orderId !== 'string' ||
|
|
306
|
+
typeof argmap.totalValue !== 'number' ||
|
|
307
|
+
!validItemsArg(argmap.items)
|
|
308
|
+
) {
|
|
309
|
+
return Promise.reject(new Error(logMessages.ecomReq));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return Promise.resolve(true);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export {
|
|
316
|
+
isValidSD,
|
|
317
|
+
validateContexts,
|
|
318
|
+
validateSelfDesc,
|
|
319
|
+
validateScreenView,
|
|
320
|
+
validateStructured,
|
|
321
|
+
validatePageView,
|
|
322
|
+
validateTiming,
|
|
323
|
+
validateConsentGranted,
|
|
324
|
+
validateConsentWithdrawn,
|
|
325
|
+
isValidEcomItem,
|
|
326
|
+
validItemsArg,
|
|
327
|
+
validateEcommerceTransaction,
|
|
328
|
+
validateDeepLinkReceived,
|
|
329
|
+
validateMessageNotification,
|
|
330
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020-2023 Snowplow Analytics Ltd. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This program is licensed to you under the Apache License Version 2.0,
|
|
5
|
+
* and you may not use this file except in compliance with the Apache License Version 2.0.
|
|
6
|
+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
|
|
7
|
+
*
|
|
8
|
+
* Unless required by applicable law or agreed to in writing,
|
|
9
|
+
* software distributed under the Apache License Version 2.0 is distributed on an
|
|
10
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
'use strict';
|
|
15
|
+
|
|
16
|
+
import * as api from './api';
|
|
17
|
+
import { safeWait, safeWaitCallback, errorHandler } from './utils';
|
|
18
|
+
import type {
|
|
19
|
+
NetworkConfiguration,
|
|
20
|
+
TrackerControllerConfiguration,
|
|
21
|
+
ReactNativeTracker,
|
|
22
|
+
} from './types';
|
|
23
|
+
import { getWebViewCallback } from './webViewInterface';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Creates a React Native Tracker object
|
|
27
|
+
*
|
|
28
|
+
* @param namespace {string} - The tracker namespace
|
|
29
|
+
* @param networkConfig {Object} - The network configuration
|
|
30
|
+
* @param control {Array} - The tracker controller configuration
|
|
31
|
+
* @returns The tracker object
|
|
32
|
+
*/
|
|
33
|
+
function createTracker(
|
|
34
|
+
namespace: string,
|
|
35
|
+
networkConfig: NetworkConfiguration,
|
|
36
|
+
controllerConfig: TrackerControllerConfiguration = {}
|
|
37
|
+
): ReactNativeTracker {
|
|
38
|
+
// initTrackerPromise
|
|
39
|
+
const initTrackerPromise: Promise<void> = Promise.resolve(
|
|
40
|
+
api.createTracker({
|
|
41
|
+
namespace,
|
|
42
|
+
networkConfig,
|
|
43
|
+
...controllerConfig,
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// mkMethod creates methods subscribed to the initTrackerPromise
|
|
48
|
+
const mkMethod = safeWait(initTrackerPromise, errorHandler);
|
|
49
|
+
|
|
50
|
+
// mkCallback creates callbacks subscribed to the initTrackerPromise
|
|
51
|
+
const mkCallback = safeWaitCallback(initTrackerPromise, errorHandler);
|
|
52
|
+
|
|
53
|
+
// track methods
|
|
54
|
+
const trackSelfDescribingEvent = mkMethod(
|
|
55
|
+
api.trackSelfDescribingEvent(namespace)
|
|
56
|
+
);
|
|
57
|
+
const trackScreenViewEvent = mkMethod(api.trackScreenViewEvent(namespace));
|
|
58
|
+
const trackStructuredEvent = mkMethod(api.trackStructuredEvent(namespace));
|
|
59
|
+
const trackPageViewEvent = mkMethod(api.trackPageViewEvent(namespace));
|
|
60
|
+
const trackTimingEvent = mkMethod(api.trackTimingEvent(namespace));
|
|
61
|
+
const trackConsentGrantedEvent = mkMethod(
|
|
62
|
+
api.trackConsentGrantedEvent(namespace)
|
|
63
|
+
);
|
|
64
|
+
const trackConsentWithdrawnEvent = mkMethod(
|
|
65
|
+
api.trackConsentWithdrawnEvent(namespace)
|
|
66
|
+
);
|
|
67
|
+
const trackEcommerceTransactionEvent = mkMethod(
|
|
68
|
+
api.trackEcommerceTransactionEvent(namespace)
|
|
69
|
+
);
|
|
70
|
+
const trackDeepLinkReceivedEvent = mkMethod(
|
|
71
|
+
api.trackDeepLinkReceivedEvent(namespace)
|
|
72
|
+
);
|
|
73
|
+
const trackMessageNotificationEvent = mkMethod(
|
|
74
|
+
api.trackMessageNotificationEvent(namespace)
|
|
75
|
+
);
|
|
76
|
+
// Global Contexts
|
|
77
|
+
const removeGlobalContexts = mkMethod(api.removeGlobalContexts(namespace));
|
|
78
|
+
const addGlobalContexts = mkMethod(api.addGlobalContexts(namespace));
|
|
79
|
+
// setters
|
|
80
|
+
const setUserId = mkMethod(api.setUserId(namespace));
|
|
81
|
+
const setNetworkUserId = mkMethod(api.setNetworkUserId(namespace));
|
|
82
|
+
const setDomainUserId = mkMethod(api.setDomainUserId(namespace));
|
|
83
|
+
const setIpAddress = mkMethod(api.setIpAddress(namespace));
|
|
84
|
+
const setUseragent = mkMethod(api.setUseragent(namespace));
|
|
85
|
+
const setTimezone = mkMethod(api.setTimezone(namespace));
|
|
86
|
+
const setLanguage = mkMethod(api.setLanguage(namespace));
|
|
87
|
+
const setScreenResolution = mkMethod(api.setScreenResolution(namespace));
|
|
88
|
+
const setScreenViewport = mkMethod(api.setScreenViewport(namespace));
|
|
89
|
+
const setColorDepth = mkMethod(api.setColorDepth(namespace));
|
|
90
|
+
const setSubjectData = mkMethod(api.setSubjectData(namespace));
|
|
91
|
+
|
|
92
|
+
// callbacks
|
|
93
|
+
const getSessionUserId = mkCallback(api.getSessionUserId(namespace));
|
|
94
|
+
const getSessionId = mkCallback(api.getSessionId(namespace));
|
|
95
|
+
const getSessionIndex = mkCallback(api.getSessionIndex(namespace));
|
|
96
|
+
const getIsInBackground = mkCallback(api.getIsInBackground(namespace));
|
|
97
|
+
const getBackgroundIndex = mkCallback(api.getBackgroundIndex(namespace));
|
|
98
|
+
const getForegroundIndex = mkCallback(api.getForegroundIndex(namespace));
|
|
99
|
+
|
|
100
|
+
return Object.freeze({
|
|
101
|
+
trackSelfDescribingEvent,
|
|
102
|
+
trackScreenViewEvent,
|
|
103
|
+
trackStructuredEvent,
|
|
104
|
+
trackPageViewEvent,
|
|
105
|
+
trackTimingEvent,
|
|
106
|
+
trackConsentGrantedEvent,
|
|
107
|
+
trackConsentWithdrawnEvent,
|
|
108
|
+
trackEcommerceTransactionEvent,
|
|
109
|
+
trackDeepLinkReceivedEvent,
|
|
110
|
+
trackMessageNotificationEvent,
|
|
111
|
+
removeGlobalContexts,
|
|
112
|
+
addGlobalContexts,
|
|
113
|
+
setUserId,
|
|
114
|
+
setNetworkUserId,
|
|
115
|
+
setDomainUserId,
|
|
116
|
+
setIpAddress,
|
|
117
|
+
setUseragent,
|
|
118
|
+
setTimezone,
|
|
119
|
+
setLanguage,
|
|
120
|
+
setScreenResolution,
|
|
121
|
+
setScreenViewport,
|
|
122
|
+
setColorDepth,
|
|
123
|
+
setSubjectData,
|
|
124
|
+
getSessionUserId,
|
|
125
|
+
getSessionId,
|
|
126
|
+
getSessionIndex,
|
|
127
|
+
getIsInBackground,
|
|
128
|
+
getBackgroundIndex,
|
|
129
|
+
getForegroundIndex,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Removes a tracker given its namespace
|
|
135
|
+
*
|
|
136
|
+
* @param trackerNamespace {string}
|
|
137
|
+
* @returns - A boolean promise
|
|
138
|
+
*/
|
|
139
|
+
function removeTracker(trackerNamespace: string): Promise<boolean> {
|
|
140
|
+
return <Promise<boolean>>(
|
|
141
|
+
api.removeTracker(trackerNamespace).catch((e) => errorHandler(e))
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Removes all trackers
|
|
147
|
+
*
|
|
148
|
+
* @returns - A boolean promise
|
|
149
|
+
*/
|
|
150
|
+
function removeAllTrackers(): Promise<boolean> {
|
|
151
|
+
return <Promise<boolean>>(
|
|
152
|
+
api.removeAllTrackers().catch((e) => errorHandler(e))
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { createTracker, removeTracker, removeAllTrackers, getWebViewCallback };
|
|
157
|
+
|
|
158
|
+
export type {
|
|
159
|
+
ReactNativeTracker,
|
|
160
|
+
TrackerControllerConfiguration,
|
|
161
|
+
NetworkConfiguration,
|
|
162
|
+
TrackerConfiguration,
|
|
163
|
+
SessionConfiguration,
|
|
164
|
+
EmitterConfiguration,
|
|
165
|
+
SubjectConfiguration,
|
|
166
|
+
GdprConfiguration,
|
|
167
|
+
GCConfiguration,
|
|
168
|
+
SelfDescribing,
|
|
169
|
+
EventContext,
|
|
170
|
+
ScreenViewProps,
|
|
171
|
+
StructuredProps,
|
|
172
|
+
PageViewProps,
|
|
173
|
+
TimingProps,
|
|
174
|
+
ConsentGrantedProps,
|
|
175
|
+
ConsentWithdrawnProps,
|
|
176
|
+
EcommerceTransactionProps,
|
|
177
|
+
DeepLinkReceivedProps,
|
|
178
|
+
MessageNotificationProps,
|
|
179
|
+
EcommerceItem,
|
|
180
|
+
ConsentDocument,
|
|
181
|
+
GlobalContext,
|
|
182
|
+
HttpMethod,
|
|
183
|
+
DevicePlatform,
|
|
184
|
+
LogLevel,
|
|
185
|
+
Basis,
|
|
186
|
+
BufferOption,
|
|
187
|
+
ScreenSize,
|
|
188
|
+
Trigger,
|
|
189
|
+
} from './types';
|