Forum Discussion
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');
Can confirm that this works!
- Set up your Google Analytics property
- In Google Analytics go to Admin (gear icon) > Data collection and modification > Data streams
- Add a stream if needed
- Click on the stream and click "View tag instructions."
- Choose "Install manually."
- 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_FindlayCommunity Member
I'm getting an error about that first line - I don't think you can refer to script library apparently.
- Will_FindlayCommunity 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_FindlayCommunity Member
Can confirm that this works!
- Set up your Google Analytics property
- In Google Analytics go to Admin (gear icon) > Data collection and modification > Data streams
- Add a stream if needed
- Click on the stream and click "View tag instructions."
- Choose "Install manually."
- 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.