Skip to content

Commit 308cfdc

Browse files
committed
Add support for Expo projects
1 parent d72596c commit 308cfdc

File tree

21 files changed

+2719
-1324
lines changed

21 files changed

+2719
-1324
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bundle install
1414
bundle exec pod install
1515

1616
# Build Android and iOS example app
17-
yarn ../..
17+
cd - # Go back to the root directory of the repo
1818
yarn build:android
1919
yarn build:ios
2020
```
@@ -40,9 +40,9 @@ The [example app](/example/) demonstrates usage of the library. You need to run
4040

4141
It is configured to use the local version of the library, so any changes you make to the library's source code will be reflected in the example app. Changes to the library's JavaScript code will be reflected in the example app without a rebuild, but native code changes will require a rebuild of the example app.
4242

43-
If you want to use Android Studio or XCode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/TrackingplanReactNativeExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > trackingplan-react-native`.
43+
If you want to use Android Studio or XCode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/TrackingplanReactNativeExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > TrackingplanReactNative`.
4444

45-
To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `trackingplan-react-native` under `Android`.
45+
To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `trackingplan_react-native` under `Android`.
4646

4747
You can use various commands from the root directory to work with the project.
4848

README.md

Lines changed: 146 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
## Table of Contents
44

55
- [Requirements](#requirements)
6-
- [Installation](#Installation)
7-
- [Android](#android)
8-
- [iOS](#ios)
9-
- [Autolinking & Rebuilding](#autolinking--rebuilding)
6+
- [Installation](#installation)
7+
- [Choose Your Project Type](#choose-your-project-type)
8+
- [Expo Projects](#expo-projects)
9+
- [React Native CLI Projects](#react-native-cli-projects)
1010
- [Usage](#usage)
1111
- [Need Help?](#need-help)
1212
- [Learn More](#learn-more)
@@ -19,7 +19,24 @@
1919

2020
## Installation
2121

22-
Install the Trackingplan for React Native SDK with NPM or Yarn:
22+
### Choose Your Project Type
23+
24+
Before proceeding, identify your project type:
25+
26+
- **Expo Projects**: Use Expo CLI, have an `app.json` or `app.config.js` file, and may use EAS Build
27+
- **React Native CLI Projects**: Use React Native CLI, have separate `android/` and `ios/` directories
28+
29+
---
30+
31+
## Expo Projects
32+
33+
**Requirements for Expo:**
34+
35+
- Expo SDK 50+
36+
- Custom development builds (Expo plugins require custom native code)
37+
- ⚠️ **Note**: This will NOT work with Expo Go due to native dependencies
38+
39+
### Step 1: Install the Package
2340

2441
```sh
2542
# Using npm
@@ -29,91 +46,163 @@ npm install --save @trackingplan/react-native
2946
yarn add @trackingplan/react-native
3047
```
3148

32-
Follow the instructions in the next sections to configure Trackingplan in Android and iOS.
49+
### Step 2: Configure the Expo Plugin
50+
51+
Add the plugin to your `app.config.js` or `app.json`. Replace `YOUR_TP_ID` with your actual Trackingplan ID (found in your Trackingplan dashboard).
52+
53+
**Option A: app.config.js**
54+
55+
```javascript
56+
export default {
57+
expo: {
58+
name: 'Your App',
59+
// ... other config
60+
plugins: [
61+
[
62+
'@trackingplan/react-native',
63+
{
64+
tpId: 'YOUR_TP_ID', // Replace with your actual Trackingplan ID
65+
},
66+
],
67+
],
68+
},
69+
};
70+
```
3371

34-
### Android
72+
**Option B: app.json**
73+
74+
```json
75+
{
76+
"expo": {
77+
"name": "Your App",
78+
"plugins": [
79+
[
80+
"@trackingplan/react-native",
81+
{
82+
"tpId": "YOUR_TP_ID"
83+
}
84+
]
85+
]
86+
}
87+
}
88+
```
3589

36-
1. Open your `/android/build.gradle` to include the Trackingplan adapter.
90+
### Step 3: Rebuild Your Project
3791

38-
Add the adapter dependency:
92+
```sh
93+
# For development builds
94+
npx expo run:android
95+
npx expo run:ios
3996

40-
```gradle
41-
// Add this to your project's root build.gradle
42-
buildscript {
43-
ext {
44-
// ... other ext properties
45-
trackingplanVersion = findProject(':trackingplan-react-native').ext.get('trackingplanVersion')
46-
}
47-
dependencies {
48-
// ... other dependencies
49-
classpath "com.trackingplan.client:adapter:$trackingplanVersion"
50-
}
51-
}
97+
# For EAS builds
98+
eas build --platform android
99+
eas build --platform ios
52100
```
53101

54-
> **Important:** The above approach is recommended to add the Trackingplan adapter. If you choose to hardcode a version string instead of using the programmatic approach, you MUST ensure that it exactly matches the version used by trackingplan-react-native, otherwise your app may experience compatibility issues.
102+
### ✅ Expo Setup Complete!
55103

56-
2. Modify your `/android/app/build.gradle` to apply the Trackingplan Gradle plugin.
104+
The Expo plugin automatically handles:
57105

58-
```gradle
59-
// ... other plugins
60-
apply plugin: "com.trackingplan.client"
61-
```
106+
- Android Gradle dependencies and configuration
107+
- iOS CocoaPods dependency
108+
- Native initialization code with your Trackingplan ID
62109

63-
3. Open your `/android/app/src/main/java/com/{projectName}/MainApplication.kt` and add the following:
110+
**No additional configuration needed!**
64111

65-
At the top of the file, import the Trackingplan SDK:
112+
---
66113

67-
```kotlin
68-
import com.trackingplan.client.sdk.Trackingplan;
69-
```
114+
## React Native CLI Projects
70115

71-
Within your existing `onCreate` method, add the following right after `super.onCreate()`:
116+
For React Native CLI projects, manual native configuration is required.
72117

73-
```kotlin
74-
override fun onCreate() {
75-
super.onCreate()
76-
Trackingplan.init("YOUR_TP_ID").start(this)
77-
// ...other code
78-
}
118+
### Step 1: Install the Package
119+
120+
```sh
121+
# Using npm
122+
npm install --save @trackingplan/react-native
123+
124+
# Using yarn
125+
yarn add @trackingplan/react-native
79126
```
80127

81-
### iOS
128+
### Step 2: Android Configuration
82129

83-
Open your `/ios/{projectName}/AppDelegate.swift` file and add the following:
130+
1. **Update root build.gradle**
84131

85-
At the top of the file, import the Trackingplan SDK:
132+
Open `/android/build.gradle` and add the Trackingplan adapter:
86133

87-
```swift
88-
import Trackingplan
89-
```
134+
```gradle
135+
buildscript {
136+
ext {
137+
// ... other ext properties
138+
trackingplanVersion = findProject(':trackingplan_react-native').ext.get('trackingplanVersion')
139+
}
140+
dependencies {
141+
// ... other dependencies
142+
classpath "com.trackingplan.client:adapter:$trackingplanVersion"
143+
}
144+
}
145+
```
146+
147+
> **Important:** Use the programmatic approach above to ensure version compatibility. Hardcoding a version string may cause compatibility issues.
148+
149+
2. **Apply the Gradle plugin**
150+
151+
Modify `/android/app/build.gradle`:
152+
153+
```gradle
154+
// ... other plugins
155+
apply plugin: "com.trackingplan.client"
156+
```
90157

91-
Within your existing `application` method, add the following to the top of the method:
158+
3. **Initialize in MainApplication**
159+
160+
Open `/android/app/src/main/java/com/{projectName}/MainApplication.kt`:
161+
162+
```kotlin
163+
import com.trackingplan.client.sdk.Trackingplan // Add this import
164+
165+
class MainApplication : Application(), ReactApplication {
166+
override fun onCreate() {
167+
super.onCreate()
168+
Trackingplan.init("YOUR_TP_ID").start(this) // Add this line
169+
// ...other code
170+
}
171+
}
172+
```
173+
174+
### Step 3: iOS Configuration
175+
176+
Open `/ios/{projectName}/AppDelegate.swift`:
92177

93178
```swift
94-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
95-
Trackingplan.initialize(tpId: "YOUR_TP_ID")
96-
// ...other code
179+
import Trackingplan // Add this import
180+
181+
@main
182+
class AppDelegate: UIResponder, UIApplicationDelegate {
183+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
184+
Trackingplan.initialize(tpId: "YOUR_TP_ID") // Add this line
185+
// ...other code
186+
return true
187+
}
97188
}
98189
```
99190

100-
## Autolinking & Rebuilding
101-
102-
Once the above steps have been completed, the Trackingplan for React Native SDK must be linked to your project and your application needs to be rebuilt.
103-
104-
To automatically link the package, rebuild your project:
191+
### Step 4: Link and Rebuild
105192

106-
```console
107-
# Android apps
193+
```sh
194+
# For Android
108195
npx react-native run-android
109196

110-
# iOS apps
197+
# For iOS
111198
cd ios/
112199
pod install --repo-update
113200
cd ..
114201
npx react-native run-ios
115202
```
116203

204+
### ✅ React Native CLI Setup Complete!
205+
117206
## Usage
118207

119208
At this point, your app is ready to start monitoring traffic sent to your data destinations with Trackingplan.

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def trackingplan_version = ext.trackingplanVersion
7676
dependencies {
7777
implementation "com.facebook.react:react-android"
7878
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
79-
implementation "com.trackingplan.client:sdk:$trackingplan_version"
79+
api "com.trackingplan.client:sdk:$trackingplan_version"
8080
}
8181

8282
react {

app.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./plugin/build');

eslint.config.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ export default defineConfig([
3333
},
3434
},
3535
{
36-
ignores: [
37-
'node_modules/',
38-
'lib/'
39-
],
36+
ignores: ['node_modules/', 'lib/'],
4037
},
4138
]);

example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The first time you create a new project, run the Ruby bundler to install CocoaPo
4242
bundle install
4343
```
4444

45-
Then, and every time you update your native dependencies, run:
45+
Then, and every time you update your native dependencies, run (from your ios directory):
4646

4747
```sh
4848
bundle exec pod install

example/android/app/src/main/java/trackingplanreactnative/example/MainApplication.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,32 @@ import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
1111
import com.facebook.react.defaults.DefaultReactNativeHost
1212
import com.facebook.react.soloader.OpenSourceMergedSoMapping
1313
import com.facebook.soloader.SoLoader
14+
import com.trackingplan.client.sdk.Trackingplan
1415

1516
class MainApplication : Application(), ReactApplication {
1617

1718
override val reactNativeHost: ReactNativeHost =
18-
object : DefaultReactNativeHost(this) {
19-
override fun getPackages(): List<ReactPackage> =
20-
PackageList(this).packages.apply {
21-
// Packages that cannot be autolinked yet can be added manually here, for example:
22-
// add(MyReactNativePackage())
23-
}
19+
object : DefaultReactNativeHost(this) {
20+
override fun getPackages(): List<ReactPackage> =
21+
PackageList(this).packages.apply {
22+
// Packages that cannot be autolinked yet can be added manually here, for example:
23+
// add(MyReactNativePackage())
24+
}
2425

25-
override fun getJSMainModuleName(): String = "index"
26+
override fun getJSMainModuleName(): String = "index"
2627

27-
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
28+
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
2829

29-
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
30-
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
31-
}
30+
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
31+
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
32+
}
3233

3334
override val reactHost: ReactHost
3435
get() = getDefaultReactHost(applicationContext, reactNativeHost)
3536

3637
override fun onCreate() {
3738
super.onCreate()
39+
Trackingplan.init("TP3708250").enableDebug().start(this) // Add this line
3840
SoLoader.init(this, OpenSourceMergedSoMapping)
3941
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
4042
// If you opted-in for the New Architecture, we load the native entry point for this app.

example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
targetSdkVersion = 35
77
ndkVersion = "27.1.12297006"
88
kotlinVersion = "2.0.21"
9-
trackingplanVersion = findProject(":trackingplan-react-native").ext.get("trackingplanVersion")
9+
trackingplanVersion = findProject(":trackingplan_react-native").ext.get("trackingplanVersion")
1010
}
1111
repositories {
1212
google()

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ PODS:
16561656
- React-utils (= 0.79.2)
16571657
- SocketRocket (0.7.1)
16581658
- Trackingplan (1.4.1)
1659-
- TrackingplanReactNative (0.1.0):
1659+
- TrackingplanReactNative (0.1.1):
16601660
- DoubleConversion
16611661
- glog
16621662
- hermes-engine
@@ -1980,7 +1980,7 @@ SPEC CHECKSUMS:
19801980
ReactCommon: 76d2dc87136d0a667678668b86f0fca0c16fdeb0
19811981
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
19821982
Trackingplan: b43e2d62090dc36d3fac3efcdef27cdbfd7d5712
1983-
TrackingplanReactNative: 9f6d3a75cac2c2858a24beacc3188a7bda96ee3e
1983+
TrackingplanReactNative: 79eb434c8d3ecc3e5333108a7812e53e2a233540
19841984
Yoga: c758bfb934100bb4bf9cbaccb52557cee35e8bdf
19851985

19861986
PODFILE CHECKSUM: 75042b8ee069f7c96c6dd0fa588b0b8f798528f9

example/ios/TrackingplanReactNativeExample/AppDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import UIKit
22
import React
33
import React_RCTAppDelegate
44
import ReactAppDependencyProvider
5+
import Trackingplan
56

67
@main
78
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -14,6 +15,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1415
_ application: UIApplication,
1516
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
1617
) -> Bool {
18+
Trackingplan.initialize(tpId: "TP3708250", debug: true)
1719
let delegate = ReactNativeDelegate()
1820
let factory = RCTReactNativeFactory(delegate: delegate)
1921
delegate.dependencyProvider = RCTAppDependencyProvider()

0 commit comments

Comments
 (0)