radar-sdk-js
package on npm here.
Install
In an HTML page, add the SDK script and stylesheet:Setup
Initialize
Initialize the library with your publishable API key, found on the Settings page:You must initialize the library before calling any other functions.
Name | Default | Possible values | Description |
---|---|---|---|
logLevel | error | none , info , warn , error | Determines the log level for console logging. Defaults to error when using a test publishable key. |
cacheLocationMinutes | null | number | Determines whether to reuse previous location updates. A cache ttl in minutes. If null or 0 , will always request a new location. |
locationMaximumAge | null | number | Determines whether to use old browser locations. A maximum age in milliseconds. If null or 0 , will always request a new location. See getCurrentPosition options(). |
locationTimeout | 30000 | number | The number of milliseconds to wait for a location update before an error is thrown. Defaults to 30 seconds. |
desiredAccuracy | high | high , medium , low | Determines the desired accuracy of location updates. Note that higher accuracy can result in slower response times. |
Geofencing
Identify user
To identify the user when logged in, call:userId
is a stable unique ID for the user.
To set an optional dictionary of custom metadata for the user, call:
metadata
is a JSON object with up to 16 keys and values of type string, boolean, or number.
Finally, to set an optional description for the user, displayed in the dashboard, call:
description
is a string.
You only need to call these functions once, as these settings will be persisted across browser sessions.
Learn more about when Radar creates new user records here.
Foreground tracking
Once you have initialized the SDK and the user has granted permissions, you can track the user’s location. The SDK uses the HTML5 geolocation API to determine the user’s location. To track the user’s location, call:err
will be one of:
RadarPublishableKeyError
: SDK not initializedRadarLocationPermissionsError
: location permissions not grantedRadarLocationError
: location services error or timeout (10 seconds)RadarTimeoutError
: network timeout (10 seconds)RadarBadRequestError
: bad request (missing or invalid params)RadarUnknownError
: unauthorized (invalid API key)RadarPaymentRequiredError
: payment required (organization disabled or usage exceeded)RadarForbiddenError
: forbidden (insufficient permissions or no beta access)RadarNotFoundError
: not foundRadarRateLimitError
: too many requests (rate limit exceeded)RadarServerError
: internal server errorRadarUnknownError
: unknown error
err
will include an HTTP response code and the API response body: