Geofence Event trigger email

I am trying to work out how to get a geofence event ( i.e moved outside geofence) to trigger an email. I have used actions before but do I have to use analysis? If so do you have an example snippet. It takes me ages to work out my scripts!!

Hi, @kim

To trigger an action such as sending an email from a moved outside geofence event, you need to use an Analysis for that. I don’t have a snippet that does exactly what you said, but I do have a snippet that checks whether a point is within a geofence or not.

const Device = require('tago/device');
const Analysis = require('tago/analysis');
const Utils = require('tago/utils');

 * Checks if point is inside polygon.
 * @param {Array} point Array that contains point's latitude and longitude
 * @param {Object} geofence Object that contains the geofence points
function withinPolygon(point, geofence) {
    const x = point[0];
    const y = point[1];
    let inside = false;
    for (let i = 0, j = geofence.length - 1; i < geofence.length; j = i++) {
      const xi = geofence[i][0];
      const yi = geofence[i][1];
      const xj = geofence[j][0];
      const yj = geofence[j][1];
      const intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
      if (intersect) inside = !inside;
    return inside;

// Snippet to check if a point is inside a geofence
async function checkIfPointIsWithinGeofence(context) {
    // Configurating device, services and account objects
    const envVars = Utils.env_to_obj(context.environment);
    if (!envVars.device_token) return context.log('Missing device_token environment variable');
    const device = new Device(envVars.device_token);

    // Getting your geofence
    const [myGeofence] = await device.find({ variable: 'geofence', qty: 1 });
    if (!myGeofence) return context.log('There is no geofence to verify!');

    // Getting the last device location
    const [deviceLocation] = await device.find({ variable: 'yourDeviceLocation', qty: 1 });
    const point = [deviceLocation.location.coordinates[1], deviceLocation.location.coordinates[0]];

    const isWithinGeofence = withinPolygon(point, myGeofence.metadata.geolocation.coordinates[0]);

    if (isWithinGeofence) {
        // do something here
    } else {
        // do something here

module.exports = new Analysis(checkIfPointIsWithinGeofence, 'YOUR ANALYSIS TOKEN HERE');

Note that this is a generic snippet, you need to customize it for your business rule. Here are also some links that can help you with your scripts:

I hope you can make progress with the snippet and the links I sent you, if you have further questions, feel free to ask me.


Thank you. Very helpful. This at least gives me a good start.