Location Tracking

To set a user’s current location, use the getCurrentPosition() method of the Geolocation API and log the location data to Braze.

1
2
3
4
5
6
7
8
9
10
11
12
function success(position) {
  var coords = position.coords;
  appboy.getUser().setLastKnownLocation(
    coords.latitude,
    coords.longitude,
    coords.accuracy,
    coords.altitude,
    coords.altitudeAccuracy
  );
}

navigator.geolocation.getCurrentPosition(success);

Note that calling navigator.geolocation.getCurrentPosition() will immediately request permission from the user unless they have already granted or denied permission. See the JSDocs for more information on setting the user’s last known location.

Logging A Single Location

Additionally, when the Web SDK sends data to Braze servers, the user’s country will be automatically detected from their IP Address if it has not been manually set by your application.

Continuous Tracking

If you’d like to continuously track a user’s location during a page load, use the watchPosition() method of the Geolocation API. This method will invoke the success callback each time the user’s location is updated.

1
2
3
4
5
6
7
8
9
10
11
12
function success(position) {
  var coords = position.coords;
  appboy.getUser().setLastKnownLocation(
    coords.latitude,
    coords.longitude,
    coords.accuracy,
    coords.altitude,
    coords.altitudeAccuracy
  );
}

navigator.geolocation.watchPosition(success);

Calling navigator.geolocation.watchPosition() will immediately request permission from the user unless they have already granted or denied permission. For information about configuring and stopping the location tracking, see the Mozilla developer docs.

WAS THIS PAGE HELPFUL?
New Stuff!