|
| 1 | +# Klaviyo React Native SDK Example App |
| 2 | + |
| 3 | +The example app is a reference integration of the Klaviyo React Native SDK. It |
| 4 | +demonstrates where and how to `initialize`, implement notification delegate |
| 5 | +methods on iOS, handle opened notification intents on Android, and exercise the |
| 6 | +SDK's public API surface (profile, events, in-app forms, geofencing, push). |
| 7 | +Sample code is annotated — search for `JS Installation Step`, `iOS Installation Step`, |
| 8 | +and `Android Installation Step` in the source to find the integration callouts. |
| 9 | + |
| 10 | +## Getting Started |
| 11 | + |
| 12 | +### 1. Configure Your API Key |
| 13 | + |
| 14 | +The app loads your Klaviyo public API key from a gitignored `.env` file via |
| 15 | +[`react-native-dotenv`](https://github.com/goatandsheep/react-native-dotenv). |
| 16 | + |
| 17 | +1. Copy the template: `cp .env.template .env` |
| 18 | +2. Edit `.env` and set `KLAVIYO_API_KEY=` to your actual public API key |
| 19 | +3. Restart Metro with `yarn start --reset-cache` so the babel plugin picks up the new value |
| 20 | + |
| 21 | +> **If you hit "Klaviyo API key not configured" after setting up `.env`:** Metro inlines |
| 22 | +> `.env` values at bundle time via `react-native-dotenv`. If Metro was already running when |
| 23 | +> you created or edited `.env`, it will serve a cached bundle with `KLAVIYO_API_KEY` still |
| 24 | +> undefined. Stop Metro and restart it with `yarn start --reset-cache` (from `example/`). |
| 25 | +
|
| 26 | +Notes: |
| 27 | + |
| 28 | +- `.env` is gitignored — only `.env.template` is checked in. |
| 29 | +- If `KLAVIYO_API_KEY` is missing or still set to the placeholder, the app fails |
| 30 | + fast at module load with a clear error — no silent misbehavior. Copy |
| 31 | + `.env.template` to `.env` and set a real key before running. |
| 32 | + |
| 33 | +### 2. Install Dependencies |
| 34 | + |
| 35 | +The one-liner, run from the repo root: |
| 36 | + |
| 37 | +```bash |
| 38 | +# From the repo root |
| 39 | +yarn example setup |
| 40 | + |
| 41 | +# This alias runs: |
| 42 | +# yarn install --immutable (repo root) |
| 43 | +# bundle install (example/) |
| 44 | +# bundle exec pod install (example/ios/) |
| 45 | +``` |
| 46 | + |
| 47 | +You can run those steps manually if you prefer, but `yarn example setup` is the |
| 48 | +preferred entry point. |
| 49 | + |
| 50 | +### 3. Run the App |
| 51 | + |
| 52 | +Preferred — from the repo root (yarn workspaces entry point): |
| 53 | + |
| 54 | +```bash |
| 55 | +# From the repo root |
| 56 | +yarn example start |
| 57 | +yarn example android |
| 58 | +yarn example ios |
| 59 | +``` |
| 60 | + |
| 61 | +Alternatively, from the `example/` directory: |
| 62 | + |
| 63 | +```bash |
| 64 | +# Start Metro bundler |
| 65 | +yarn start |
| 66 | + |
| 67 | +# Run iOS |
| 68 | +yarn ios |
| 69 | + |
| 70 | +# Run Android |
| 71 | +yarn android |
| 72 | +``` |
| 73 | + |
| 74 | +## Push Notifications |
| 75 | + |
| 76 | +Push notifications are handled via `@react-native-firebase/messaging` in the |
| 77 | +JavaScript layer — the Klaviyo SDK is initialized from JS, and Firebase manages |
| 78 | +push tokens cross-platform. |
| 79 | + |
| 80 | +### Platform-specific Firebase setup |
| 81 | + |
| 82 | +**Android** builds and runs out of the box without Firebase configured. The |
| 83 | +`com.google.gms.google-services` plugin is conditionally applied only when |
| 84 | +`google-services.json` is present. Without the file, push features are dormant |
| 85 | +but everything else (profile, events, in-app forms, geofencing) works normally. |
| 86 | + |
| 87 | +- To enable push: drop a real `google-services.json` from your Firebase project |
| 88 | + into `example/android/app/`. The file is gitignored. Ensure the |
| 89 | + `applicationId` in `example/android/app/build.gradle` |
| 90 | + (`com.klaviyoreactnativesdkexample`) is registered as an Android app in your |
| 91 | + Firebase project. |
| 92 | + |
| 93 | +**iOS** requires a `GoogleService-Info.plist` at `example/ios/` to build at all |
| 94 | +— the Xcode project's Resources build phase references it unconditionally. Two |
| 95 | +options: |
| 96 | + |
| 97 | +- **To enable push:** drop a real `GoogleService-Info.plist` from your Firebase |
| 98 | + project at `example/ios/GoogleService-Info.plist` (gitignored). Ensure your |
| 99 | + iOS bundle identifier is registered in the same Firebase project. Then run |
| 100 | + `bundle exec pod install` from `example/ios/`. |
| 101 | +- **To build without push:** write a stub plist at the same path. The app's |
| 102 | + AppDelegate guards `[FIRApp configure]` on the real plist's presence of valid |
| 103 | + values — a stub passes the Xcode build-input check, Firebase logs an |
| 104 | + invalid-config warning at runtime, and the rest of the app works. Stub |
| 105 | + content you can copy: |
| 106 | + <details> |
| 107 | + <summary>Stub <code>GoogleService-Info.plist</code></summary> |
| 108 | + |
| 109 | + ```xml |
| 110 | + <?xml version="1.0" encoding="UTF-8"?> |
| 111 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 112 | + <plist version="1.0"> |
| 113 | + <dict> |
| 114 | + <key>BUNDLE_ID</key><string>com.klaviyoreactnativesdkexample</string> |
| 115 | + <key>GOOGLE_APP_ID</key><string>1:000000000000:ios:0000000000000000</string> |
| 116 | + <key>API_KEY</key><string>stub-for-ci</string> |
| 117 | + <key>GCM_SENDER_ID</key><string>000000000000</string> |
| 118 | + <key>PROJECT_ID</key><string>stub-project</string> |
| 119 | + <key>STORAGE_BUCKET</key><string>stub.appspot.com</string> |
| 120 | + <key>IS_GCM_ENABLED</key><true/> |
| 121 | + <key>IS_ADS_ENABLED</key><false/> |
| 122 | + <key>IS_ANALYTICS_ENABLED</key><false/> |
| 123 | + <key>IS_APPINVITE_ENABLED</key><false/> |
| 124 | + <key>IS_SIGNIN_ENABLED</key><false/> |
| 125 | + </dict> |
| 126 | + </plist> |
| 127 | + ``` |
| 128 | + </details> |
| 129 | + |
| 130 | +### Native push (alternative) |
| 131 | + |
| 132 | +For apps that prefer to handle push tokens natively (e.g., brownfield apps), the |
| 133 | +Klaviyo iOS and Android SDKs both have their own native push APIs. See the |
| 134 | +`AppDelegate.mm` comments on iOS and `MainApplication.kt` comments on Android for |
| 135 | +pointers, plus the platform SDKs' READMEs: |
| 136 | + |
| 137 | +- iOS: https://github.com/klaviyo/klaviyo-swift-sdk#push-notifications |
| 138 | +- Android: https://github.com/klaviyo/klaviyo-android-sdk#push-notifications |
| 139 | + |
| 140 | +Note: `Klaviyo.handlePush(intent)` in `MainActivity.kt` is always required on Android — push open tracking depends on native intent handling regardless of how tokens are managed. |
| 141 | + |
| 142 | +## Integration Step Comments |
| 143 | + |
| 144 | +The example source is annotated with `JS Installation Step`, `iOS Installation Step`, |
| 145 | +and `Android Installation Step` comments that mark the code an integrator needs to |
| 146 | +replicate in their own app. Grep for any of those prefixes across `example/` to walk |
| 147 | +the integration surface: |
| 148 | + |
| 149 | +- `JS Installation Step` — `example/src/App.tsx` and `example/index.js` (SDK init, |
| 150 | + API key sourcing, optional `@react-native-firebase/messaging` background handler) |
| 151 | +- `iOS Installation Step` — `example/ios/KlaviyoReactNativeSdkExample/AppDelegate.{h,mm}`, |
| 152 | + `PushNotificationsHelper.swift`, `NotificationService.swift`, and the Podfile |
| 153 | +- `Android Installation Step` — `example/android/build.gradle`, `app/build.gradle`, |
| 154 | + `AndroidManifest.xml`, and `MainActivity.kt` |
| 155 | + |
| 156 | +Each comment is self-describing — the prefix is just a grep handle, not part of a |
| 157 | +strictly ordered checklist. Some steps are labelled by concern rather than being |
| 158 | +numbered (e.g., "Firebase, part 1 of 2") where that reads more clearly than an |
| 159 | +ordinal would. |
| 160 | + |
| 161 | +## Features Demonstrated |
| 162 | + |
| 163 | +- **Profile Management** — Set email, phone, external ID individually or together; reset profile |
| 164 | +- **Event Tracking** — Create test events and Viewed Product events |
| 165 | +- **In-App Forms** — Register/unregister with configurable session timeout |
| 166 | +- **Geofencing** — Location permission flow, register/unregister, list current geofences |
| 167 | +- **Push Notifications** — Firebase-based permission request, token display, badge count (iOS) |
| 168 | +- **Deep Linking** — Universal tracking link handling via `Linking` API |
0 commit comments