Use the RudderStack Cordova SDK to send events from your Cordova apps to various destinations.
9 minute read
The RudderStack Cordova SDK lets you track event data from your Apache Cordova applications and send it to your specified destinations via RudderStack.
Once signed up, set up an Cordova source in the dashboard. You should be able to see a Write Key for this source, as shown below:
You will also need a data plane URL. Refer to the Glossary for more information on the data plane URL and where to find it.
The Setup tab in the RudderStack dashboard (seen above) has an SDK installation snippet containing both the write key and the data plane URL. You can use it to integrate the Cordova SDK into your project.
Installing the Cordova SDK
To add the Cordova SDK as a dependency, navigate to the root folder of your application and run the following command:
cordova plugin add rudder-sdk-cordova
The Cordova SDK supports device mode starting from version 1.3.0.
Initializing the RudderStack client
After adding the SDK as a dependency, you need to set up the SDK.
Add the following code in the onDeviceReady() function of your home page to initialize the SDK.
The number of events included in a batch request to the server.
30
dbCountThreshold
Integer
The number of events to be saved in the SQLite database. Once the limit is reached, older events are deleted from the database.
10000
sleepTimeout
Integer
Minimum waiting time to flush the events to the server.
10 seconds
configRefreshInterval
Integer
RudderStack fetches the config after this time interval.
2
autoCollectAdvertId
Boolean
Determines if the SDK should automatically collect the advertisement ID on Android platforms.
false
trackLifecycleEvents
Boolean
Determines if the SDK should capture the application lifecycle events automatically.
true
Identify
The identify call lets you identify a visiting user and associate them with their actions. It also lets you record the traits about them like their name, email address, etc.
As a best practice, we recommend calling identify at the start of every session or page load for logged-in users. This will ensure allir latest traits are captured in all subsequent events.
You can disable tracking these events by setting trackLifecycleEvents to false while initializing the SDK. However, it is highly recommended to keep it enabled.
Group
The group call lets you associate an identified user to a group - either a company, project, or a team, and record any custom traits or properties associated with that group.
Extra properties that you want to pass along with the screen call.
options
JSON Object
Optional
Extra options to be passed along with screen event.
Alias
The alias call lets you merge different identities of a known user.
alias is an advanced method that lets you change the tracked user’s ID explicitly. This method is useful when managing identities for some of the downstream destinations.
A sample alias call is shown below:
RudderClient.alias("userId")
The alias method has the following signature:
Name
Data Type
Presence
Description
newId
String
Required
The new userId that you want to assign to the user.
You can use the reset method to clear the persisted traits from the identify call. We recommend calling it during the Logout operation.
A sample reset call is as shown:
RudderClient.reset()
Configuring the options object
The options object can be sent along with all above-mentioned API calls. It has the following signature:
Name
Data Type
Presence
Description
externalIds
JSON Object
Optional
Each key within externalIds object should define the type of external ID, and its value should be a String or Integer.
integrations
JSON Object
Optional
Each key within the integrations object should hold the display name of your desired destination. Its value should be a boolean indicating whether you want to send that event or not. For more details check the Enabling/disabling events for specific destinations section below.
A sample options object for an identify event is as shown:
Enabling/disabling user tracking via the optOut API (GDPR support)
RudderStack gives the users (e.g., an EU user) the ability to opt out of tracking any user activity until the user gives their consent. You can do this by leveraging RudderStack’s optOut API.
The optOut API takes true or false as a Boolean value to enable or disable tracking user activities. This flag persists across the device reboots.
The following snippet highlights the use of the optOut API to disable user tracking:
RudderClient.optOut(true);
Once the user grants their consent, you can enable user tracking once again by using the optOut API with false as a parameter sent to it:
RudderClient.optOut(false);
The optOut API is available in the Cordova SDK starting from version 1.0.1.
Filtering events
When sending events to a destination via device mode, you can explicitly specify which events should be discarded or allowed to flow through - by allowlisting or denylisting them.
Enabling/disabling events for specific destinations
RudderStack lets you send your event data only to the explicitly specified destinations and filtering out the rest. You can do this in one of the following two ways:
While initializing the Cordova SDK
While making the event calls
Passing destinations during SDK initialization
This approach is useful when you want to send the events to specific destinations across all event calls made using the SDK.
This approach is useful when you want to send particular events to specific destinations, or if you want to override the destinations specified during the SDK initialization for a particular event.
In iOS, you need to call the putAdvertisingId method before calling initialize.
Setting the device token
You can pass your device-token for push notifications to be passed to the destinations which support the Push Notifications feature. RudderStack sets the token under context.device.token.
An example of setting the device-token is as shown:
RudderClient.putDeviceToken("sampleDeviceToken");
Debugging
If you face any unexpected behavior while using the SDK, you can turn on the VERBOSE or DEBUG logging feature to determine out the issue.
You configure logging behavior of your SDK by sending the value of the logLevel property of the configuration object and pass it over to the initialize call as shown below:
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.