react-native-swrve v0.6.1
react-native-swrve
Getting started
$ npm install react-native-swrve --save
Mostly automatic installation
$ react-native link react-native-swrve
After, you must Open up android/app/src/main/java/[...]/MainActivity.java
file and follow the steps under the additional configuration section.
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-swrve
and addRNSwrve.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNSwrve.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
Android
- Open up
android/app/src/main/java/[...]/MainApplication.java
- Add
import io.underscope.RNSwrvePackage;
to the imports at the top of the file. - Add
new RNSwrvePackage()
to the list returned by thegetPackages()
method. - Follow the steps under the additional configuration section.
- Append the following lines to
android/settings.gradle
:include ':react-native-swrve' project(':react-native-swrve').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-swrve/android')
Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-swrve')
Additional configuration
Android
1. Include required libraries
Add the following in your android/app/build.gradle
file, so the react-native-swrve
dependencies are properly included.
repositories {
...
flatDir {
dirs project(':react-native-swrve').file('libs')
}
jcenter {
url 'http://dl.bintray.com/swrve-inc/android'
}
...
}
2. Add required configuration
Add to your AndroidManifest.xml
the following line into the <application>
tag.
<application ...>
...
<meta-data android:name="com.swrve.sdk.appId" android:value="@integer/swrve_app_id"/>
<meta-data android:name="com.swrve.sdk.apiKey" android:value="@string/swrve_api_key"/>
...
</application>
And in your strings.xml
file add your swrve configuration:
<integer name="swrve_app_id">YOUR_APP_ID</integer>
<string name="swrve_app_key">YOUR_API_KEY</string>
3. Initialize library
Swrve must be initialized from the onCreate
method in the MainApplication.java
class.
...
// <-- Add this line
import com.dcpi.swrvemanager.SwrveManager;
// -->
...
@Override
public void onCreate() {
super.onCreate();
// <--- Add this block
try {
SwrveManager swrveManager = SwrveManager.createInstance(this);
swrveManager.initWithAnalyticsKeySecret(getResources().getInteger(R.integer.swrve_app_id), getResources().getString(R.string.swrve_api_key));
} catch (Exception e) {
Log.e("MyApp", "failed to initialized SwrveManager", e);
}
// -->
SoLoader.init(this, /* native exopackage */ false);
}
iOS
1. Include required libraries (check if it is necessary or react-native link
actually does this step)
- Select your target
- Go to Build Settings
- Look for Header Search Paths and add
$(SRCROOT)/../node_modules/react-native-swrve/ios
asrecursive
2. Add required configuration
- If you have multiple environments and a
.xcconfig
file for each one then you should add the following variables per environmentSWRVE_APP_ID=12345 SWRVE_API_KEY=abc123xyz1Axxxx
- Then in the
Info.plist
file add those values as keys so can be used on Swrve module init<key>SwrveApiKey</key> <string>$(SWRVE_API_KEY)</string> <key>SwrveAppId</key> <string>$(SWRVE_APP_ID)</string>
3. Add pub.pem
file to Resources
- Go to Resources (if doesn't exist then create a Group with that name)
- Do a right click and select Add new file
- Navigate to
node_modules/react-native-swrve/ios/Resources
folder and selectpub.pem
file
You can verify the file was included correctly selecting your target and going to Build Phases > Copy Bundle Resources. The
pub.pem
file should be there.
4. Initialize library
Swrve must be initialized from the didFinishLaunchingWithOptions
method in the AppDelegate.m
file.
IMPORTANT: the appId is a number, not a string.
// <-- Add this line
#import "DcpiSwrveManager.h"
// -->
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// <-- Add this lines
// Chances are you've the SwrveAppId and SwrveApiKey in the Info.plist file.
// In that case you should get those values like following
NSNumber* swrveAppId = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"SwrveAppId"];
NSString* swrveApiKey = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"SwrveApiKey"];
SwrveConfig* config = [[SwrveConfig alloc] init];
config.pushEnabled = YES;
[[DcpiSwrveManager alloc] init:swrveAppId key:swrveApiKey config:config launchOptions:launchOptions];
// -->
return YES;
}
@end
Usage
import RNSwrve from 'react-native-swrve'
Tracking Session Events
In order to keep track the user's session you must add to your main component the following:
class App extends Component {
...
componentWillMount() {
AppState.addEventListener('change', this.handleAppStateChange)
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange)
}
handleAppStateChange = nextAppState => {
if (nextAppState === 'active') {
Swrve.sessionStart()
} else {
Swrve.sessionEnd()
}
}
...
}
Methods
sessionStart()
Track the users' session started.
sessionEnd()
Track the users' session ended.
userUpdate(Object attributes)
Update user attributes.
attributes
may include:level
(Number)consumable.<consumable_name>_balance
(Number)monetization.<currency>_balance
(Number)
logIAPAction(String productId, Number productPrice, Number quantity, String currency, String store, String durability, Integer level, Object options)
Tracks purchases done with real money.
options
may include:context
(String)type
(String)subtype
(String)
DEPRECATED iap(Integer quantity, String productId, Number productPrice, String currency)
Tracks purchases done with real money without store verification.
DEPRECATED iapPlay(String productId, Double productPrice, String currency, String receipt, String receiptSignature)
Tracks purchases done with real money with store verification.
logFailedReceiptAction(String productId, String error)
Tracks a failure in the store verification.
logPurchaseAction(String item, String currency, Number cost, Number quantity)
Tracks purchases done with any type of currency.
logCurrencyGivenAction(String givenCurrency, Number givenAmount, Object options)
Tracks when a user is given currency.
options
may include:level
(Number)context
(String)type
(String)subtype
(String)subtype2
(String)custom
(Object)
logTimingAction(Number elapsedTime, String context, Object options)
Tracks timing events.
options
may include:stepNumber
(Number)stepName
(String)custom
(Object)
navigation(String buttonPressed, Object options)
Tracks navigation between screens.
options
may include:fromLocation
(String)toLocation
(String)module
(String)order
(Number)custom
(Object)
logAction(String tier1, Object options)
Tracks any type of event or user action.
options
may include:subevent
(String)level
(Number)tier2
(String)tier3
(String)tier4
(String)context
(String)message
(String)custom
(Object)
logFunnelAction(String type, Number stepNumber, String stepName, Object options)
Tracks funnel events.
options
may include:message
(string)custom
(object)
logErrorAction(String reason, Object options)
Tracks error events.
options
may include:type
(string)context
(string)custom
(object)
Troubleshooting
Android
1. com.android.dex.DexException
If you run into the following error (or similar) when running the app:
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/internal/zzbs;
This means that two dependencies in your project are using the com.google.android.gms
library (one of them being react-native-swrve
).
To solve it, exclude this dependency from on your android/app/build.gradle
for react-native-swrve
under the dependencies section:
dependencies {
...
compile(project(':react-native-swrve')) {
exclude group: 'com.google.android.gms'
}
...
}