MatteoGabriele / vue-gtag

Global Site Tag plugin for Vue (gtag.js)
https://matteo-gabriele.gitbook.io/vue-gtag/
MIT License
861 stars 62 forks source link

How to set a dimension before the first router pageview? #334

Open potpot59 opened 3 years ago

potpot59 commented 3 years ago

Hello, First of all, a big thanks for this plugin which made it easier for us to set up Google Analytics in our Vue project.

I'm using vue-gtag 1.16.1 with vue 2.6.14.

I'm trying to set a dimension (the value is retrieved from the backend) for all queries made to Google Analytics. But I can't do: ["set", {dimension1: "MY_VALUE"}] before the first: ["event", "page_view", {page_title: "MY_TITLE", page_path: "/", page_location: "http://localhost:4200/", send_page_view: true}]

I do not perform the pageview manually, they are triggered by the router. The first pageview does not contain the dimension, the following ones do.

I have tried a lot of things without success, for example:

Here is the sample code from my last try:

Vue.use(VueGtag,
    {
      config: {id: 'UA-XXX'},
      bootstrap: true,
      enabled: false,
    },
    router,
);
   created(): void {
      userService.getCurrentUser().then((user) => {

        this.$gtag.set({
          dimension1: user.role,
        });

        this.$gtag.optIn();
      });
    }

Output with the Google Analytics Debugger:

Processing GTAG command: ["event", "page_view", {page_title: "MY_TITLE", page_path: "/", page_location: "http://localhost:4200/", send_page_view: true}] Processing GTAG command: ["set", {dimension1: "MY_VALUE"}]

Thanks in advance for the help šŸ™šŸ»

MatteoGabriele commented 3 years ago

Hi @potpot59, I might have to add a callback function before the first configuration hit. I think that would fix your issue and you wouldn't need all this

potpot59 commented 3 years ago

That would be really great, would you offer this on version 1.x? Thank you.

MatteoGabriele commented 3 years ago

Yeah I'm currently supporting both at the moment. We'll see in the future

potpot59 commented 2 years ago

Any news?

rasmus0201 commented 2 years ago

Hello. I'm also interested in this issue being fixed.

Maybe we can help you somehow @MatteoGabriele? :)

Beatbug commented 2 years ago

A fix for this would be most helpful for a project I'm currently working on.

ebrake commented 2 years ago

+1