angulartics / angulartics-google-analytics

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

Invoking ga 'send' for social tracking purposes #111

Closed mikesnare closed 6 years ago

mikesnare commented 6 years ago

I'm having some trouble getting social tracking working in some scenarios. I know that social tracking is not something you support, but something seems off and it seems related to angulartics/angulartics-google-analytics.

Issue https://github.com/angulartics/angulartics-google-analytics/issues/94 makes it clear that this plugin will not support social tracking since it's not supported in angulartics itself. Makes sense. Not questioning that.

My understanding, though, is that angulartics-google-analytics is a wrapper around the standard functionality in ga such that I should be able to use window.ga as a fallback when necessary. So, I'm falling to back to using window.ga('send', 'social',... in order to do social tracking, but it's not working anywhere except in my localhost testing environment. Doesn't work in any other environment and I can't figure out why.

Issue https://github.com/angulartics/angulartics-google-analytics/issues/16 suggests that the 'send' message is somehow removed from ga which would seem to indicate that it can't be used. That would explain what I'm seeing in staging environments (where it doesn't work) but not locally (where it does work).

In the environments where it does not work, I've tried using the console to run ga('create', ... to see what happens. Following that, I can manually send the event using ga('send', 'social'... but now every event I send is duplicated.

mikesnare commented 6 years ago

A bit more info, the following snippet in the console dumps a correctly configured ga instance when this is run locally:

window.ga(console.dir)

but yields nothing when running outside of development. However, ga events triggered using the $analytics service still work correctly.

timelf123 commented 6 years ago

We only remove the send pageview call to let angulartics handle the first pageview track, not the actual function. Sounds like something in prod isn't firing correctly - have you made sure to whitelist the site in your ad blocker? I'd like to see a plunk/jsfiddle of this to debug, seems like a local issue and I won't be able to do much without a full example

mikesnare commented 6 years ago

I have whitelisted it. It's not that. If it were that, then $analytics.eventTrack wouldn't work, either.

This is a situation where $analytics.eventTrack works correctly but window.ga('send', ... does not, which makes absolutely no sense since (as I read the angulartics-google-analytics code) this plugin just delegates directly to window.ga in applyUniversalCall_.

However, I have found a workaround to anyone that needs this:

$window.ga.getAll(trackers => trackers.forEach(tracker => tracker.send(...)))

works correctly.

Seems like something is mucking with the command queue, but I can't quite figure it out. If I do I'll update this, but I'm ok for now. Thanks for the response.

DanWilkerson commented 6 years ago

@mikesnare are you using GTM? GA "names" trackers; the default one is named t0, and they've added some sugar so you can call t0 simply omitting the name, e.g. the standard ga('send', 'pageview') call. This translates under the hood to ga('t0.send', 'pageview');.

Try using the Google Analytics Debugger plugin and look for this:

Initializing Google Analytics.
analytics_debug.js:10 Registered new plugin: ga(provide, "render", Function)
analytics_debug.js:10 Executing Google Analytics commands.
analytics_debug.js:10 Registered new plugin: ga(provide, "GTM-TLRPHD", Function)
analytics_debug.js:10 Running command: ga("require", "GTM-TLRPHD")
analytics_debug.js:10 Plugin "GTM-TLRPHD" intialized on tracker "t0".
analytics_debug.js:10 Running command: ga("create", "UA-296882-47", {name: "gtm1", cookieDomain: "auto"})
                                                                     ^^^^^^^^^^^^
analytics_debug.js:10 Creating new tracker: gtm1
analytics_debug.js:10 Auto cookieDomain found: "lunametrics.com"
analytics_debug.js:10 Running command: ga("gtm1.set", "&gtm", "Gbe9H9Q")
analytics_debug.js:10 Running command: ga("gtm1.set", "hitCallback", [function])
analytics_debug.js:10 Running command: ga("gtm1.send", "pageview")
                                         ^^^^^^^^^^^

If the output looks like the above, a named tracker is in play. If it's labeled "GTM", you've got Google Tag Manager and may be better off usign that. You may want to check that you're sending data to the correct UA number, which you can do by calling tracker.get('trackingId') === MY_UA_NUMBER (here's a handy blog with a snippet that does that).

As an aside, the social hits feature seems to be on the way to deprecation, by my read. They've nixed a lot of the reports and metrics as of last year. I'd suggest using Events, which can capture the same data and are able to be used in Segments and as Goals, which social hits are not.

mikesnare commented 6 years ago

@DanWilkerson we are not using GTM, so that wouldn't be the issue. My tracker names are 't0' (default) and one other that we've set up. Using the debugger I do not see any references to gtm.

Again, though, that would only make sense (to me, at least) if it were always happening. This only happens when I'm not running locally.

Here's what I get trying to manually run this in the console with the debugger installed (thanks for that link, btw...):

image

I'd also seen your comment about not using the social tracking, but that's the only reference online I've found to any of this being sunsetted. I'm happy to make that case here at my org, but without something to back it up it might a hard sell. Got any links to support that?

DanWilkerson commented 6 years ago

It's not GTM specific - that error actually IS the error that you get when you don't prepend the name to the command. Try running this:

ga.getAll().map(t => t.get('name'))

And see what comes out.

Per the social reports, here's the changelog for the API where they document deprecating/removing many of the metrics and dimensions associated with that hit type:

https://developers.google.com/analytics/devguides/reporting/core/v3/changelog

and here's a blog post mentioning the notice in the UI (couldn't find an official announcement):

https://www.analyticsedge.com/2016/04/google-analytics-deprecated-social-data-hub-metrics/

They haven't made an official announcement, but the feature has been stagnant for years. Combined that with the deprecation of those reports and metrics/dimensions, seems to me they won't be around for much longer.

mikesnare commented 6 years ago

Thanks for the response. Yes, I was able to work around the issue using ga.getAll and I posted that info earlier. Still, calling ga('send', 'event', ...) does not work but $analytics.eventTrack(...) does. That's the crux of the issue and it just doesn't make any sense to me. Your snippet regarding pulling names works as expected, but (again) I already knew that using ga.getAll was working so that's not surprising.

It may be that I have a faulty understanding of how GA works... To your comment:

that error actually IS the error that you get when you don't prepend the name to the command.

Are you saying that it's required to prefix the name for all commands? That seems odd. If that's true then that would explain it, but that doesn't seem to be what this plugin is doing?

DanWilkerson commented 6 years ago

What did the output for that command look like? If you share a page I can take a look, too. I'm a bit puzzled myself at the moment.

Per prepending: yup, you got it; the code has to prepend the name of the tracker with one exception. We actually do that here:

https://github.com/angulartics/angulartics-google-analytics/blob/master/lib/angulartics-ga.js#L483-L489

The exception is if the tracker is instantiated without a name, like in the default snippet:

ga('create', 'UA-99999999-99', 'auto');
ga('send', 'pageview');

screen shot 2017-11-17 at 4 56 41 pm

As you can see, you don't need to prepend 't0', even though it works and is technically what the tracker thinks it's named. This is the aforementioned sugar. Personally, I think this was a bad design decision, but what do I know.

If the tracker does have a name, calls w/o that name either get passed to the nameless tracker, which can cause a false positive read:

screen shot 2017-11-17 at 5 01 46 pm

Or if there isn't one, they fail (though silently):

screen shot 2017-11-17 at 4 58 27 pm

I don't make the stupid rules, I just learned them all. ¯_(ツ)_/¯

mikesnare commented 6 years ago

Well... shit. Surprising that my 2 days of experience with google analytics failed me here... :)

I have several places where I'm testing this and some have the default tracker and some do not. As you'd expect, window.ga(...) works in those places where I have a default tracker setup -- but only for the default one and not the non-default one. This is working as expected and the correct way is either to iterate (as I'm doing) or just call send once for each known tracker.

I knew that we had the t0 tracker in some cases, but clearly I wasn't testing in those places where we had it otherwise I'd have seen it. As to localhost vs external, the difference was that the model I was working with locally DOES have the default, but the one in staging does not. So in local it was only half working -- the default tracker was sending an event but the other was not.

Thanks for sticking with me and schooling me. I'll close this now since it's clear why this works the way it does.

DanWilkerson commented 6 years ago

Word! Glad you got it solved 🎉