Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robgoodliffe's avatar

adding gtag to inertia / vue / ssr

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

0 likes
6 replies
robgoodliffe's avatar

@martinbean thanks. I was aware of that, but unsure about the best way to set it up.

for example, do I need to push gtag('js', new Date()); gtag('config', 'G-xxxxxxx'); on every inertia page visit?

martinbean's avatar

@robgoodliffe The guide tells you what you need to do:

Where appropriate, make the following gtag call, replacing placeholder values as necessary

So you need to fire that before navigating to a new page:

router.on('navigate', (event) => {
    gtag('event', 'page_view', {
      page_location: event.detail.page.url,
      page_title: '[REPLACE WITH PAGE TITLE]',
    });
});
robgoodliffe's avatar

@martinbean yes, but I'm specifically interested in setup. I understand that events can be triggered on inertia events, but i wasn't sure if I had to call the following on each inertia page visit. gtag('js', new Date()); gtag('config', 'G-xxxxxxx') or i can put them both in the head in blade

martinbean's avatar

@robgoodliffe No. Again, the docs explain.

Put your snippet in the head of your Blade layout like you normally would. But you disable the regular pageview measurement:

<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', {
        send_page_view: false,
    });
</script>

And instead log a page view when Inertia navigates to a page like I showed you in the previous post:

router.on('navigate', (event) => {
    gtag('event', 'page_view', {
      page_location: event.detail.page.url,
      page_title: '[REPLACE WITH PAGE TITLE]',
    });
});
robgoodliffe's avatar

@martinbean thanks for your advice. Yes, I've read the docs, but also seen some other guides that approach it differently: https://postsrc.com/code-snippets/how-to-integrate-google-analytics-with-inertia-js

the docs also suggest that it may be still possible to "sends a pageview when the browser history state changes" which is why i wanted to check what people's approaches were.

I'm just going to run it in production and see that it's tracking as it should.

Thanks for your help.

Please or to participate in this conversation.