Hi everyone, I am working on a multi-tenant application setup where users can access:
customer1.example.com for Customer 1's tenant customer2.example.com for Customer 2's tenant - And so on
In my code, I have a tenant variable that is sent to Amplitude, as shown below:
useEffect(() => {<br /> if (!userId || !tenant) {<br /> return;<br /> }<br /> amplitude.init(AMPLITUDE_ID, { userId });<br /> const identifyEvent = new amplitude.Identify();<br /> identifyEvent.setOnce('tenant', tenant);<br /> amplitude.identify(identifyEvent);<br />}, [userId, tenant]);
I am using @amplitude/analytics-browser version ^2.11.2. This works well for our custom events when triggered (like page load or button clicks) as the tenant property is included.
However, we also have Amplitude's autocapture enabled, which captures two initial events on page load that lack the tenant property:
[Amplitude] Start Session - missing tenant [Amplitude] Page Viewed - missing tenant Page Load View - includes both tenant and userId
See the screenshot for more detail
When session start and the first Page Viewed event, tenant didn’t get sent as expected

When I send my custom event Home Page Load, tenant is being sent as expected

Question: Is there a way to delay Amplitude initialisation until a certain user property, like tenant, is set? Or are there alternatives to ensure these autocapture events include tenant?
Without this, we risk having a large amount of data that is missing the tenant, which is problematic for our multi-tenant analytics. Thanks so much for all of your help!
What I have try so far
I noticed in the documentation that trackOn can be used for conditional autocapture. I attempted the following code to ensure Page Viewed events only trigger when both tenant and userId are available. However, I still see events being sent without the tenant, so this does not seem to work as expected.
Documentation reference: https://amplitude.com/docs/sdks/analytics/browser/browser-sdk-2#track-page-views
amplitude.init(AMPLITUDE_ID, {<br /> userId,<br /> autocapture: {<br /> pageViews: {<br /> trackOn() {<br /> return Boolean(tenant && userId);<br /> },<br /> },<br /> },<br />});<br />