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:
Add
region,currencyandlanguage* to thecontextobject. 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
localewhich is equivalent to ourlocale.language(e.g. en-US), so we could setcontext.locale: locale.languageinstead ofcontext.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:
- Call
identifyanonymously on page load with our locale astraitse.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
localedata ascontext.traitsvia some middleware for all other calls?
- Therefore, I think we'd have to additionally pass our
- Having tested this out it appears identified traits aren't sent along with subsequent events (track, page, etc.)
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'?
- Define a separate segment.io
sourcefor 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?