Custom dimension not being set - google-analytics

I've got myself a bit of a head scratcher here, for me anyway, as I'm a bit new to all this.
I have WP a site that has its GA snippet inserted with Google Site Kit.
There is a plugin that, when a user registers, sets the users ID to a custom dimension.
The code to execute this 'set' has been added to the WP footer with the add_action('wp_footer') command.
The code looks like this:
<script type=\"text/javascript\" id=\"set-google-id-dimension\">
document.addEventListener(\"readystatechange\", event => {
if (event.target.readyState === \"complete\") {
try{
gtag('event', 'registration', {'dimension1': '".$client."'});
}catch(e){
try{
ga(function() {
// Logs an array of all tracker objects
var trackers = ga.getAll();
var firstTracker = trackers[0];
console.log('tracker: '+firstTracker.get('name'));
ga(firstTracker.get('name')+\".set\",\"dimension1\" ,\"".$client."\");
});
}catch(e){
console.log('GA and GTAG not defined');
}
}
}
});
</script>
For ease of reading, I've stripped out the PHP, but this is being echoed out in the footer.
Why the GTAG and GA command? Both analytics are being output in the console, though the site owner does not know why as "they only use Google Site Kit".
Now, this code works on the dev site that I control (and I've set it up to mimic the live site as best as possible):
However, when on the "live" site, the dimension is never set, even though I can see the 'set' command being executed (ignore the timestamps in the console, I forgot to screenshot before navigating away from the site and had to go back and reload the page):
The live site does not use the default tracker, hence the ga.getAll() call to access the tracker information.
From what I can see, everything should work fine.
I understand that from reviewing this question
Google Analytics Custom Dimension Not Being Set
that the 'set' needs to come before the 'send'.
I'm not sure how to accomplish this though since the plugin does not send the pageview to GA, from what I understand, that's Google Site Kit. I have contemplated adding a 2nd pageview send when this plugins code is loaded (it is only executed immediately after a registration and never again), but that would skew the page hits.
This site has had a myriad of "admins" over the years, so I wouldn't be surprised if there was something buried in one of the plugins causing a conflict somewhere. At one point I thought it was a timing issue, so I had the function load every 50 milliseconds checking for 'ga' to be defined, then execute the 'set' command (with a limit to 35 iterations), but the issue was the same (could set the command execute in the console, but the dimension did not reflect the value).
Any advice I can get to debug and get to the root cause would be of great assistance to me. Please ask any questions you need and I will respond as quickly as possible.

It seems a bit complex as a situation so understanding how it works and why there is gtag and ga at the same time is not easy to understand.
In any case, assuming that everything is working, what you can do is not to send a second pageview but send a dedicated event (by setting non-interaction to true) in this way you do not alter any information in Google Analytics and you can pass to the platform the data you are interested in (dimension1).

Related

Vue Router routes not tracked by Firebase Analytics

I'd like to know if there's some configuration I might've missed when setting up Firebase Analytics for a Vue app (and for SPAs in general, I suppose) to get it to track page changes, because when I'm in Firebase's DebugView, it doesn't track page_views unless I refresh the page or manually enter a URL into the browser. I know for the regular Google Analytics module (vue-analytics) you have to pass it the router object on initialisation for it to work (more about that) so it makes sense to me that you'd have to do something similar with Firebase Analytics, but I can't find anything in the docs about it.
I've tried manually calling logEvent('page_view') from my router.afterEach() guard, but then it logs each page change multiple times.
Please let me know if there's any info or demo code you'd like me to include.
So this doesn't fix the problem, per se, but it's what I'm going with until a better answer comes along.
Moving the logEvent('page_view') from the router.afterEach() guard to the router.beforeEach() guard prevents it from being logged multiple times in Firebase Analytics, and adding a custom type parameter allows me to distinguish between an actual page change/load and an internal "SPA" page change/load. So (the relevant part of) my router code now looks like this:
router.beforeEach(async (to, from, next) => {
firebase.analytics().logEvent('page_view', { type: 'internal' });
});

Google Analytics - Overriding Default Values

I am trying to confirm a 'pageview' is being processed properly.
( using google real time dashboard ).
The issue is simple, If I use this call:
// page uri is: www.example.com/signup
ga( 'send', 'pageview' );
I see a matching entry at google's dashboard.
Attempting to override the title / page parameters ( according to the docs ) like so:
ga( 'send', 'pageview', {
'page': '/admin/logout',
'title': "test"
} );
Simply produces nothing over at google's side.
Any idea what is going on?
EDIT
It appears that google analytics service is filtering pages that contain
the word "admin".
Removing that from the 'page' helped in my case.
I don't know what's going on because your syntax is definitely correct.
FWIW, while the real time dashboard is a great way to see your hits come in, it should not be considered proof or evidence that something is or isn't working. Everything you send to Google Analytics goes through processing after it comes in, and (for a variety of reasons) a hit may be dropped from reports after showing up successfully in the real time view.
There's also always the possibility that there's a bug in the real time dashboard, and a particular hit may not show up even though it will ultimately make it into your Google Analytics reports.
A much more reliable way to see if your code is working is to take a look at the data that is actually being sent from your page. You can do this via the network inspector (in Chrome) or by using the debug version of analytics.js, which logs status and error message to the console.

Not receiving Fragment in Google Analytics

I have a website like this:
https://example.com/path1/path2/#!portal/1
I've inserted the script of ga and I've made the next modification in the code to receive the fragment (after hash #):
ga('send', 'pageview', {
'page': location.pathname + location.search + location.hash
});
But when I check the ga page, in "Behavior/Behavior Flow/Site Content/All Pages" I only have:
https://example.com/path1/path2/
I'm new in ga, am I doing something wrong?
The code you have in your question will work just fine. I think there may have been a temporary glitch affecting hash URLs, but everything seems to be working now, so I'd just try it again.
Also, since it sounds like you're building an AJAX site, I'd recommend taking a look at the developer guide on tracking single page applications with Google Analytics:
https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications
I had the same problem - overriding the page value didn't do anything, despite the fact that this is explicitly mentioned in the documentation.
I finally replaced the hash with another character (two actually I used a double slash //) in the tracking code and used a search-replace filter in the view settings to restore the hash character.
If anybody has a proper solution I'll take it in a jiffy, but this is at least a workaround that does the job.

Ember.js and Google Analytics

I wanted to get a definitive answer on here for later reference now that we have a stable Ember RC. A combination of the top 2 search results for emberjs google analytics reveals that this is a good way to do track route changes:
App.ApplicationController = Ember.Controller.extend
routeChanged: ( ->
return unless window._gaq
Em.run.next ->
page = if window.location.hash.length > 0 then window.location.hash.substring(1) else window.location.pathname
_gaq.push(['_trackPage', page])
).observes('currentPath')
but then I also see results for using Event Tracking for single page web applications.
I haven't tested the code above yet, it takes a few hours to propagate changes to the GA dashboard. Update: This doesn't show up under the Content category on my Google Analytics dashboard. Neither under "Pages" or "Events".
If anyone has advice or if there's something I'm missing somewhere let me know. I can also PR a guide for the website based on the answers here.
Alex DiLiberto gave a really nice talk about a robust & scalable way of adding Google Analytics to an ember app in his EmberConf 2014 talk here.
- Slides
- GitHub
App.ApplicationRoute = Ember.Route.extend({
actions: {
didTransition: function() {
Ember.run.once(this, function() {
ga('send', 'pageview', this.router.get('url'));
});
}
}
});
The talk was aiming to be independent of which analytics library was used.
There is also now an official Ember Cookbook on implementing Google Analytics here.
I would use _trackPageview for things that have routable URLs and _trackEvent for things that don't.
In the Event Tracking link when they refer to "Embedded AJAX page elements". They're not meaning SPA's, but rather those cases when the URL stays the same, but some event that you wish to track happens within the page (in the case via AJAX).
There may be other cases where it makes sense to use _trackEvent, but for route transitions I'd use _trackPageview.
Using routeChanged() is not a good way to track dynamic segments such as /category/food /category/something since it's going to be fired only once. I wrote an article about this here: http://www.randomshouting.com/2013/05/04/Ember-and-Google-Analytics.html. I also consulted with the guys behind Ember and confirmed that this is indeed the proper way to track url changes for Google Analytics.
Most of these answers are outdated. You should be using a mixin and adding it to your Router to listen for the didTransition event and fire your pageview there. That way it's handled for all routes. There are several addons out there, including one I wrote called ember tracker which gives you pageviews and event tracking out-of-the-box.
You can see how I did it here. It's fairly straight forward.

Analytics receives events, but does not log them in reporting

I've been all over google and SO looking for answers here. I've checked code against google's tracking code suggestions and I've even tested with their sample code. But I cannot get the events to be properly tracked in Google Analytics. My code has been live on the site for some time (~ a month), so this should have overcome any lag on the reports there.
I'm using analytics, the async version, which works fine for page tracking and other 'normal' features. Trying to set up event tracking, I followed the example, and waited a week. Still nothing. I've been debugging and I'm not having any of the 'normal' problems (as far as I can see).
So:
Example tracking code fired:
Play
This triggers an event, which according to ga_debug.js, is fired successfully. Checking the network panel I can see the request and _umt.gif returns status 200.
I've checked the request string in there. All seems good.
But checking GA, there is nothing in the reports. But when you go to GA Realtime > Events, you can see the events being fired!?
I can trigger the events on page or fire them from the console and corresponding events show up in the realtime section, but they have no data (Category, Action or Label).
This is odd, that even firing Google's own example code nothing is showing up.
If anyone has any ideas of where to start debugging for this (or has experience of anything similar), it would be great to hear.
I have been struggling with this issue for three days. I know this thread is old, but I stumbled upon it during my searching. I wanted to post what my personal resolution was in case someone else comes across this.
My Solution:
Here is where I finally found it:
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables
Under: "Using Custom Variables" > "Traditional (ga.js) Snippet" (We are using the ga.js still... for now.)
I had a similar problem and ran into this issue:
https://issuetracker.google.com/issues/35353093
In summary, I was setting the userId property via below. Removing that started historical tracking for me.
ga('set', 'userId', username);
This question gets asked a lot and the answer is almost always "Google Analytics standard SLA is 24 hours". The logs are processed in batch behind the scenes. At peak times, this takes a while.
It can take up to 24 hours for the Google Analytics servers to update -- Source: Google Documentation - Check your web tracking code setup

Resources