angulartics / angulartics-google-analytics

Google Analytics plugin for Angulartics
MIT License
133 stars 86 forks source link

Help needed: Automatic pageview tracking does not track anchor URI per default #102

Closed kwinz closed 7 years ago

kwinz commented 7 years ago

Dear all,

thanks to angulartics-google-analytics I have to declarative event tracking working well, and also the automatic pageviews trigger nice pageview entries in Google Analytics. However what drives me mad is that the anchor part of the URI is not visible in the Analytics Dashboard.

angulartics-google-analytics creates requests like this:

https://www.google-analytics.com/collect?
v=1&_v=j51&a=333486222&t=screenview&_s=7&
cd=%2Findex.html%23%2Fsign-in&
dl=http%3A%2F%2Flocalhost%2Findex.html&
ul=de&de=UTF-8&
dt=Treat&
sd=24-bit&sr=1918x941&
vp=1918x845&je=0&
fl=25.0%20r0&an=Treats&_u=SACAAMABO~&jid=&
gjid=&cid=1883662174.1492999789&
uid=Mark-user&tid=UA-9675XXXX-1&z=124537179

Notice how the cd=%2Findex.html%23%2Fsign-in includes #sign-in part of the URI, but the important dl=http%3A%2F%2Flocalhost%2Findex.html does not. (documentation)

My app.js looks like this:

... }).config(function ($analyticsProvider) {
$analyticsProvider.firstPageview(true); /*Records pages that don't use $state or $route*/
$analyticsProvider.withAutoBase(true);  /*Records full path*/
$analyticsProvider.virtualPageviews(true);
});
;

How can I track the full URI including the anchor for pageviews?

Cross-posting to stackoverflow: http://stackoverflow.com/questions/43590466/angulartics-google-analytics-track-full-uri-including-anchor

DanWilkerson commented 7 years ago

Sorry, I believe you may be using a different plugin - the example hit you shared is a "screenview" hit type, which is for tracking mobile apps and is not supported by this plugin.

The field you need is the "page" field to set. That overrides the page URI found in the dl parameter.

kwinz commented 7 years ago

Thanks @DanWilkerson! Where does it state that "screen-view' hit type is not supported by this plugin on mobile apps? I think this needs to be documented much more prominently. It was pretty surprising behavior to me.

I would expect that the dl field contains the URI and the dt field to contain the title as per the Google Analytics documentation. [1]

Furthermore why is it not supported? I was able to work around the issue pretty quickly after I understood the problem with the code posted an stackoverflow. Is there any reason besides "did not have time to implement" to not support this?

[1] https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#location

DanWilkerson commented 7 years ago

How are you dispatching the screenview - are you calling ga('send', 'screenview') directly? That's what I'm trying to figure out here.

It's not supported because that hit type is meant for native app tracking and this plugin is for web application tracking. Simple as that. It's worth pointing out that the two hit types do not play together - you can't view them both in the same View, for example. There's a bunch of other things that aren't great about screenviews. Not my favorite feature.

kwinz commented 7 years ago

Maybe it's a missunderstanding between us. I am not using a native app but an Angular / Cordova / Ionic app. To the tracking code this should just look like a webpage. I am not calling ga() directly, I am using automatic Angulartics pageviews.

I was now able to work around the arguably wrong usage of thedlparameter in this plugin or the Angulartics code by manually calling

var currentPath =  $location.path();
$analytics.pageTrack(currentPath);

in $stateChangeSuccess See http://stackoverflow.com/a/44001181/643011

Even though it works for me with this workaround I would still urge you to fix this. Or I missunderstand something completely.

DanWilkerson commented 7 years ago

I'm afraid that there is a misunderstanding.

For starters, there's no way the plugin could emit a screenview hit type. It simply isn't possible. You may be seeing that from the GA SDK on the native side of your hybrid app. Without seeing your code, I can't tell you how this hit is being dispatched, just that it is. The only way to dispatch a hit with t=screenview using analytics.js is by calling ga('send', 'screenview'). You can search the source code; we don't do that.

Furthermore, your comment about the dl parameter is not entirely correct. The DL parameter is used for much more than simply parsing the page path - it is also used to determine the Source and Medium of a session; campaign parameters (e.g. utm_source=...) are stored in that field, when present, and are used by Google Analytics to determine where a session came from. It's also where the hostname is parsed.

The p parameter is the parameter to use when you wish to change the Page that is displayed in the UI.

https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dp

By default, Google Analytics ignores fragments when it triggers a request. That you see the fragment in your dl parameter is actually odd - whatever code is triggering that screenview is most likely responsible.

[EDIT] For clarification, I'm referring to vanilla Google Analytics installed WITHOUT Angulartics. We correct this behavior.

An easy way to prove this is to just call $analytics.pageTrack without setting the Page. You should see your fragment in the hit that is generated, barring any other code getting in the way. I double-checked in my own sandbox to be sure.

TL;DR: Angulartics DOES pass the fragment as part of the page path using the correct parameter p.

DanWilkerson commented 7 years ago

Here's a plunkr demonstrating the above:

https://embed.plnkr.co/YldKruEbVJP1LqOjdZWa/

I used the same configurations that you shared. Sorry, I really believe this is being caused by other code in your application.

kwinz commented 7 years ago

Hi @DanWilkerson: thanks for the replies. This is in a consulting project that has finished. I will keep investigating but on my spare time so I will take longer to respond.

kwinz commented 5 years ago

I have since moved on and unfortunately I can not keep working on this. Thank you!