Best way to include Google analytics code without plugins

Best way to include Google analytics code without plugins

  

Hi,

We have a mobile app that also can be run in the browser (so we are not relying on any Cordova plugins). What is the best way to include google analytics code in the page <head> ? 

Current code is like this:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=XXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'XXXXXXXXXX');
</script>

I can include js as a required script to the web block and add it to layout but how to include external tag script?


Thank you

Hello Mykola,


I've faced same issue, but for web only. I've created the extension HTTPRequestHandlerBuffed. I'm not sure if it helps in your case - since you're looking for a mobile approach.

Not sure if this helps, but going to share it anyway: load scripts asynchronously.


I would say for you to add a new Required Script with something similar to that link.


Let me know if this helps,

Armando

Solution

Thank you Armando,

I've managed to solve this by copying script source to script file and then use on ready event to fire tracking code with parameters like page url etc.

Solution