Lifetime access is $100 off. Ends this week.Join here →
Inertia

Setting up gtag with laravel inertia vue

Does anyone have any experience setting up gtag in inertia / vue.

so in non spa application you can just set it up like this

<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-xxxxxxx');  
</script>

but i'm presuming i'll need to so something like this

<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxxxxxxx"></script>  
<script>  
  window.dataLayer = window.dataLayer || [];  
    function gtag(){dataLayer.push(arguments);}    
</script>
// not sure where to put this app.js?

router.on('navigate', (event) => {  
      gtag('js', new Date());  
	  gtag('config', 'G-xxxxxxx');
})

would the router events have access to window on the inital ssr page load?

any help appreciated

robert29275
robert29275
0
1
494
alex
alex
Moderator

I don't have much experience with Google tag, but possibly look at using a package that handles everything for you?

There's one just here for Vue 3