2

I am working on js file, within which I want to call google analytics methods. I only have access to the js file(s) and not html pages. I can create a new js file if need be.

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-XXXXXXXX-X');
</script>

is there a way I can do the above script tag equivalent within a js file, or do in another js file and import it?

user3861559
  • 973
  • 8
  • 25
  • 43
  • There are many ways you can do this. Take look at this: https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – Matheus Dal'Pizzol Jan 22 '19 at 23:18
  • Nitpicking, but even while it literallly says "googletagmanager.com" this is not GTM, but the Global Site Tag. "Dynamic script loading" from the answer in Matheus' link willl do the trick nicely. – Eike Pierstorff Jan 23 '19 at 23:45

0 Answers0