Solved

Cross domain tracking - typescript

  • 29 September 2022
  • 15 replies
  • 1545 views

Hi Amplitude,

 

We want to cross domain tracking for our landing site and product site. We found guide for cross domain tracking for JS https://developers.amplitude.com/docs/advanced-settings, but cant find the guide for typescript. 

How should I configure for cross domain tracking with typescript SDK then?

 

Thanks in advance.

icon

Best answer by jarren.patao 27 October 2022, 18:12

View original

15 replies

Userlevel 4

Hi @shirleyfang,

Cross domain tracking should be handled relatively the same as the legacy JS SDK with slightly different syntax, but you can find our documentation on this in our new developer docs which I'll include a snippet of here: https://www.docs.developers.amplitude.com/data/sdks/typescript-browser/#cross-domain-tracking 

 

You can track anonymous behavior across two different domains. Amplitude identifies anonymous users by their device IDs which must be passed between the domains. For example:

  • Site 1: www.example.com
  • Site 2: www.example.org

Users who start on Site 1 and then navigate to Site 2 must have the device ID generated from Site 1 passed as a parameter to Site 2. Site 2 then needs to initialize the SDK with the device ID. The SDK can parse the URL parameter automatically if deviceId is in the URL query parameters.

  1. From Site 1, grab the device ID from getDeviceId().
  2. Pass the device ID to Site 2 via a URL parameter when the user navigates. (for example: www.example.com?deviceId=device_id_from_site_1)
  3. Initialize the Amplitude SDK on Site 2 with init('API_KEY', null).

If the deviceId isn't provided with the init like init('API_KEY', null, { deviceId: 'custom-device-id' }), then it automatically fallbacks to use URL parameter.

 

Hope this helps!

Userlevel 1
Badge +1

For instructions like “grab the ID with getDeviceId()” is that possible to do with Google Tag Manager? Trying to understand how we would do this in our setup more specifically, which is mostly GTM with some app SDK. Or is it an event we would set up in Amplitude? We’re mid-implementation but want to understand this better.

Thank you

Userlevel 5
Badge +8

@mkoiva Hi! Thanks for writing in and apologies for the delay. I’ve shared your question over to our support team who can help get this sorted and also other implementation questions you might have.

Following

Userlevel 1
Badge +1

Thanks @shirleyfang, when you say “passed as a parameter” and “Pass the device ID to Site 2 via a URL parameter “ where specifically is that referring to or how would we accomplish that? We’re using the GTM SDK and trying to understand how this would be accomplished. Thanks

Badge +2

@Esther Trapadoux I am having the same issue as @mkoiva and am frantically looking for a solution. Is this achievable though GTM or a combination of react/js and GTM? Thanks!

 

@mkoiva Hi! Thanks for writing in and apologies for the delay. I’ve shared your question over to our support team who can help get this sorted and also other implementation questions you might have.

 

Userlevel 1
Badge +1

@Linus Lennstrand talk to your Amplitude representative or contact. We worked with ours and they gave us a cross domain work around. They are working on making it live in the GTM template but it is not live there yet.

Badge +2

@Linus Lennstrand talk to your Amplitude representative or contact. We worked with ours and they gave us a cross domain work around. They are working on making it live in the GTM template but it is not live there yet.

Thanks a lot! Can you share some details of that solution while I wait for their reply?

Badge

Hi @Linus Lennstrand 

This is the cross-domain workaround. https://github.com/amplitude/GTM-cross-domain-script. This script will append deviceId into the URL during redirect. If the other domain has amplitude installed, the SDK will auto-capture the deviceId from the URL and set that deviceId for you. 

Thanks.

Badge +2

Hi @Linus Lennstrand 

This is the cross-domain workaround. https://github.com/amplitude/GTM-cross-domain-script. This script will append deviceId into the URL during redirect. If the other domain has amplitude installed, the SDK will auto-capture the deviceId from the URL and set that deviceId for you. 

Thanks.

Thanks a lot @Alyssa Yu! I’ll try this out right now. 

Badge +2

@Alyssa Yu I have tried this now and unfortunately it’s still not working. The script is running, but it eventually times out. I have attached the console log messages and from what I can see amplitudeGTM.getDeviceId() returns undefined

 

 

 

 

Badge +2

I realised I missed changing to the custom instance name. After doing that, I can access the amplitude instance through the console, however the script is still not working. 

Badge +2

Ok, problem solved! The issue was that we are running a non-standard Amplitude instance name, so had to change the code to use amplitudeGTM._iq["instance-name"].getDeviceId() to access the device id. Thanks for all the help!

Badge

Good to hear. 
The details for different situation has been explained in the readme file

To summary

  • if you are using default instance, you can access the deviceId by amplitudeGTM.getDeviceId()
  • if you are using customized deviceId, you need to access the deviceId by  amplitudeGTM._iq["instance-name"].getDeviceId() .

Also, we are working on improving our GTM doc, we will also include the cross-domain section there and improve this script. 

Thanks.

Badge +2

Thanks @Alyssa Yu! And yes, I did realise from the readme file how to get access to the customised instance. And that worked really well in code! However, the script did not work when I ran it through GTM, it kept timing out just like before I modified the code. So we eventually stopped trying to fix the script in GTM and just put the deviceId on the querystring “manually” instead...

Reply