Git Product home page Git Product logo

gtm-web-demo's Introduction

Demo App - Amplitude JS SDK with Google Tag Manager

GTM integration on web requires installing our Javascript SDK, and then setting up GTM to trigger SDK functions using custom HTML tags. This demo apps is a proof of concept, with just the 3 most important functionalities implemented (logging events, setting user Id, and setting user properties). Here is our GTM container setup:

Note: Make sure to put the Amplitude JS load snippet and initialize with your API key on every page before you load the Google Tag Manager script.

User-Defined Variables (all Data Layer Variables):

  • eventProperties
  • eventType
  • userId
  • userProperties

Triggers:

  • logEvent: Event equals 'logEvent'
  • setUserId: Event equals 'setUserId'
  • setUserProperties: Event equals 'setUserProperties'

Tags:

logEvent

  • type: Custom HTML Tag
  • HTML: <script>amplitude.getInstance().logEvent('{{eventType}}', {{eventProperties}});</script>
  • Fire on trigger: Event equals 'logEvent'

setUserId

  • type: Custom HTML Tag
  • HTML: <script>amplitude.getInstance().setUserId('{{userId}}');</script>
  • Fire on trigger: Event equals 'setUserId'

setUserProperties

  • type: Custom HTML Tag
  • HTML: <script>amplitude.getInstance().setUserProperties({{userProperties}});</script>
  • Fire on trigger: Event equals 'setUserProperties'

The idea is that when you want to log an event, you push a "logEvent" event to the data layer with the variables for eventType and eventProperties -> activates the logEvent trigger -> activates the logEvent tag -> calls our SDK's logEvent using the script tags.

To run this locally

  1. Run npm install
  2. Run node server.js
  3. In browser, navigate to http://localhost:9000/amplitudejs-gtm.html

Note

A lot of the other functionality from the SDK hasn't been implemented (such as logging revenue, user property operations, etc), but this proof of concept demonstrates that GTM integration with Amplitude is possible, and events are being logged with the correct user Ids. Implementing the other functionalities would follow a similar pattern. You can customize your integration based on your setup, for example you can have tags triggered by clicks or page views, but the important thing is that you map it to the correct SDK function in the custom HTML.

gtm-web-demo's People

Contributors

djih avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.