MatteoGabriele / vue-gtag

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

Misleading error when using pageTrackerScreenviewEnabled with pageTrackerTemplate #84

Closed benirose closed 4 years ago

benirose commented 4 years ago

I was wrestling with this error for a bit: To use the screenview, add the appName to the plugin options with this configuration:

    Vue.use(VueGtag, {
      config: {
        id: options.gTag,
        params: {
          language: i18n.locale
        }
      },
      pageTrackerScreenviewEnabled: true,
      appName: "Participant Portal",
      pageTrackerTemplate(to) {
        return {
          screen_name: to.name,
          user_id: store.getters.getParticipant ? store.getters.getParticipant.id : null,
        }
      }
    }, router);

After digging into the source code it became apparent, that the top level option of appView only gets set in the template if you're not using a custom one, otherwise you need to set app_view within your pageTrackerTemplate method. Would it make sense to report a different error if they have a custom pageTrackerTemplate that's missing app_view?

benirose commented 4 years ago

If you agree with the idea of more descriptive errors when using a customTemplate, I can submit a PR with that change.

MatteoGabriele commented 4 years ago

the error is correct, the thing is that by having a custom template like this you are making the error yourself by not passing the app_name. this is how I create the template

let template;
  const customTemplate = pageTrackerTemplate(to, from);

  if (customTemplate) {
    template = customTemplate;
  } else if (pageTrackerScreenviewEnabled) {
    template = {
      app_name: appName,
      screen_name: to.name
    };
  } else {
    template = {
      page_title: to.name,
      page_path: to.path,
      page_location: window.location.href
    };
  }

so when you create the custom one, u are taking care of everything basically

sorry for the huge delay of the response 🙏