Google analytics to laravel page

im trying to add google analytics to my page. I have created analytics.blade.php with code like this:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'Analytics_ID', 'auto');
  ga('send', 'pageview');

</script>

and added @include (‘analytics’) to app.blade.php

But i cant get any info in google analytics of that page. Maybe i have something more to do, or what?

Answer

Analytics snippet like G-XXXXXXXX is for Google Analytics 4 while your code is for Google Analytics Universal. So you have to use this snippet in this case (gtag library instead of ga):

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *