ScreamZ / vue-analytics

Google Universal Analytics support in Vue.js
Apache License 2.0
216 stars 27 forks source link

Pass router parameters? #17

Closed goobdeveloper closed 5 years ago

goobdeveloper commented 7 years ago

Hi. Great plugin, thanks for making it. Installation was very easy.

My current issue, though, is that, if I just use the default router integration which passes the route name to GA, none of the route parameters are sent. e.g. visiting /#/users/1 only sends /users to GA, instead of /users/1. Is there a way to make it pass the params, too, other than manually calling this.$ua.trackView(constructedScreenName) myself?

goobdeveloper commented 7 years ago

It seems this can be accomplished by changing this line from

Vue.analytics.trackView(to.meta.analytics || to.name)

to

Vue.analytics.trackView(to.meta.analytics || to.path)

Obviously, I suppose some people would want to.name still, so maybe you could make this some kind of config option? Anyway, I am using a custom router.afterEach setup to accomplish what I want in the mean time.

ScreamZ commented 7 years ago

Hey! Thanks for the feedback, highly appreciated ;)

I agree with the idea, I'll make something to take that it parameter. I ping you when it's all right.

Best regards

ScreamZ commented 7 years ago

@goobdeveloper

Hey buddy just made a fix, can you try this on your project and tell me if it's okay ? Don't have a testing project under the hand at the moment ? Thanks


Add a meta attribute usePathAnalytics on the route which take a boolean


  vueRouter.afterEach(to => {
    // Ignore some routes
    if (ignoredViews && ignoredViews.indexOf(to.name.toLowerCase()) !== -1) {
      return
    }

    // If specified : the system will use the path instead of the route name.
    const viewName = to.meta.usePathAnalytics ? to.path : to.name

    // Dispatch vue event using meta analytics value if defined otherwise fallback to route name/path.
    Vue.analytics.trackView(to.meta.analytics || viewName)
  })
`
ScreamZ commented 7 years ago

@goobdeveloper Poke, you agree with that ?

goobdeveloper commented 7 years ago

@ScreamZ I haven't tried it, but just looking at it, it seems okay to me, so I think you should merge it in. I will update versions and try it that way.

cspeer commented 5 years ago

Hey @ScreamZ can we get this merged please? :)

ScreamZ commented 5 years ago

@cspeer released under 1.7.0

cspeer commented 5 years ago

Great, thanks! Quick question though: Does this work as a global setting or do I have to pass this along with every route?

ScreamZ commented 5 years ago

As you can see this is passed at every routes for now.

You can submit a PR to this, but I think it's better if you disable router integration and implement the above snippet on your own if you need such functionnality

cspeer commented 5 years ago

yeah, I implemented it on a per route basis now. Thanks again!