googleanalytics / autotrack

Automatic and enhanced Google Analytics tracking for common user interactions on the web.
Other
4.93k stars 563 forks source link

pageVisibilityTracker: Issue with Custom metric " Page Visible Time" #205

Open EW0JY opened 6 years ago

EW0JY commented 6 years ago

Hi!

I'm using the following setup of the plugin within my GA snippet:

ga('require', 'pageVisibilityTracker', {
  sendInitialPageview: true,
});
// The command below is no longer needed.
// ga('send', 'pageview');`

After reading the documentation many times over, I'm still unclear on the following:

Basically, I'd like to use the suggested formula within my GA reports:

{{Page Visible Time}} / {{Unique Pageviews}}

The reason I'm confused is because the documentation says the default values already send the elapsed time as the "eventValue".

But then I read this a bit further up in the doc:

...to track the time a page was visible is to create a custom metric called Page Visible Time that you set in your plugin configuration options

So basically, my question is:

Any help would be greatly appreciated!

Thank you! Flo

philipwalton commented 6 years ago

Does the above code suffice to send the visible page time, or is there additional code necessary?

Yes, it does. This is what I do on my website.

The reason I'm confused is because the documentation says the default values already send the elapsed time as the "eventValue".

Right, it's on the event value as a backup (since I know some people won't go through the trouble of creating custom metrics). But I'd definitely recommend making the custom metric, as it'll give you much more flexibility in reports (e.g. when just using the event value, if you have multiple distinct event types in the same report, those values will all get added together).

Do I only need to set up a custom metric named "Page Visible Time" within Google Analytics and then create a calculated metric with it? Or do I need to alter the above mentioned plugin code that I'm currently using to achieve that goal?

You set all that up in Google Analytics, you don't need to change the code you have above.

EW0JY commented 6 years ago

Thanks so much, Philip!

That helped a lot. There are only 2 questions left in my mind:

1) Just out of curiosity, if I didn't set up any custom metric in Analytics, where could I even see the page view time that gets sent on the event value?

2) I will follow your advice and set up the custom metric, though. I was just about to do that, with the following details:

Would that work like that?

I guess both questions come down to the fact that I believe the custom metric serves as some sort of mapping for the value received. That's why I'm unsure where it gets mapped to if I didn't set it up.

But mainly I'd really appreciate it if you could comment on whether that mentioned custom metric would work like that.

Thanks so much!

philipwalton commented 6 years ago

if I didn't set up any custom metric in Analytics, where could I even see the page view time that gets sent on the event value?

Go to "Behavior" > "Events" > "Top Events" and then click on "Page Visibility". There you should see both the total value and the average value.

Would that work like that?

I use time, here's my config:

screen shot 2017-10-17 at 11 36 26 am

I guess both questions come down to the fact that I believe the custom metric serves as some sort of mapping for the value received. That's why I'm unsure where it gets mapped to if I didn't set it up.

It's not really a mapping, it's just a user-defined metric. It might help to think of it like a traditional metric like pageviews, but one that you define and implement yourself (you could actually implement pageviews yourself with a custom metric, I describe a similar strategy for implementing page load tracking via custom metrics in this article).

You can read more about custom metrics here: https://support.google.com/analytics/answer/2709828?hl=en

EW0JY commented 6 years ago

Again, thanks so much!

I set up the custom metric "Page Visible Time" exactly llike you said. Checking the data within Analytics today, I can see under events that data is getting through, showing as "Page Visibility" categorie and the eventValues are also filled with data (which must be the total of all measured seconds).

Here's the issue: When I use that custom metric in a custom report, it doesn't show any data, it's all 0. What could be the reason for this?

philipwalton commented 6 years ago

Did you set the visibleMetricIndex option? (Sorry, it may have been misleading when I said above that you don't need to change your code).

It should look something like this:

ga('require', 'pageVisibilityTracker', {
  sendInitialPageview: true,
  visibleMetricIndex: X // Where 'X' is whatever the index is in GA
});

Setting this options tells the plugin to send the custom metric field metricX with the hit, and use the same value as eventValue.

You can read more about how custom dimensions and metrics work with analytics.js here: https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets

EW0JY commented 6 years ago

Alright, so the code looks like this now:

ga('require', 'pageVisibilityTracker', {
  sendInitialPageview: true,
  visibleMetricIndex: 1,
});

So from now on the custom report should work, i.e. the custom metric "Page Visible Time" should be "filled" with data?

What I don't get is this: as I said, even now (before I updated the code with the visibleMetricIndex) the "Page Visibility" category shows plenty "eventValue", so it seems that there was already some data exchange happening.

philipwalton commented 6 years ago

I think this idea of "data exchange" is what's causing your confusion. The eventValue and metric1 fields are two completely separate metrics. The plugin is just setting them both to the same value.

Before you added visibleMetricIndex: 1, the data you were sending to Google Analytics looked something like this:

v=1&t=event&tid=UA-XXXX-Y&cid=uuid&ec=Page%20Visibility&ev=123

And after adding visibleMetricIndex: 1, it looks like this:

v=1&t=event&tid=UA-XXXX-Y&cid=uuid&ec=Page%20Visibility&ev=123&cm1=123

(notice the cm1=123 added to the end)

The reason eventValue showed up before is because the eventValue metric is a standard metric and its the same for every GA user. Custom metrics, on the other hand, are unique to each user, so autotrack can't send data for them until it knows the index.

EW0JY commented 6 years ago

ok, so if I understand you correctly, after setting the visibleMetricIndex: 1, I should be able to use the custom metric that I had already set up according to your suggestions? What I don't quite understand is how that can work without me telling Google Analytics which index (in this case "1") it should use to fill the custom metric. But that doesn't matter, as long as it works :)

philipwalton commented 6 years ago

What I don't quite understand is how that can work without me telling Google Analytics which index (in this case "1") it should use to fill the custom metric. But that doesn't matter, as long as it works :)

When you created the metric, Google Analytics automatically assigns it an index. As long as you're using that index, it should work.

screen shot 2017-10-18 at 3 14 16 pm

EW0JY commented 6 years ago

Alright, that makes a lot of sense now, thanks for pointing that our :) !

Btw: does your plugin fix the issue regarding measuring the time if a user only ever visits one page and then leaves? With the standard GA code this results in 0 seconds duration on every exit page.

philipwalton commented 6 years ago

Does your plugin fix the issue regarding measuring the time if a user only ever visits one page and then leaves? With the standard GA code this results in 0 seconds duration on every exit page.

Yep, that was the primary motivation for making the plugin :)

Note that it won't log any time if the user visit one page, leaves the page visible, and then the session times out, but that's much less common. If the user closes the tab or switches tabs, time will be logged.