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.
Related
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).
General setting
I have a website, which uses regular and encrypted urls. Now I want to track the pageviews or all pages the same way.
I have regular URLs like this:
/library.dll?page=page12&arg1=0&arg2=some&session_id=7892734
and special pages like this:
/library.dll?page=specialpage&arg1=0&arg2=some&session_id=7892734&id=page13
aswell as encrypted URLs like this, which are also containing the session id:
/library.dll?page=encrypted&args=gYZEI7lnRAQLzVXdtdbcral8.cOoc6NDtMUGY2yep9wO3JM
So the interesting niformation is always the page, which is in this examples page12, page13 and page14 (where page14 is also part of the encrypted string).
Clarification
I can change the HTML and JS code only. I have no access to the Google Analytics interfaces at all. This will be administrated by multiple customers.
The GA code will be integrated within a template using a customer-specific code and their unique tracking id.
Problem description
I need to track the page argument, because this is basically the interesting part of the url. When tracking other url parameters I cannot accumulate the pageviews for a certain page, because Google Analytics shows them as separate pages.
In addition I don't see any way to track the pageviews with encrypted URLs, because I cannot set a generic name for them unless there is a way to utilize the method ga('send', 'pageview');
Solution idea
I read about overwriting the pageview attributes like this:
ga('send', 'pageview', '/my-overridden-page?id=1');
in the article on page tracking #Google Developers
Utilizing (event) triggers is in my opinion a pretty bad idea.
The question itself
Is there any smarter way to track this information? Is extracting the page-information and overwriting the pageview attributes the best way to do this?
I just started using GA and have kind of no idea how to do this any other way.
You could use filters (custom advanced filters) to rewrite the request url inside the google admin interface (admin->views->filters). This has the advantage that you do not need to change your site/application code.
However using filters will require multi-step-filters with heavy use of regular expressions, and you would have to test this in a "staging" view first (because a wrong filter will permanently mess up your data).
Passing a custom url to the pageview tracking is pretty straightforward and can be tested immediately via the real time view. In my opinion this is indeed the smartest way to do this.
When looking at Google Analytics, all reports show URLs that begin with a slash and www: "/www.url.com/page.html."
I've never seen Google report like this. Webmaster Tools is set up correctly. Not sure what else can be set up in Analytics. Any idea?
Current: /www.url.com/page.html
Typical: /page.html
by default GA only reports the relative path and query string of the URL (it strips the protocol and domain. So one of two things must be happening:
1) you have code that is passing a custom page name to the _trackPageview call, adding that "/[domain]".
2) you have a custom filter within the interface setup that is prefixing the page name with "/[domain]"
Adding the domain to the page name is a fairly common thing to do when you have GA code spanning multiple domains, most especially when they are going to a rollup profile, so that you can see which pages are coming from which domain.
So if I had to guess (and this is only a guess, seeing as how I don't have access to your
code or GA interface), someone must have attempted to rebuild the full url to use as the page name instead of just the path+querystring - and then messed up (probably a messup in some regex with the protocol, if I wanted to throw even more guesses at it).
But the 64 thousand dollar question is.. where is it being changed? Like I said.. GA by default does not do this, so someone has added code to do it on your site, or else a filter within the interface.
I would start by looking to see if there are any filters in your interface, since that is the easier thing to determine. If you see no filters relevant to this, then you will have to look on your page code (including any script includes or other javascript code being output). It would be a value passed with _trackPageview so ctrl+f for that.
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.
I have this code setup to track image downloads throught Google Analytics.
<a href="/media/37768/CC20100117m001_thumb_2000.jpg"
onclick="pageTracker._trackEvent('Image', 'Download', 'file.jpg');"
class="hi-res track">
Hi-Res</a>
But the events don't ever show up in the GA reports.
I thought maybe the the browser was following the link before the javascript was being run but setting href="#" doesn't work either.
Any ideas?
Your browser may be following the link before Analytics can make the call logging the event. Check out this page for Google's solution:
http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55527
I don't know what your error is but to find out what it is you can:
Install firefox
Click on the link
Go to Tools | Error Console
Look at the error
This doesn't look correct to me--for one thing, you're using 'pageTracker' to call _trackEvent. In fact, _trackEvent is called by an 'event tracker' object, which is instantiated with _createEventTracker.
There are a couple more steps you haven't mentioned so et's go through them all. (There could be more than one correct way to do this--i'm not sure. So what i can do is compare your code with the steps i follow every time i set up Event Tracking, and which so far, have worked every time.)
Enable 'Event Tracking'. (Do this in
your profile.)
Create the 'event tracker' instance and insert it just after the last line of the pageTracker script. This is usually a single line of code that in your case would look something like this:
var ImageTracker = pageTracker._createEventTracker("Image")
Set up the call to the _trackEvent()
method in your html. I think all you
need to do here is replace
'pageTracker' in your code with 'ImageTracker' (the
event tracker instance you created
in step 2 above).
I faced a lot of problem then I realize it's easy steps and want to share with everyone.
First change your script code, link is https://developers.google.com/analytics/devguides/collection/gajs/#quickstart
Second add a button like this.
<>button onclick="_gaq.push(['_trackEvent', 'button3', 'clicked'])">Press<>/button>
Enjoy :)