Home Manual Reference Source Repository

Plugins/Geoservice

The Geoservice module provides an easy-to-use interface for visitor geolocation and currency information.

Getting started

Get visitor geolocation info

ES6:

import {plugins} from '@eastsideco/escshopify';

const geoService = new plugins.GeoService;

async function alertVisitorCountry() {
    var geoInfo = await geoService.lookupGeo();
    alert(geoInfo.country.iso_code);
}

Get currency exchange rates

Note: It's highly recommended to use the EasyCurrency plugin instead of re-implementing currency conversion yourself - it had more features and covers many easy-to-miss edge cases.

ES6:

import {plugins} from '@eastsideco/escshopify';

const geoService = new plugins.GeoService;

class VisitorCurrencyConverter {
    constructor(baseCurrency) {
        this._baseCurrency = basyCurrency;
        this._visitorCurrency = 'USD';
        this._rates = null

        this._getRates();
        this._getVisitorCurrency();
    }

    async _getVisitorCurrency() {
        var geoInfo = await geoService.lookupGeo();
        this._visitorCurrency = geoInfo.currency;
    }

    async _getRates() {
        var res = await geoService.getCurrencyInfo();
        this._rates = res.rates;
    }

    convert(amount) {
        var baseToUsd = this._baseCurrency == 'USD' ? 1 : this._rates[this._baseCurrency];
        var usdToVisitor = this._visitorCurency == 'USD' ? 1 : this._rates[this._visitorCurrency;

        var rate = usdToVisitor / baseToUsd;

        return amount * rate;
    }    
}