@anglerai/react-native-analytics 0.0.0-stage → 0.1.0-rc.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/AnglerAnalyticsRN.podspec +25 -0
- package/LICENSE +21 -0
- package/README.md +92 -2
- package/android/build.gradle +64 -0
- package/android/src/main/java/ai/getangler/analytics/reactnative/AnglerAnalyticsModule.kt +51 -0
- package/android/src/main/java/ai/getangler/analytics/reactnative/AnglerAnalyticsPackage.kt +25 -0
- package/android/src/main/java/ai/getangler/analytics/reactnative/ConfigParser.kt +58 -0
- package/android/src/test/java/ai/getangler/analytics/reactnative/ConfigParserTest.kt +51 -0
- package/ios/AnglerAnalyticsBridge.swift +58 -0
- package/ios/AnglerAnalyticsRN.mm +84 -0
- package/ios/vendor/AnglerAnalytics/Angler.swift +132 -0
- package/ios/vendor/AnglerAnalytics/AnglerCore.swift +398 -0
- package/ios/vendor/AnglerAnalytics/Attribution/AttributionStore.swift +109 -0
- package/ios/vendor/AnglerAnalytics/Attribution/ClickIdParser.swift +52 -0
- package/ios/vendor/AnglerAnalytics/Device/AdIdProvider.swift +36 -0
- package/ios/vendor/AnglerAnalytics/Device/DeviceContext.swift +111 -0
- package/ios/vendor/AnglerAnalytics/Device/FirebaseIdProvider.swift +35 -0
- package/ios/vendor/AnglerAnalytics/Lifecycle/LifecycleObserver.swift +77 -0
- package/ios/vendor/AnglerAnalytics/Lifecycle/SharedApplication.swift +15 -0
- package/ios/vendor/AnglerAnalytics/Log.swift +24 -0
- package/ios/vendor/AnglerAnalytics/Network/Flusher.swift +112 -0
- package/ios/vendor/AnglerAnalytics/Network/ResponsePolicy.swift +29 -0
- package/ios/vendor/AnglerAnalytics/Network/Scheduler.swift +13 -0
- package/ios/vendor/AnglerAnalytics/Network/Transport.swift +55 -0
- package/ios/vendor/AnglerAnalytics/Payload/JSONValue.swift +81 -0
- package/ios/vendor/AnglerAnalytics/Payload/Models.generated.swift +580 -0
- package/ios/vendor/AnglerAnalytics/Payload/PayloadBuilder.swift +148 -0
- package/ios/vendor/AnglerAnalytics/Payload/StandardEvent.swift +26 -0
- package/ios/vendor/AnglerAnalytics/Payload/TypedModels.swift +14 -0
- package/ios/vendor/AnglerAnalytics/Resources/PrivacyInfo.xcprivacy +77 -0
- package/ios/vendor/AnglerAnalytics/Storage/Clock.swift +14 -0
- package/ios/vendor/AnglerAnalytics/Storage/EventQueue.swift +85 -0
- package/ios/vendor/AnglerAnalytics/Storage/IdentityStore.swift +76 -0
- package/ios/vendor/AnglerAnalytics/Storage/KeyValueStore.swift +38 -0
- package/ios/vendor/AnglerAnalytics/Version.swift +4 -0
- package/package.json +46 -4
- package/src/NativeAnglerAnalytics.ts +21 -0
- package/src/index.ts +159 -0
- package/src/models.generated.ts +347 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
# Not "AnglerAnalytics", so an app can also use the SPM package without a module clash.
|
|
7
|
+
s.name = "AnglerAnalyticsRN"
|
|
8
|
+
s.version = package["version"]
|
|
9
|
+
s.summary = package["description"]
|
|
10
|
+
s.homepage = "https://github.com/angler-ai/angler-mobile-sdk"
|
|
11
|
+
s.license = package["license"]
|
|
12
|
+
s.authors = "Angler AI"
|
|
13
|
+
s.source = { :git => "https://github.com/angler-ai/angler-mobile-sdk.git", :tag => s.version.to_s }
|
|
14
|
+
s.ios.deployment_target = "15.1"
|
|
15
|
+
s.swift_version = "5.9"
|
|
16
|
+
|
|
17
|
+
# The Swift core is compiled from source (copied in by scripts/sync-native.mjs on prepack),
|
|
18
|
+
# so there's no dependency on a CocoaPods trunk pod.
|
|
19
|
+
s.source_files = "ios/*.{h,mm,swift}", "ios/vendor/AnglerAnalytics/**/*.swift"
|
|
20
|
+
s.resource_bundles = {
|
|
21
|
+
"AnglerAnalyticsPrivacy" => ["ios/vendor/AnglerAnalytics/Resources/PrivacyInfo.xcprivacy"]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
install_modules_dependencies(s)
|
|
25
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Angler AI
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,93 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @anglerai/react-native-analytics
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Angler app events and ad-attribution signals for React Native (iOS and Android), over the
|
|
4
|
+
native Angler SDKs. React Native 0.76+ with the New Architecture.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @anglerai/react-native-analytics
|
|
10
|
+
cd ios && pod install
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
iOS compiles the Swift core from source inside the package (no extra pod). Android depends on
|
|
14
|
+
the Maven artifact `ai.getangler:analytics` at the same version.
|
|
15
|
+
|
|
16
|
+
## Configure and track
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Angler, AnglerEvent } from '@anglerai/react-native-analytics';
|
|
20
|
+
|
|
21
|
+
// Once at startup, e.g. in index.js or your root component's module scope.
|
|
22
|
+
// Calling it again (Fast Refresh) is ignored.
|
|
23
|
+
Angler.configure({ workspaceId: '<workspace id>', token: '<workspace token>' });
|
|
24
|
+
|
|
25
|
+
// Standard events; `data` is typed (AnglerData, generated from data-api's schema)
|
|
26
|
+
Angler.track(AnglerEvent.productAddedToCart, {
|
|
27
|
+
data: { cartLine: { quantity: 1, merchandise: { id: 'variant-1', price: { amount: 29, currencyCode: 'USD' } } } },
|
|
28
|
+
});
|
|
29
|
+
Angler.track(AnglerEvent.checkoutCompleted, {
|
|
30
|
+
data: { checkout: { currencyCode: 'USD', totalPrice: { amount: 58, currencyCode: 'USD' } } },
|
|
31
|
+
eventId: 'order-123', // your own ID, for deduplication
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Any other string is a custom event; properties become data.customData
|
|
35
|
+
Angler.track('level_up', { properties: { level: '2' } });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`AnglerEvent` lists the standard event names. `AnglerData` has one field per data object data-api
|
|
39
|
+
accepts (`cart`, `cartLine`, `checkout`, `collection`, `productVariant`, `searchResult`,
|
|
40
|
+
`customer`, `form`, `contacts`, `customData`); the nested types (`AnglerCartLine`,
|
|
41
|
+
`AnglerMoney`, …) are exported too.
|
|
42
|
+
|
|
43
|
+
Options: `domain` (`dev-data.getangler.ai` for testing), `clientId`, `appStoreId`, `trackLifecycleEvents`,
|
|
44
|
+
`flushAt`, `flushIntervalSeconds`, `maxQueueSize`, `debugLogging`, `autoHandleDeepLinks`.
|
|
45
|
+
|
|
46
|
+
No method throws: problems are logged with `console.warn` in development. `data` that can't be
|
|
47
|
+
serialized (cycles, `BigInt`) is dropped and the event is still sent.
|
|
48
|
+
|
|
49
|
+
## Deep links
|
|
50
|
+
|
|
51
|
+
With `autoHandleDeepLinks` (the default), the SDK forwards the launch URL
|
|
52
|
+
(`Linking.getInitialURL()`) and every later `Linking` URL event. Your app still has to deliver
|
|
53
|
+
URLs to `Linking`, as React Native's docs describe. On iOS, add this to `AppDelegate.mm`:
|
|
54
|
+
|
|
55
|
+
```objc
|
|
56
|
+
#import <React/RCTLinkingManager.h>
|
|
57
|
+
|
|
58
|
+
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url
|
|
59
|
+
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
|
|
60
|
+
{
|
|
61
|
+
return [RCTLinkingManager application:app openURL:url options:options];
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
To filter URLs yourself, set `autoHandleDeepLinks: false` and call `Angler.handleDeepLink(url)`.
|
|
66
|
+
|
|
67
|
+
## Identity, consent and opt-out
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
Angler.setUserId('customer-42'); // customer_id; null clears it
|
|
71
|
+
Angler.setIdentifier('appsflyer_id', afId); // custom identifiers; null removes
|
|
72
|
+
Angler.setApplicationTrackingEnabled(false); // sent as application_tracking_enabled
|
|
73
|
+
Angler.setEnabled(false); // stop collecting and delete queued events
|
|
74
|
+
Angler.reset(); // e.g. on logout: new install ID
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The SDK never shows the iOS ATT prompt; your app owns it (for example with
|
|
78
|
+
`react-native-tracking-transparency`). The IDFA is sent only when ATT is authorized. On
|
|
79
|
+
Android, see the core's notes on the `AD_ID` permission.
|
|
80
|
+
|
|
81
|
+
If the app uses Firebase Analytics, its app instance ID is sent as identifier
|
|
82
|
+
`firebase_app_instance_id` (for Google Ads and GA4); nothing extra to install. On iOS, set
|
|
83
|
+
`appStoreId` (`id1234567890`) so events carry the `app_id` TikTok's app events need; Android
|
|
84
|
+
sends the package name.
|
|
85
|
+
|
|
86
|
+
## What gets sent
|
|
87
|
+
|
|
88
|
+
- **The whole deep-link URL is sent** as `url` on every event until the app has been in the
|
|
89
|
+
background for 30 minutes, and every `Linking` URL is forwarded by default. If your app
|
|
90
|
+
receives URLs carrying secrets (OAuth `code`, magic-link or password-reset tokens, emails),
|
|
91
|
+
set `autoHandleDeepLinks: false` and pass only cleaned URLs.
|
|
92
|
+
- **`app_installed` means "first run with the SDK".** When you add the SDK to an app that's
|
|
93
|
+
already published, existing users send `app_installed` on their first launch after updating.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Android TurboModule for @anglerai/react-native-analytics. Depends on the Kotlin core from
|
|
2
|
+
// Maven at the same version as this package.
|
|
3
|
+
import groovy.json.JsonSlurper
|
|
4
|
+
|
|
5
|
+
buildscript {
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
dependencies {
|
|
11
|
+
classpath "com.android.tools.build:gradle:8.7.3"
|
|
12
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
def packageJson = new JsonSlurper().parseText(file("../package.json").text)
|
|
17
|
+
def anglerVersion = packageJson.version
|
|
18
|
+
|
|
19
|
+
apply plugin: "com.android.library"
|
|
20
|
+
apply plugin: "kotlin-android"
|
|
21
|
+
apply plugin: "com.facebook.react"
|
|
22
|
+
|
|
23
|
+
def safeExtGet(prop, fallback) {
|
|
24
|
+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
android {
|
|
28
|
+
namespace "ai.getangler.analytics.reactnative"
|
|
29
|
+
compileSdkVersion safeExtGet("compileSdkVersion", 35)
|
|
30
|
+
|
|
31
|
+
defaultConfig {
|
|
32
|
+
minSdkVersion safeExtGet("minSdkVersion", 24)
|
|
33
|
+
targetSdkVersion safeExtGet("targetSdkVersion", 35)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
compileOptions {
|
|
37
|
+
// React Native 0.76 builds with Java 17.
|
|
38
|
+
sourceCompatibility JavaVersion.VERSION_17
|
|
39
|
+
targetCompatibility JavaVersion.VERSION_17
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
kotlinOptions {
|
|
43
|
+
jvmTarget = "17"
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
sourceSets {
|
|
47
|
+
main {
|
|
48
|
+
java.srcDirs += ["generated/java", "generated/jni"]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
repositories {
|
|
54
|
+
mavenCentral()
|
|
55
|
+
google()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
dependencies {
|
|
59
|
+
implementation "com.facebook.react:react-android"
|
|
60
|
+
implementation "ai.getangler:analytics:$anglerVersion"
|
|
61
|
+
|
|
62
|
+
testImplementation "junit:junit:4.13.2"
|
|
63
|
+
testImplementation "org.json:json:20240303"
|
|
64
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package ai.getangler.analytics.reactnative
|
|
2
|
+
|
|
3
|
+
import ai.getangler.analytics.Angler
|
|
4
|
+
import android.util.Log
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
7
|
+
|
|
8
|
+
/** TurboModule `AnglerAnalytics`: forwards every spec method to the Kotlin core. Never throws. */
|
|
9
|
+
@ReactModule(name = AnglerAnalyticsModule.NAME)
|
|
10
|
+
class AnglerAnalyticsModule(reactContext: ReactApplicationContext) : NativeAnglerAnalyticsSpec(reactContext) {
|
|
11
|
+
companion object {
|
|
12
|
+
const val NAME = "AnglerAnalytics"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
override fun getName(): String = NAME
|
|
16
|
+
|
|
17
|
+
override fun configure(configJson: String) = guard {
|
|
18
|
+
val config = ConfigParser.parse(configJson)
|
|
19
|
+
if (config == null) {
|
|
20
|
+
Log.w("Angler", "configure: invalid configuration JSON; ignored")
|
|
21
|
+
} else {
|
|
22
|
+
Angler.configure(reactApplicationContext.applicationContext, config)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
override fun track(name: String, dataJson: String?, propertiesJson: String?, eventId: String?) = guard {
|
|
27
|
+
Angler.track(name, ConfigParser.parseMap(dataJson), ConfigParser.parseStringMap(propertiesJson), eventId)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override fun handleDeepLink(url: String) = guard { Angler.handleDeepLink(url) }
|
|
31
|
+
|
|
32
|
+
override fun setUserId(userId: String?) = guard { Angler.setUserId(userId) }
|
|
33
|
+
|
|
34
|
+
override fun setIdentifier(name: String, value: String?) = guard { Angler.setIdentifier(name, value) }
|
|
35
|
+
|
|
36
|
+
override fun setApplicationTrackingEnabled(enabled: Boolean) = guard { Angler.setApplicationTrackingEnabled(enabled) }
|
|
37
|
+
|
|
38
|
+
override fun setEnabled(enabled: Boolean) = guard { Angler.setEnabled(enabled) }
|
|
39
|
+
|
|
40
|
+
override fun flush() = guard { Angler.flush() }
|
|
41
|
+
|
|
42
|
+
override fun reset() = guard { Angler.reset() }
|
|
43
|
+
|
|
44
|
+
private inline fun guard(block: () -> Unit) {
|
|
45
|
+
try {
|
|
46
|
+
block()
|
|
47
|
+
} catch (t: Throwable) {
|
|
48
|
+
Log.e("Angler", "react-native call failed: $t")
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package ai.getangler.analytics.reactnative
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
class AnglerAnalyticsPackage : BaseReactPackage() {
|
|
10
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
|
|
11
|
+
if (name == AnglerAnalyticsModule.NAME) AnglerAnalyticsModule(reactContext) else null
|
|
12
|
+
|
|
13
|
+
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
|
|
14
|
+
mapOf(
|
|
15
|
+
AnglerAnalyticsModule.NAME to ReactModuleInfo(
|
|
16
|
+
AnglerAnalyticsModule.NAME,
|
|
17
|
+
AnglerAnalyticsModule::class.java.name,
|
|
18
|
+
false, // canOverrideExistingModule
|
|
19
|
+
false, // needsEagerInit
|
|
20
|
+
false, // isCxxModule
|
|
21
|
+
true, // isTurboModule
|
|
22
|
+
)
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
package ai.getangler.analytics.reactnative
|
|
2
|
+
|
|
3
|
+
import ai.getangler.analytics.AnglerConfiguration
|
|
4
|
+
import org.json.JSONArray
|
|
5
|
+
import org.json.JSONObject
|
|
6
|
+
|
|
7
|
+
/** Parses the JSON strings `src/index.ts` sends over the bridge. Never throws. */
|
|
8
|
+
internal object ConfigParser {
|
|
9
|
+
/** Null on invalid JSON or a missing `workspaceId` / `token`. Sets `wrapper = "react-native"`. */
|
|
10
|
+
fun parse(json: String): AnglerConfiguration? {
|
|
11
|
+
val o = try { JSONObject(json) } catch (e: Exception) { return null }
|
|
12
|
+
val workspaceId = o.optString("workspaceId").takeIf { o.has("workspaceId") && it.isNotEmpty() } ?: return null
|
|
13
|
+
val token = o.optString("token").takeIf { o.has("token") && it.isNotEmpty() } ?: return null
|
|
14
|
+
val defaults = AnglerConfiguration(workspaceId, token)
|
|
15
|
+
return AnglerConfiguration(
|
|
16
|
+
workspaceId = workspaceId,
|
|
17
|
+
token = token,
|
|
18
|
+
domain = o.stringOrNull("domain") ?: defaults.domain,
|
|
19
|
+
clientId = o.stringOrNull("clientId"),
|
|
20
|
+
trackLifecycleEvents = if (o.has("trackLifecycleEvents")) o.optBoolean("trackLifecycleEvents", defaults.trackLifecycleEvents) else defaults.trackLifecycleEvents,
|
|
21
|
+
flushAt = if (o.has("flushAt")) o.optInt("flushAt", defaults.flushAt) else defaults.flushAt,
|
|
22
|
+
// Whole seconds on Android: round a fraction up so 0.5 doesn't become 0 (no timer).
|
|
23
|
+
flushIntervalSeconds = if (o.has("flushIntervalSeconds")) Math.ceil(o.optDouble("flushIntervalSeconds", defaults.flushIntervalSeconds.toDouble())).toLong() else defaults.flushIntervalSeconds,
|
|
24
|
+
maxQueueSize = if (o.has("maxQueueSize")) o.optInt("maxQueueSize", defaults.maxQueueSize) else defaults.maxQueueSize,
|
|
25
|
+
debugLogging = if (o.has("debugLogging")) o.optBoolean("debugLogging", defaults.debugLogging) else defaults.debugLogging,
|
|
26
|
+
).apply { wrapper = "react-native" }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A JSON object → `Map`/`List`/primitives (JSON null → null). Null for null or invalid input. */
|
|
30
|
+
fun parseMap(json: String?): Map<String, Any?>? {
|
|
31
|
+
if (json == null) return null
|
|
32
|
+
return try { toMap(JSONObject(json)) } catch (e: Exception) { null }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A JSON object with string values; non-string values are dropped. */
|
|
36
|
+
fun parseStringMap(json: String?): Map<String, String>? =
|
|
37
|
+
parseMap(json)?.mapNotNull { (k, v) -> (v as? String)?.let { k to it } }?.toMap()
|
|
38
|
+
|
|
39
|
+
private fun JSONObject.stringOrNull(key: String): String? =
|
|
40
|
+
if (has(key) && !isNull(key)) optString(key).takeIf { it.isNotEmpty() } else null
|
|
41
|
+
|
|
42
|
+
private fun toMap(o: JSONObject): Map<String, Any?> {
|
|
43
|
+
val out = LinkedHashMap<String, Any?>()
|
|
44
|
+
val keys = o.keys()
|
|
45
|
+
while (keys.hasNext()) {
|
|
46
|
+
val k = keys.next()
|
|
47
|
+
out[k] = convert(o.opt(k))
|
|
48
|
+
}
|
|
49
|
+
return out
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private fun convert(v: Any?): Any? = when (v) {
|
|
53
|
+
null, JSONObject.NULL -> null
|
|
54
|
+
is JSONObject -> toMap(v)
|
|
55
|
+
is JSONArray -> (0 until v.length()).map { convert(v.opt(it)) }
|
|
56
|
+
else -> v
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package ai.getangler.analytics.reactnative
|
|
2
|
+
|
|
3
|
+
import org.junit.Assert.assertEquals
|
|
4
|
+
import org.junit.Assert.assertNull
|
|
5
|
+
import org.junit.Test
|
|
6
|
+
|
|
7
|
+
class ConfigParserTest {
|
|
8
|
+
@Test fun parsesFieldsAndKeepsDefaults() {
|
|
9
|
+
val c = ConfigParser.parse("""{"workspaceId":"ws","token":"t","flushAt":5,"wrapper":"react-native"}""")!!
|
|
10
|
+
assertEquals(5, c.flushAt)
|
|
11
|
+
assertEquals("data.getangler.ai", c.domain)
|
|
12
|
+
assertEquals("react-native", c.wrapper)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@Test fun parsesEveryOptionalField() {
|
|
16
|
+
val c = ConfigParser.parse(
|
|
17
|
+
"""{"workspaceId":"ws","token":"t","domain":"dev-data.getangler.ai","clientId":"crm-1",
|
|
18
|
+
"trackLifecycleEvents":false,"flushIntervalSeconds":15,"maxQueueSize":50,"debugLogging":true}"""
|
|
19
|
+
)!!
|
|
20
|
+
assertEquals("dev-data.getangler.ai", c.domain)
|
|
21
|
+
assertEquals("crm-1", c.clientId)
|
|
22
|
+
assertEquals(false, c.trackLifecycleEvents)
|
|
23
|
+
assertEquals(15L, c.flushIntervalSeconds)
|
|
24
|
+
assertEquals(50, c.maxQueueSize)
|
|
25
|
+
assertEquals(true, c.debugLogging)
|
|
26
|
+
assertEquals("react-native", c.wrapper)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Test fun fractionalFlushIntervalRoundsUp() {
|
|
30
|
+
assertEquals(1L, ConfigParser.parse("""{"workspaceId":"ws","token":"t","flushIntervalSeconds":0.5}""")!!.flushIntervalSeconds)
|
|
31
|
+
assertEquals(3L, ConfigParser.parse("""{"workspaceId":"ws","token":"t","flushIntervalSeconds":2.5}""")!!.flushIntervalSeconds)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@Test fun missingRequiredFieldsReturnNull() = assertNull(ConfigParser.parse("{}"))
|
|
35
|
+
|
|
36
|
+
@Test fun invalidJsonReturnsNull() = assertNull(ConfigParser.parse("nope"))
|
|
37
|
+
|
|
38
|
+
@Test fun parseMapTurnsNestedJsonIntoMapsAndLists() {
|
|
39
|
+
val m = ConfigParser.parseMap("""{"productVariant":{"id":"v1","tags":["a",1,true,null]},"n":null}""")!!
|
|
40
|
+
assertEquals(mapOf("id" to "v1", "tags" to listOf("a", 1, true, null)), m["productVariant"])
|
|
41
|
+
assertEquals(true, m.containsKey("n"))
|
|
42
|
+
assertNull(m["n"])
|
|
43
|
+
assertNull(ConfigParser.parseMap(null))
|
|
44
|
+
assertNull(ConfigParser.parseMap("[1]"))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Test fun parseStringMapKeepsOnlyStrings() {
|
|
48
|
+
assertEquals(mapOf("a" to "1"), ConfigParser.parseStringMap("""{"a":"1","b":2}"""))
|
|
49
|
+
assertNull(ConfigParser.parseStringMap("bad"))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// The React Native module's entry into the Swift core (compiled into the same pod from
|
|
4
|
+
/// `ios/vendor/AnglerAnalytics`). One method per spec method, taking the same JSON strings.
|
|
5
|
+
@objc(AnglerAnalyticsBridge)
|
|
6
|
+
public final class AnglerAnalyticsBridge: NSObject {
|
|
7
|
+
@objc public static func configure(_ configJson: String) {
|
|
8
|
+
guard let config = parseConfiguration(configJson) else {
|
|
9
|
+
Log.warning("configure: invalid configuration JSON; ignored")
|
|
10
|
+
return
|
|
11
|
+
}
|
|
12
|
+
Angler.configure(config)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@objc public static func track(_ name: String, dataJson: String?, propertiesJson: String?, eventId: String?) {
|
|
16
|
+
let data = dataJson.flatMap { jsonObject($0) as? [String: Any] }
|
|
17
|
+
let properties = propertiesJson.flatMap { jsonObject($0) as? [String: Any] }?.compactMapValues { $0 as? String }
|
|
18
|
+
Angler.track(name, data: data, properties: properties, eventId: eventId)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@objc public static func handleDeepLink(_ url: String) { Angler.handleDeepLink(urlString: url) }
|
|
22
|
+
|
|
23
|
+
@objc public static func setUserId(_ userId: String?) { Angler.setUserId(userId) }
|
|
24
|
+
|
|
25
|
+
@objc public static func setIdentifier(_ name: String, value: String?) { Angler.setIdentifier(name, value: value) }
|
|
26
|
+
|
|
27
|
+
@objc public static func setApplicationTrackingEnabled(_ enabled: Bool) { Angler.setApplicationTrackingEnabled(enabled) }
|
|
28
|
+
|
|
29
|
+
@objc public static func setEnabled(_ enabled: Bool) { Angler.setEnabled(enabled) }
|
|
30
|
+
|
|
31
|
+
@objc public static func flush() { Angler.flush() }
|
|
32
|
+
|
|
33
|
+
@objc public static func reset() { Angler.reset() }
|
|
34
|
+
|
|
35
|
+
/// JSON from `src/index.ts` → configuration with `wrapper = "react-native"`. Nil if invalid.
|
|
36
|
+
static func parseConfiguration(_ json: String) -> AnglerConfiguration? {
|
|
37
|
+
guard let o = jsonObject(json) as? [String: Any],
|
|
38
|
+
let workspaceId = o["workspaceId"] as? String, !workspaceId.isEmpty,
|
|
39
|
+
let token = o["token"] as? String, !token.isEmpty
|
|
40
|
+
else { return nil }
|
|
41
|
+
var config = AnglerConfiguration(workspaceId: workspaceId, token: token)
|
|
42
|
+
if let v = o["domain"] as? String, !v.isEmpty { config.domain = v }
|
|
43
|
+
if let v = o["clientId"] as? String, !v.isEmpty { config.clientId = v }
|
|
44
|
+
if let v = o["appStoreId"] as? String, !v.isEmpty { config.appStoreId = v }
|
|
45
|
+
if let v = o["trackLifecycleEvents"] as? Bool { config.trackLifecycleEvents = v }
|
|
46
|
+
if let v = o["flushAt"] as? NSNumber { config.flushAt = v.intValue }
|
|
47
|
+
if let v = o["flushIntervalSeconds"] as? NSNumber { config.flushInterval = v.doubleValue }
|
|
48
|
+
if let v = o["maxQueueSize"] as? NSNumber { config.maxQueueSize = v.intValue }
|
|
49
|
+
if let v = o["debugLogging"] as? Bool { config.debugLogging = v }
|
|
50
|
+
config.wrapper = "react-native"
|
|
51
|
+
return config
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private static func jsonObject(_ s: String) -> Any? {
|
|
55
|
+
guard let data = s.data(using: .utf8) else { return nil }
|
|
56
|
+
return try? JSONSerialization.jsonObject(with: data)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#import <AnglerAnalyticsSpec/AnglerAnalyticsSpec.h>
|
|
2
|
+
#import <React/RCTBridgeModule.h>
|
|
3
|
+
|
|
4
|
+
#if __has_include(<AnglerAnalyticsRN/AnglerAnalyticsRN-Swift.h>)
|
|
5
|
+
#import <AnglerAnalyticsRN/AnglerAnalyticsRN-Swift.h>
|
|
6
|
+
#else
|
|
7
|
+
#import "AnglerAnalyticsRN-Swift.h"
|
|
8
|
+
#endif
|
|
9
|
+
|
|
10
|
+
/// TurboModule `AnglerAnalytics`: forwards every spec method to the Swift bridge.
|
|
11
|
+
@interface AnglerAnalytics : NSObject <NativeAnglerAnalyticsSpec>
|
|
12
|
+
@end
|
|
13
|
+
|
|
14
|
+
@implementation AnglerAnalytics
|
|
15
|
+
|
|
16
|
+
RCT_EXPORT_MODULE(AnglerAnalytics)
|
|
17
|
+
|
|
18
|
+
+ (BOOL)requiresMainQueueSetup
|
|
19
|
+
{
|
|
20
|
+
return NO;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Calls arrive on the main queue so `configure` can read the screen size from UIKit; the
|
|
24
|
+
// core does its own work on a background queue.
|
|
25
|
+
- (dispatch_queue_t)methodQueue
|
|
26
|
+
{
|
|
27
|
+
return dispatch_get_main_queue();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
- (void)configure:(NSString *)configJson
|
|
31
|
+
{
|
|
32
|
+
[AnglerAnalyticsBridge configure:configJson];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
- (void)track:(NSString *)name
|
|
36
|
+
dataJson:(NSString *_Nullable)dataJson
|
|
37
|
+
propertiesJson:(NSString *_Nullable)propertiesJson
|
|
38
|
+
eventId:(NSString *_Nullable)eventId
|
|
39
|
+
{
|
|
40
|
+
[AnglerAnalyticsBridge track:name dataJson:dataJson propertiesJson:propertiesJson eventId:eventId];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
- (void)handleDeepLink:(NSString *)url
|
|
44
|
+
{
|
|
45
|
+
[AnglerAnalyticsBridge handleDeepLink:url];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
- (void)setUserId:(NSString *_Nullable)userId
|
|
49
|
+
{
|
|
50
|
+
[AnglerAnalyticsBridge setUserId:userId];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
- (void)setIdentifier:(NSString *)name value:(NSString *_Nullable)value
|
|
54
|
+
{
|
|
55
|
+
[AnglerAnalyticsBridge setIdentifier:name value:value];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
- (void)setApplicationTrackingEnabled:(BOOL)enabled
|
|
59
|
+
{
|
|
60
|
+
[AnglerAnalyticsBridge setApplicationTrackingEnabled:enabled];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
- (void)setEnabled:(BOOL)enabled
|
|
64
|
+
{
|
|
65
|
+
[AnglerAnalyticsBridge setEnabled:enabled];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
- (void)flush
|
|
69
|
+
{
|
|
70
|
+
[AnglerAnalyticsBridge flush];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
- (void)reset
|
|
74
|
+
{
|
|
75
|
+
[AnglerAnalyticsBridge reset];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
79
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
80
|
+
{
|
|
81
|
+
return std::make_shared<facebook::react::NativeAnglerAnalyticsSpecJSI>(params);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// Configuration for `Angler.configure`.
|
|
4
|
+
public struct AnglerConfiguration {
|
|
5
|
+
public var workspaceId: String
|
|
6
|
+
/// The workspace JWT, sent as `Authorization: Bearer <token>`.
|
|
7
|
+
public var token: String
|
|
8
|
+
/// `data.getangler.ai` in production, `dev-data.getangler.ai` for testing.
|
|
9
|
+
public var domain: String
|
|
10
|
+
/// Overrides `client_id`. Defaults to the install ID.
|
|
11
|
+
public var clientId: String?
|
|
12
|
+
/// Your App Store ID (`id1234567890` or `1234567890`), sent as the `app_id` device property
|
|
13
|
+
/// (the value AppsFlyer reports). TikTok's app events need it.
|
|
14
|
+
public var appStoreId: String?
|
|
15
|
+
public var trackLifecycleEvents: Bool
|
|
16
|
+
/// Number of queued events that triggers a flush.
|
|
17
|
+
public var flushAt: Int
|
|
18
|
+
/// Seconds between periodic flushes.
|
|
19
|
+
public var flushInterval: TimeInterval
|
|
20
|
+
/// When full, the oldest event is dropped.
|
|
21
|
+
public var maxQueueSize: Int
|
|
22
|
+
public var debugLogging: Bool
|
|
23
|
+
/// `native`, or set by the React Native and Flutter wrappers.
|
|
24
|
+
public var wrapper: String = "native"
|
|
25
|
+
|
|
26
|
+
public init(workspaceId: String, token: String, domain: String = "data.getangler.ai", clientId: String? = nil,
|
|
27
|
+
trackLifecycleEvents: Bool = true, flushAt: Int = 20, flushInterval: TimeInterval = 30,
|
|
28
|
+
maxQueueSize: Int = 1000, debugLogging: Bool = false, appStoreId: String? = nil) {
|
|
29
|
+
self.workspaceId = workspaceId
|
|
30
|
+
self.token = token
|
|
31
|
+
self.domain = domain
|
|
32
|
+
self.clientId = clientId
|
|
33
|
+
self.trackLifecycleEvents = trackLifecycleEvents
|
|
34
|
+
self.flushAt = flushAt
|
|
35
|
+
self.flushInterval = flushInterval
|
|
36
|
+
self.maxQueueSize = maxQueueSize
|
|
37
|
+
self.debugLogging = debugLogging
|
|
38
|
+
self.appStoreId = appStoreId
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// The Angler app SDK. Every method is safe to call from any thread and never throws.
|
|
43
|
+
public enum Angler {
|
|
44
|
+
static let core: AnglerCore = {
|
|
45
|
+
let queue = DispatchQueue(label: AnglerCore.queueLabel)
|
|
46
|
+
let support = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
|
|
47
|
+
?? FileManager.default.temporaryDirectory
|
|
48
|
+
#if canImport(UIKit) && os(iOS)
|
|
49
|
+
let backgroundTasks: BackgroundTasking? = UIKitBackgroundTasks()
|
|
50
|
+
#else
|
|
51
|
+
let backgroundTasks: BackgroundTasking? = nil
|
|
52
|
+
#endif
|
|
53
|
+
return AnglerCore(
|
|
54
|
+
store: UserDefaultsStore(),
|
|
55
|
+
clock: SystemClock(),
|
|
56
|
+
queueDirectory: support.appendingPathComponent("AnglerAnalytics", isDirectory: true),
|
|
57
|
+
transportFactory: { config in
|
|
58
|
+
// AnglerCore.configure only gets this far once the endpoint has been validated.
|
|
59
|
+
URLSessionTransport(endpoint: URLSessionTransport.endpoint(domain: config.domain, workspaceId: config.workspaceId)!,
|
|
60
|
+
token: config.token)
|
|
61
|
+
},
|
|
62
|
+
adIds: SystemAdIdProvider(),
|
|
63
|
+
device: { config, adTracking, appTracking in
|
|
64
|
+
DeviceContext.collect(wrapper: config.wrapper, appStoreId: config.appStoreId, adTrackingEnabled: adTracking,
|
|
65
|
+
applicationTrackingEnabled: appTracking)
|
|
66
|
+
},
|
|
67
|
+
uuid: { UUID().uuidString.lowercased() },
|
|
68
|
+
scheduler: DispatchScheduler(queue: queue),
|
|
69
|
+
lifecycle: .system,
|
|
70
|
+
backgroundTasks: backgroundTasks,
|
|
71
|
+
protectedData: .system,
|
|
72
|
+
firebase: SystemFirebaseIdProvider(),
|
|
73
|
+
queue: queue
|
|
74
|
+
)
|
|
75
|
+
}()
|
|
76
|
+
|
|
77
|
+
/// Starts the SDK. Call once at launch; repeat calls with the same workspace do nothing.
|
|
78
|
+
public static func configure(_ configuration: AnglerConfiguration) {
|
|
79
|
+
// UIKit is main-thread only: read the screen size there (now, or soon if called elsewhere).
|
|
80
|
+
if Thread.isMainThread {
|
|
81
|
+
DeviceContext.captureScreenIfOnMain()
|
|
82
|
+
} else {
|
|
83
|
+
DispatchQueue.main.async { DeviceContext.captureScreenIfOnMain() }
|
|
84
|
+
}
|
|
85
|
+
core.configure(configuration)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// Queues an event. Names data-api knows are sent as-is; others become `custom_event`.
|
|
89
|
+
/// Dropped (with a warning) before `configure`.
|
|
90
|
+
public static func track(_ name: String, data: [String: Any]? = nil, properties: [String: String]? = nil,
|
|
91
|
+
eventId: String? = nil) {
|
|
92
|
+
core.track(name, data: data, properties: properties, eventId: eventId)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// Queues a typed event: a standard name (or `.custom("name")`) with typed `data`.
|
|
96
|
+
///
|
|
97
|
+
/// Angler.track(.productAddedToCart, data: AnglerData(cartLine: AnglerCartLine(
|
|
98
|
+
/// merchandise: AnglerProductVariant(id: "variant-1", price: AnglerMoney(amount: 29, currencyCode: "USD")),
|
|
99
|
+
/// quantity: 1)))
|
|
100
|
+
public static func track(_ event: AnglerEvent, data: AnglerData? = nil, properties: [String: String]? = nil,
|
|
101
|
+
eventId: String? = nil) {
|
|
102
|
+
core.track(event, data: data, properties: properties, eventId: eventId)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// Captures click IDs and the session URL. Works before `configure`.
|
|
106
|
+
public static func handleDeepLink(_ url: URL) {
|
|
107
|
+
core.handleDeepLink(urlString: url.absoluteString)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// Like `handleDeepLink(_:)`, for URL strings (including ones `URL` would reject).
|
|
111
|
+
public static func handleDeepLink(urlString: String) {
|
|
112
|
+
core.handleDeepLink(urlString: urlString)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// Sets the `customer_id` identifier; nil clears it.
|
|
116
|
+
public static func setUserId(_ userId: String?) { core.setUserId(userId) }
|
|
117
|
+
|
|
118
|
+
/// Sets a custom identifier such as `appsflyer_id`; nil removes it. Reserved names are ignored.
|
|
119
|
+
public static func setIdentifier(_ name: String, value: String?) { core.setIdentifier(name, value: value) }
|
|
120
|
+
|
|
121
|
+
/// Sent as `application_tracking_enabled` (default true).
|
|
122
|
+
public static func setApplicationTrackingEnabled(_ enabled: Bool) { core.setApplicationTrackingEnabled(enabled) }
|
|
123
|
+
|
|
124
|
+
/// `false` stops all collection and deletes queued events. Stored across launches.
|
|
125
|
+
public static func setEnabled(_ enabled: Bool) { core.setEnabled(enabled) }
|
|
126
|
+
|
|
127
|
+
/// Sends queued events now.
|
|
128
|
+
public static func flush() { core.flush() }
|
|
129
|
+
|
|
130
|
+
/// Clears the user ID, custom identifiers and attribution, and creates a new install ID.
|
|
131
|
+
public static func reset() { core.reset() }
|
|
132
|
+
}
|