Forum Discussion

Will_Findlay's avatar
Will_Findlay
Community Member
3 months ago

Can I insert Google Analytics javascript in a trigger?

Will it work to put the code below in a trigger: "Execute JavaScript when the timeline starts on this slide?" I want to track using Google Analytics, but I don't want to edit the HTML launch file every time I publish a course.

<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-MYGCODE"></script>

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'MYGCODE');

 

  • Will_Findlay's avatar
    Will_Findlay
    3 months ago

    Can confirm that this works!

    1. Set up your Google Analytics property
    2. In Google Analytics go to Admin (gear icon) > Data collection and modification > Data streams
    3. Add a stream if needed
    4. Click on the stream and click "View tag instructions."
    5. Choose "Install manually."
    6. Copy the code it gives you and paste into a text editor
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-YOURGCODE"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'G-YOURGCODE');
    </script>

    7. Replace lines 2 and 3 with the actual javascript contents of the page at https://www.googletagmanager.com/gtag/js?id=G-YOURGCODE

    8. Copy all of that and paste into a Javascript Trigger.

     

  • Will_Findlay's avatar
    Will_Findlay
    Community Member

    I'm getting an error about that first line - I don't think you can refer to script library apparently.

  • Will_Findlay's avatar
    Will_Findlay
    Community Member

    So, I'm attempting a workaround: I'm going to  https://www.googletagmanager.com/gtag/js?id=G-MYGCODE (replacing MYGCODE with my actually Google Analytics code) and then copying and pasting everything into the trigger above the original script so it doesn't need that line 1 that is <script async="" src="https://www.googletagmanager.com/gtag/js?id=G-MYGCODE"></script>

    • Will_Findlay's avatar
      Will_Findlay
      Community Member

      Can confirm that this works!

      1. Set up your Google Analytics property
      2. In Google Analytics go to Admin (gear icon) > Data collection and modification > Data streams
      3. Add a stream if needed
      4. Click on the stream and click "View tag instructions."
      5. Choose "Install manually."
      6. Copy the code it gives you and paste into a text editor
      <!-- Google tag (gtag.js) -->
      <script async src="https://www.googletagmanager.com/gtag/js?id=G-YOURGCODE"></script>
      <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
      
        gtag('config', 'G-YOURGCODE');
      </script>

      7. Replace lines 2 and 3 with the actual javascript contents of the page at https://www.googletagmanager.com/gtag/js?id=G-YOURGCODE

      8. Copy all of that and paste into a Javascript Trigger.