It is unlikely that you have a hard-coded Amplitude implementation in your websites alongside a second one done with Google Tag Manager and in need of a more advanced solution providing flexibility, scalability and control BUT if that's an scenario you are facing, here you have a way to solve it all in once. Check the code of this page to see it in action. Feel free to copy and tweak as needed.
- Ensures the SDK is really initiated before anything else
- It uses an Instance Name the GTM analytics browser wrapper so you will see the use of amplitudeGTM._iq[instanceName] to isolate one implementation from the other and avoid polution
- Enables a tight control of order in the basic events like $identify and page view
- Sends to the dataLayer information about Amplitude session, device and user IDs which makesit available to other tracking tools or services you may have in your website (allows you to merge data from several tools using those IDs as keys)
- Faciliteates cross-domain tracking implementation
- Makes easier the integration of other Amplitude tools like Session Replay or Guides & Surveys
The code, as indicated in the HTML comments, can be added into GTM HTML container with the trigger that works in your scenario and the GTM variables to replace the hard coded ones there.
This is how the dataLayer push would look like:
window.dataLayer.push({ event: 'amplitudeSDK', amplitudeSKDstatus: 'initiated', amplitudeData: { sessionID: 1747403510592, deviceID: fdf7f068-f6f6-44d2-b876-cac7f90eb108, userID: jI1APYfbocHVc2s } });