0

I am struggling to get Google Analytics to report correctly using the InertiaJS stack of Laravel Jetstream. I need to track individual page visits but since it's a SPA I'm unsure how to go about this. I have placed the tag in my app.blade but that shows me all traffic, not specific to each page, nor can I trigger events.

What's the best way to go about getting GA onto a Laravel Jetstream Inertia stack?

Tim Rowley
  • 420
  • 4
  • 15

1 Answers1

0

I opted in for using Inertia::share then on my app.blade I referenced the respective elements such as title etc as props.

Eg. RouteController (returns Inertia View):

Inertia::share([
            'title' => $title,
            'description' => $description,
            'meta_image' => $meta_image,
            'meta_url' => $meta_url
        ]);
return Inertia::render...

App.blade:

<title>{{$page['props']['title'] ?? ''}}</title>
<meta name="description" content="{{$page['props']['description'] ?? ''}}">
...

And finally in my AppLayout.vue I have a watch on these props for page changes:

watch: {
        "$page.props.title"(newTitle) {
            this.setPageData();
        },
        "$page.props.description"(newDescription) {
            this.setPageData();
        },
    },

Only really need to watch the Title as robots should load each page fresh (for SEO purposes) but the this works well for meta tags (Open Graph) and also GA to track each page.

Tim Rowley
  • 420
  • 4
  • 15