How to pass common data (region, currency, language) to segment.io using analytics.js

483 Views Asked by At

For some background, we're integrating with https://segment.com/docs/ on our e-commerce site.

Our site currently has 3 regions (US, CA and UK).

The region is identified by different url paths, (e.g. /* (US), /ca/* (CA), /uk/* (UK)).

We define a locale object depending on which region you visit, e.g.

// A user who visits /uk/* will have this locale
const locale = {
    region: 'UK',
    currency: 'GBP', // one day this may be configurable
    language: window.navigator.language // en, en-US, en-GB etc.
}

// A user who visits /* will have this locale
const locale = {
    region: 'US',
    currency: 'USD', // one day this may be configurable
    language: window.navigator.language // en, en-US, en-GB etc.
}

We'd like to have our locale data associated with segment API calls (track, page, identify, etc.)


I can see 3 potential options:

  1. Add region, currency and language* to the context object. e.g.

    window.analytics.track('Button Clicked', {}, {
      context: {
        region: locale.region,
        currency: locale.currency,
        language: locale.language
      }
    });
    
    • This would allow all API calls (track, page, identify, etc.) to associate the call with the current locale.
    • I expect this should be done via analytics.js middleware?
    • *I know the context spec already defines locale which is equivalent to our locale.language (e.g. en-US), so we could set context.locale: locale.language instead of context.language: locale.language.

My main concern with the above is I'm not 100% sure it's acceptable to add our own data to the context object, the documentation merely says "so properties outside the spec are ignored", however I expect our data warehouse could still utilise these extra properties?

Therefore, an alternative approach is to perhaps treat it as a user trait instead:

  1. Call identify anonymously on page load with our locale as traits e.g.
    window.analytics.identify({
        region: locale.region,
        currency: locale.currency,
        language: locale.language
    });
    
    • Having tested this out it appears identified traits aren't sent along with subsequent events (track, page, etc.)
      • Therefore, I think we'd have to additionally pass our locale data as context.traits via some middleware for all other calls?

My concern with this 2nd approach however is, given our locale data is temporal (the user could navigate to a different region at any time / it's not stored against a user record in a DB), it perhaps doesn't belong as a user 'trait'?

  1. Define a separate segment.io source for each region e.g.
    window.analytics.load("REGION_SPECIFIC_WRITE_KEY");
    
    • I'm not sure whether this is safe given they all live on the same domain?

Is there a recommended design for this?

0

There are 0 best solutions below