beyonk-group / svelte-google-analytics

Google Analytics component for Svelte
77 stars 12 forks source link

Don't work me #4

Closed alextomas80 closed 3 years ago

alextomas80 commented 3 years ago

Hi!

I have followed the steps in the example, but it doesn't work for me. I don't see the page views in google analytics.

image

On the other hand, the events do work for me as in the example.

image

I hope you can help me.

oskarhane commented 3 years ago

You might need to send the page views manually on every transition.

In Sapper, this is what I do in _layout.svelte

<script>
import { stores } from "@sapper/app";
import { onMount } from "svelte";
import { GoogleAnalytics, ga } from "@beyonk/svelte-google-analytics";

const { page } = stores();

onMount(() => {
        return page.subscribe(({ path }) => {
            ga.addEvent("page_view", { page_path: path });
        });
    });
</script>

<GoogleAnalytics properties={['UA-XXXX-X', {send_page_view: false}]} />

Notice the {send_page_view: false}, which I don't think works because we always get two page view on the initial page load.

Is there a way to define configuration options @antony?

EDIT

After writing this, I updated the code we use so it now looks like this. Would still want to be able to pass in config options.

<script>
import { stores } from "@sapper/app";
import { onMount } from "svelte";
import { GoogleAnalytics, ga } from "@beyonk/svelte-google-analytics";

const { page } = stores();
let initialLoad = true;

onMount(() => {
        return page.subscribe(({ path }) => {
            if (!initialLoad) {
                ga.addEvent("page_view", { page_path: path });
            } else {
                initialLoad = false;
            }
        });
    });
</script>

<GoogleAnalytics properties={['UA-XXXX-X']} />

Very much a hack, but currently works with Sapper.

antony commented 3 years ago

@alextomas80 I've not tried it with Routify, but this library doesn't deal with recording page views, that's down to Google Analytics, which claims to do it by default. It might br worth using the GA debugging tools to find out what is/isn't being recorded.

@oskarhane properties is for google analytics property ids - I've not tested it by passing in config options, but this would probably work.

I think in order to properly support configuration a PR would be ideal where we could pass the property configuration optionally as an object perhaps, with the property id as the key, and config being the value.

I'm not going to have time to do this myself, but happy to accept a PR.

antony commented 3 years ago

Fixed by v2.1.0