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.
This is Yuanyuan from the Amplitude Support team - thank you for the detailed description!
trackOn is only set on initialization at Amplitude-TypeScript/packages/plugin-page-view-tracking-browser/src/page-view-tracking.ts at 46d77ddb057ebc1b2775212e840692541338d9ba · amplitude/Amplitude-TypeScript · GitHub. It won’t listen for updates in browser config. Would it help you case if it is configured to be dynamic?
Best, Yuanyuan
P.S. Checkout upcoming events and user meetups on our events page.
Hi Yuanyuan, thanks for your swift response.
Would it help your case if it is configured to be dynamic?
I am not entirely sure what you mean here. Based on your response, I checked out page-view-tracking.ts in the Amplitude-TypeScript library. From my testing, it appears that the function I provide to trackOn is evaluated each time I navigate within the browser, meaning it re-evaluates, not just on the initial load.
I tried using trackOn since I could not find any other options. Ideally, I would like tenant to work similarly to amplitude.setUserId(), where userId is set before the session initialises. Do you happen to know of any way to set a user property before Amplitude begins tracking?
Thanks again for your help.
Hi Trung,
By 'dynamic', I was referring to `trackOn` listening for updates in browser config, instead of only being set at initialization. When you change the URL, does amplitude gets initialied each time for a new URL?
Best, Yuanyuan
P.S. Checkout upcoming events and user meetups on our events page.
Hi Yuanyuan, we are using Amplitude in a Next.js application, which means Amplitude is initialised only once. Subsequent URL changes will not reinitialise Amplitude, as the view updates without a full page reload. Hope this answers your question.
Hi Trung,
Thanks for letting me know! Let me check with the engineer and get back to you!
Best, Yuanyuan
P.S. Checkout upcoming events and user meetups on our events page.
Hi Trung,
The engineer clarified that trackOn() should execute every time when url changes but the value of trackOn() is unchanged after initialization.
> I would like _tenant_ to work similarly to `amplitude.setUserId()` , where `userId` is set before the session initialises. Do you happen to know of any way to set a user property before Amplitude begins tracking?
For this use case, you can call identify before init. For example,
const identifyEvent = new amplitude.Identify();identifyEvent.set('tenant', 'tenant-id'); amplitude.identify(identifyEvent);amplitude.init();
Let me know if this helps!
Best, Yuanyuan
P.S. Checkout upcoming events and user meetups on our events page.
Hi Yuanyang, thank you for your reply the other day. I have applied the code on our staging environment to first call identify and then amplitude.init. However, the traffic still shows events being sent without a tenant value.
We are using the 'Any Active Event' option provided by Amplitude.
Is there anything I might be missing? During local testing, I also noticed events are sent without a tenant, even when the init call is placed after identify
The result is similar to what I share with you earlier a week ago
Would you be able to share any additional insights or check with the engineering team? I would greatly appreciate it. Thank you!
Hi Trung,
Thanks for getting back to me with feedback!
I checked the raw data and saw the identify call is still fired after the page view event. I am checking with the engineer again and will keep you updated.
Best, Yuanyuan
P.S. Checkout upcoming events and user meetups on our events page.
Thank you Yuanyuan for your support. Looking forward to your response. For now, have a great weekend!
You are very welcome Trung. I will keep you updated!
Best, Yuanyuan
P.S. Checkout upcoming events and user meetups on our events page.
Thank you, Yuanyuan and Roque, for your input—it looks very promising. I have pushed the changes to our staging environment for testing and will monitor them over the next few days. I will keep you updated on the results.
If you don't have an Amplitude account, you can create an Amplitude Starter account for free and enjoy direct access to the Community via SSO. Create an Amplitude account. You can also create a Guest account below!
If you're a current customer, select the domain you use to sign in with Amplitude.
If you don't have an Amplitude account, you can create an Amplitude Starter account for free and enjoy direct access to the Community via SSO. Create an Amplitude account. Want to sign up as a guest? Create a Community account.
If you're a current customer, select the domain you use to sign in with Amplitude.