Add User's Current GeoIP to Email Template

Overview

This article details how to get a user’s current GeoIP data displayed in a custom email template.

Applies To

  • GeoIP
  • Custom Email Template
  • User Metadata
  • Current Location
  • Post Login Action

Cause

Because the common variables for email templates can be limited, sometimes assigning data to the user’s metadata object is the best solution. Although user.location is unavailable, the template can use user.user_metadata.

Solution

First, this data must be set before calling on user.user_metadata. This can be achieved via a PostLogin Action.

exports.onExecutePostLogin = async (event, api) => {

let city = event.request.geoip.cityName;
let country = event.request.geoip.countryName;

api.user.setUserMetadata("city", city);
api.user.setUserMetadata("country", country);
};

In this Action script, I am creating “city” and “country” user_metadata. Because this is triggered at every login, the user’s current location will override the previous location.

Now that the metadata is set to the user object, we can edit the custom email template to pull this data. Navigate to the custom HTML and reference the user’s location via the following variable:

{{user.user_metatdata.city}}
{{user.user_metatdata.country}}