Skip to main content
Gainsight Inc.

Install Gainsight PX React-Native

This article explains how developers can use the Gainsight PX React Native code in their React Native application.

Overview

Gainsight PX can effectively monitor your application,  track usage data, and provide real-time analytics. 

You can use Gainsight PX React Native in your mobile application development. This codebase is platform-independent and works with Android, iOS, and Windows Operating systems. You can ingest the Gainsight PX code into any mobile application which is developed using the React Native programming language. When you ingest Gainsight PX’s React Native code in your React Native based application code, Gainsight PX starts tracking all of the data on your application.  

This article explains various facets of ingesting Gainsight PX React Native code in your iOS application. 

Use Gainsight PX

To use Gainsight PX in your application, you must first download and install React Native. Once completed, you must install Gainsight PX for Android or iOS, based on the platform you are using. After installing Gainsight PX, initialize Gainsight PX and then create events to track data from your application. 

The following flow chart showcases a high-level overview of the tasks to be accomplished in the usage of Gainsight PX.

Flow diagram.png

The following are the steps required:

The first step requires developers to download and install the Gainsight PX package and other prerequisites on their system.  

Download and Install React Native

To download and install React Native, you must use Gainsight PX’s Tar Archive (.tgz extension) file. This file is a prerequisite to use Gainsight PX.  You can download this file from the Gainsight PX server. Once you download the file, you must add it as a dependency in your JSON file and then install React Native. 

To download react-native Module:

  1. Download react-native-gainsight-px-x.x.x.tgz.

  2. Store the .tgz extension file in the required location.

  3. Add the .tgz file as a dependency in your application `package.json` file; Add the following code to the Dependencies section:

"react-native-gainsight-px": "file:<path-of react-native-gainsight-px-x.x.x.tgz-file>"

OR,

`yarn add react-native-gainsight-px --save`
  1. Save and close your application JSON file.

  2. Launch Terminal.

  3. Install Gainsight PX by running the following code: 
    Note: Press Enter after each line of code.

cd <file path of your application JSON file>
npm install

Alternatively, you can also use the following single line code instead of two lines of code and then press Enter.

npm install <file-path-of-tgz> 

Add PXKit Framework for iOS

This section explains how to add PXKit framework for an iOS/Xcode project. To install Gainsight PX iOS, you must use the PXKit.xcframework file. You can download this file from the PX dashboard.

Follow the steps below:

  1. Copy RNGainsightPx.h and RNGainsightPx.m from node_modules to Libraries folder in Xcode.

  2. Download the PXKit.xcframework file. 

  3. In the Xcode, select Target, and add the above framework file to the Embedded Binaries.

  4. Launch Terminal.

  5. Execute the following code to install Gainsight PX:

cd <file path of your project folder>
react-native run-ios

Note:

If you targeting Android, all configurations are taken care of by React-Native plugin.
 

Initialize Gainsight PX 

ExceptionCallback

 

All exceptions occurred when creating events are passed into catch as error using GainsightPXCallback interface. The properties of GainsightPXCallback are as follows:

export interface GainsightPXCallback {
  status: GainsightPXCallbackStatus;
  methodName?: string;
  params?: object;
  exceptionMessage?: string;
}
 
export enum GainsightPXCallbackStatus {
  FAILURE = 0,
  SUCCESS = 1
}
  1. The status property informs if the created event is success/failure. 
  2. The methodName property informs about the invoked function name.
  3. The params object gives the event properties that were sent to create an event.
  4. The exceptionMessage tells about the exception occured.

Once you complete the download and installation of Gainsight PX, you can use it in your application to track data. The code used in this section is platform-independent and can be used in Android, iOS, or Windows-based mobile applications. 

To track data with Gainsight PX: 

  1. Insert the following import code in the header section:

import {GainsightPX} from 'react-native-gainsight-px';

  1. Use the following code to initialize Gainsight PX. You must use the initialization code only once in your entire application. Gainsight PX recommends using this code in the first screen displayed to users (launch screen or login screen) so that tracking can be initialized from the first page of your application. 

let configurations = new Configurations (" <API KEY> ")
configurations.enableLogs = true
    GainsightPX.getInstance().initialize(configurations)
    .then(() => {
            console.log("GainsightPX is initialized");
    })
    .catch((error) => {
            console.log(error.status);
            console.log(error.exceptionMessage);
    })

Note:

All the Gainsight PX methods return a Promise<GainsightPXCallBack>

Screen Events

This section explains how to create events that can track data from your mobile application. Events are actions associated with your app and allow you to track various actions performed by users in your app. Currently, the Gainsight PX SDK supports four types of events.

You can use the Screen event to track each of your screens visited by the user. The syntax to capture screen events is as follows:

GainsightPX.getInstance().screenEvent("<#screen-name#>", "<#screen-class#>");

You can also create a screen event to just capture the name of the screen and exclude all of the other details.

GainsightPX.getInstance().screen("<#screen-name#>", "<#map#>")

Custom Events 

Custom Events are used to track specific actions in your app.

The syntax to track custom events is:

GainsightPX.getInstance().customEventWithProperties(<#custom_event_name#>, <#map#>);

You can also track custom events without any properties. The syntax for this is:

GainsightPX.getInstance().customEvent(<#custom_event_name#>);

Identify Events

These events are used to identify a user or an Account uniquely.

Gainsight PX provides you with the following syntax:

GainsightPX.getInstance().identifyWithUserId(<#user_id#>)
GainsightPX.getInstance().identify(<#user#>, <#account#>)

The following line of code can be used to track a user: 

var user = new PXUser("<#user_id#>")

user.usem = "david@gmail.com"

The above line of code identifies the user, since you have added used the usem property to define the user. 


If you use an undefined property with user, the user record is tracked as a custom attribute.

user.nickName = 20

The list of properties supported for User tracking is given in the table below:

Property Data type
ide String
usem String (Email)
userHash String
gender  String
lastName String
firstName String
signUpDate Date
title String
role String
subscriptionId String
phone String
countryCode String
countryName String
stateCode String
stateName String
city String
street String
continent String
postalCode String
regionName String
timezone String
longitude Double
latitude Double
organization String
organizationEmployees String
organizationRevenue String
organizationIndustry String
organizationSicCode String
organizationDuns Int
accountID String
firstVisitDate Date
score Int
sfdcContactId String
<AnyKey> <Value>(All other keys other than default keys will map to `customAttributes`)


The list of Attributes for Account Object is given in the table below:

Property  Data Type
name String
trackedSubscriptionId String
industry String
numberOfEmployees int
sicCode String
website String
naicsCode String
plan String
countryCode String
countryName String
stateCode String
stateName String
city String
street String
continent String
postalCode String
regionName String
timezone String
latitude Double
longitude Double
sfdcId String
<AnyKey> <Value>(All other keys other than default keys will map to `customAttributes`)

Set Global Context 

Once you create all the required events, you can set events with a global context. You can set a global context with key-value pairs and send them with the payload. Global context can be set on Date, String, Double, and Boolean data types. Global Context data is stored at the memory level and not disk-level. The key-value pairs are sent with the Payloads. If an application is terminated, all the key-value pairs are erased. 

The following code snippet can be used to set the global context:

GainsightPX.getInstance().setGlobalContext(<#[String: GlobalContextValue]#>);

The following code snippet can be used to check if the key is available:

GainsightPX.getInstance().hasGlobalContextKey(<#Name of the key#>)

The following code snippet can be used to remove the key:

GainsightPX.getInstance().removeGlobalContextKeys(<#Array Of Keys#>)

General Methods

This section describes the general methods (functions) of Gainsight PX SDK. There are three general methods:

Method Name Method Description
Flush GainsightPX.getInstance().flush() This method sends all events in the queue to the server, by ignoring the scheduled auto flush. Generally, you need not use this method but there could be situations in which you may need to flush out events
Enable/Disable GainsightPX.getInstance().enable()

GainsightPX.getInstance().disable()
These methods help you to enable or disable event tracking on your app. When you use the disable method, no event is sent to server. You can use the disable method on data sensitive areas of your app.

Gainsight PX Configurations

This section describes methods, whose configurations can be modified by you during the initialization stage. 

Method Call Default Description Override by Server?
flushQueueSize 20 The number of events that are considered to form a batch to be sent to server. Once this limit is reached, PX sends the events to the server even if it's still not the time for that (by the timer) Yes
flushInterval 20 sec The time interval during which the client checks if there are any new events to send to the server Yes
collectDeviceId (this method is applicable only to Android application) true Whether or not to collect the device id from the device No
enableLogs False Which log level should be logged to CatLog. By default, false is logged No
trackApplicationLifecycleEvents true Should the client auto-track app related events (APP_OPENED, APP_INSTALLED and APP_UPDATED) No
(Server can drop the event based on remote configuration)
shouldTrackTapEvents false

Should the client auto-track tap events

Note: Enabling this helps you track only the single tap gestures.

Yes
recordScreenViews true Whether or not the client should auto track, this is not fully supported in React-Native, need to manually collect the  screen event. No (Server can drop the event based on remote configuration)
  • Was this article helpful?