Track dynamic variables

A newer version of this documentation is available. Switch to the latest version docs.

Dynamic variables are additional information on the session that can be used to segment sessions.

For example, they can include information on the A/B Test variations displayed to the current user.

  • It is possible to save up to 40 distinct dynamic variable keys per screen view. If more are received, only the first 40 keys will be kept.
  • If you are using the same key twice, the last value associated with the key will be collected.
  • Every dynamic variable is composed of a pair of key (max. 512 characters) and value (max. 255 characters string or number of type Long between 0 and 232 - 1). In case these maximums length are reached, the SDK will automatically trim the exceeding characters.
  • If key or value are empty, the SDK will instead send the literal string “cs-empty”.

To define and send a dynamic variable, you just need to use the following API. For each dynamic variable, we send:

  • Key (string, mandatory)
  • Value (string or integer, mandatory)

Note that if you send an integer as a value, it has indeed to be an integer. A null or a float will not be accepted as a valid parameter, and will result in an error.

Also, in order for you to be able to handle such errors happening when we try to send the dynamic variable (see the constraints on keys and values in the Limits section above), you can also add, as a last parameter, a function that we will call so you can handle it gracefully. Note that if you do not add that callback, we will log the error’s message on the console whenever it happens.

import Contentsquare from '@contentsquare/react-native-bridge';
// alphanumeric value: Contentsquare.sendDynamicVar(string, string, ({ name, message, stack }: { name: string, message: string, stack: string }) => {} ?)
Contentsquare.sendDynamicVar("my key", "my value 1");
Contentsquare.sendDynamicVar("my key", "my value 2", ({ message }) => {
Toast.show(message, 0.3);
});
// numeric value: Contentsquare.sendDynamicVar(string, number, ({ name, message, stack }: { name: string, message: string, stack: string }) => {} ?)
Contentsquare.sendDynamicVar("my key", 10);
Contentsquare.sendDynamicVar("my key", 5000, (errorObject) => {
Console.log(errorObject.toString());
});

Type of the value — The value can be either a number or a string. For each case, available features won’t be the same in the Contentsquare app:

  • For number, you will be able to do some algebra. Example: sessions with dynamic variable key = “numberOfFriends” and value >= 10
  • For string, auto-completion and Regular Expression will be available. Example: sessions with dynamic variable key = “accountType” and value is “Premium”

Sending a dynamic variable for each session

Section titled Sending a dynamic variable for each session

You may want to send a dynamic variable for each session (like the user’s country or store). While triggering the dynamic variable at app launch will cover most cases, it will not be enough. A session can start when the app is put in foreground, after staying in background for more than 30 minutes. See Session definition section for more information.

That is why we also recommend sending such dynamic variable every time the app enters foreground.

You can use the AppState API to detect foreground and trigger a dynamic variable.