Skip to main content
All CollectionsSwaarm MMPMMP: Developer DocumentationSDK
React Native SDK: Step by Step Implementation guide
React Native SDK: Step by Step Implementation guide
Updated over a year ago

The Swaarm SDK for React Native allows for better measurement and tracking of user journeys and activities in apps that use the React Native framework. You can find the Swaarm React Native SDK on Github.

⚠️Important:

The Swaarm SDK supports iOS 9 or later and Android API level 9 (Gingerbread) or later.

Step 1: Setting up your Environment

To install the SDK you need to add the following dependencies to your package.json file:

    "@swaarm/swaarm-sdk": "^1.0.6",
"@react-native-async-storage/async-storage": "^1.19.6",
"react-native-device-info": "^10.11.0",

✏️ Note:

If you are already using the react-native-async-storage or react-native-device-info libraries in your project, feel free to skip them and use your existing versions.

To install the dependencies run the following command on your terminal:

$ npm install

Step 2: Integrate the SDK

iOS

To integrate the Swaarm React Native SDK for the iOS platform apps, also run the following command on your terminal:

$ npx pod-install

Android

Use the following steps to integrate the Swaarm React Native SDK for the Android platform apps.

  • Add permissions:

    The Swaarm SDK requires the following permissions. Add them to your AndroidManifest.xml file if they are not already present:

    <uses-permission android:name="android.permission.INTERNET" />
  • Setup Install Referrer:

    • To include Install Referrer, add the following code to the build.gradle file of your app

      compile 'com.android.installreferrer:installreferrer:2.2' 

      ...

      allprojects {
      repositories {
      maven {
      url "https://maven.google.com"
      }
      }
      }

  • Setup Proguard:

    If you are using Proguard, add the following to your Proguard file:

- keep public class com.android.installreferrer.** { *; }

Step 3: Initialize the Swaarm SDK

Ensure the Swaarm SDK is initialized promptly within your React Native application. To accomplish this, initialize your configuration object by including your app token and your tracking domain. Add the following code lines to your app's .js (or App.tsx) file to do so:

SwaarmClient.init("track.mycompany.swaarm-app.com", "abc123");

To enable Swaarm SDK logging you can pass a third parameter as follows:

SwaarmClient.init("track.mycompany.swaarm-app.com", "abc123", true);

or call the log method at any point in the execution of the app as shown below:

SwaarmClient.log(true)

Step 4: Record Events

To record any event you can use the SwaarmClient.event method which takes the following 3 parameters:

  • Event Type Id - the name of the event that you wanted to track as defined in Swaarm platform under App Events using the Mapping ID field. This can be "registration" or "first_time_deposit" etc.

  • Aggregated value - this is a numeric value that Swaarm will hold for you and can aggregate in the reports. Could be the amount of coins the user purchased, how many tasks they completed etc.

  • Custom value - this is a free field that can be used to store any additional information about the event. For a registration you might want to put the user's id or the email. For a payment you might want to store the package that the user selected. You can even send JSON and then analyse it using our SQL Lab.

The following codes are some of the examples of how you can register events with the Swaarm SDK.

Track the number of coins earned by a user:

SwaarmClient.event('earned_coins', 120, '{"plan": "premium"}');

To register a purchase event you can use the more specialized SwaarmClient.purchase call. By default, this method requires a monetary value and a currency.

SwaarmClient.purchase("registration", 12.42, "USD",  "iOS purchase or subscription receipt", "android purchase or subscription id")

💡Tip:

To enable the validation of purchases made using App/Play Store functionality you can pass the receipt or purchase id.

✏️Note:

The SDK will automatically record app installations, open events or reinstalls, there is no need to fire such events

Step 5: Build your App and Publish it

Congratulations, you are now ready to build, run and distribute your app. Swaarm SDK is all set to start tracking your events and attributing them!

Did this answer your question?