Step 1: Reference your Google Analytics link in Layout Page
The first step you need to reference your Google Analytics code and it's as follows. Do note that the location of this file is "views/app.blade.php". The head and footer part and other styling are committed for the code readability.
<body class="font-sans antialiased bg-slate-50 dark:bg-slate-800"> @inertia @production <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} </script> @endproduction </body>
Step 2: Setup Inertia JS Google Analytics Binding
The next step is to define the analytic code on the app.js file. This code will be called & executed whenever we navigate to different pages.
/** * Track Page and Send to Google Analytic * */ if (process.env.NODE_ENV === 'production') { Inertia.on('navigate', (event) => { gtag('js', new Date()); gtag('config', 'G-XXXX'); }) }
Leave a reply