I'm looking to collect data on page load speeds via Google Analytics, and would like to split this between pages that have been returned having HIT the Varnish cache, and those that have MISSED the cache.
Before looking into this I just assumed I'd get JS to have a look at the varnish headers in the page response, and create a GA custom var to track this on a per-page basis. Of course, JS doesn't have access to the page headers, so I'm at a bit of a loss currently. I've made server-side GA tracking work ing the past (via php-ga) but this needs to tie in to real-world page load time.
Just a thought, but you could set a cookie in the "vcl_deliver" subroutine. Something like this:
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.Set-Cookie = "VarnishHit=Yes;Path=/;";
}
return (deliver); }
This basically says: if the obj has more than one hit, set a cookie saying so. You will need to make sure you do not over write any other cookies, so maybe just concatenate this to you existing Set-Cookie, if you are using cookies. For more info on the obj.hits look here:
https://www.varnish-cache.org/docs/3.0/reference/vcl.html
Here is the line that matters:
obj.hits
The approximate number of times the object has been delivered. A value of 0 indicates a cache miss. This variable is also available in vcl_deliver.
That will give you access to this information from within Javascript using the document.cookie variable. I believe that jQuery has some plugins to make this easier, here is one I found: https://github.com/carhartl/jquery-cookie on Google. Once you can check for the existence of the cookie in JS you should be able to use the GA API to log a event. I hope that helps.
Related
We have a web application (AJAX/SPA type) that I did not build but need to track analytics on. The URLs are not very nice or symantic and they are ran via Ajax in most cases. I don't really have control over what gets generated.
What I want to do is on an onClick, a function is called so I can track the "pageview" of the Ajaxed called page.
The URL format that gets generated in the address bar is something like this:
https://subdomain.mysite.com/subpage/myfoopage.html#details=ecatalogue.54321
https://subdomain.mysite.com/subpage/mybarpage.html#details=ecatalogue.12345
Given that the URL has a hash AND a period in it, I have NO idea how this will show up in Analytics and before I go sending "sends" to my analytics dashboard, I thought I would reach out for some advice. Thank you in advance.
My current code I was thinking of using would be the ga set and ga send.
But given the hash, I an wondering if I should use window.location.href to send it all or do I have to modify the URL before sning it to Analytics.
So currently, simply using jQuery for simplicity sake:
$(".somelink").on('click', function(){
//let the hash load
setTimeout(function(){
var myPath = window.location.href; //https://subdomain.mysite.com/subpage/myfoopage.html#details=ecatalogue.54321
ga('set', 'page', myPath);
ga('send', 'pageview');
},2000);
});
Would the path in full get sent with the hash and the period or would it add a special "+" character or something or do I need to so some string parsing to get rid of the hash and the period in the location path??
Thanks in advance.
If you don't want to send the hash and anything after it, you may should remove it from the path string using split method or whatever like this.
The best way to test it is configuring a "development" property in GA and send the data to it. After your validation you can change the property id to the production one.
The hash will come through. Using your example of:
https://subdomain.mysite.com/subpage/myfoopage.html#details=ecatalogue.54321
In GA it will show up like so:
/https://subdomain.mysite.com/subpage/myfoopage.html#details=ecatalogue.54321
With the additional / in front, since you're only supposed to send the page path.
So I recommend you parse out the hostname and protocol from it.
I'd like to know how active are the users depending on the source/medium/campaign of the traffic they came from before sign up. I'm hitting wall when trying to get campaign/source/medium for the current visit from Google Analytics tracker in JS.
I found this tracker.get() method: https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#get but they don't specify what are the possible values for 'fieldName'. I inspected the tracker object in Firebug and found out the screen attached screen grab:
Are these the only fields that I can query? Is reading any of the campaign fields possible? I understand that GA intends to be anonymous, but I figured this information is not really a secret...
If I can't retrieve this information from GA tracker, how would you approach this question? Should I set my own cookie whenever there's a visit with utm_ parameters and then read it if the visitor chooses to sign up?
I'm aware of this response: How do I extract Google Analytics campaign data from their cookie with Javascript? but a comment says parsing a cookie won't work for AdWords traffic..
I had to do something similar for a project. You need to think outside of the box to get this working. This will only work for cases where you put the campagn information in the url, for tagged url's.
1) On every page add server side code(php, asp .net) to parse the querystring and save the campagn/source/medium from the url in a session(or cookie)
2) Retrieve this information where you need it and process it.
You indeed need some custom javascript in order to achieve this.
I created an open source project for this that addresses the need quite simply:
https://github.com/ilkkapeltola/visitdata
Once you include the javascript library, you can call visitdata.get() and it'll return an object with things such as source, medium etc.
After a user fills in my "new" user form on "example-one.com", the "create" controller creates the record in the db. Then it does a redirect_to to an external site "payment-checkout.com". I have setup the Google Analytics code on both sites.
Google provides two functions _link and _linkByPost for use to use in any links or forms that go to your external domains. The problem is the user is being redirected by the controller action outside of the view and I cant use those two javascript functions to pass on the relevent G.A. info - what do i do?
Can anyone help?
The way _link works is by passing the Google Analytics cookies from your first domain via a query string to your second domain. The second domain, if configured correctly, will accept those URL parameters and apply them as cookie values for the purposes of tracking.
So, it shouldn't be difficult for you to apply your own version of the _link function.
Specifically, the _link function passes the following cookies:
__utma, __utmb, __utmc, __utmx, __utmz, __utmv and __utmk
Into a query string as such: ?__utma=87278922.614105561.1288923931.1294376393.1298325957.6&__utmb=87278922.1.10.1298325957&__utmc=87278922&__utmx=-&__utmz=87278922.1288923931.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)&__utmv=-&__utmk=72493274
So, all you need to do to replicate the _link function is, before you apply the server side redirect, grab the cookie values, and apply them as a query string on the URL you're redirecting to.
Now, that's not the only thing you'll need to do to get this working. The Google Analytics configuration on the payment site will need to be configured with _setAllowLinker set to true, as well as potentially disabling the domain hash and setting a particular domain name for the tracking cookies; it depends on your configuration. You can find out more about that in Google Analytics Cross Domain Tracking Guide.
#yc's approach looks like the best bet but if that doesn't work, I would suggest having your controller redirect the user to a "temp" page on your site itself and show some text like "Checking out....Please wait..." and using Javascript trigger the call to the "_link" function to redirect the user to the "payment-checkout.com" (again using Javascript).
I assume you're also tracking the page the user returns to and want to measure how many users you lose in the process in between?
My knowledge of the Google Analytics API is fairly limited, so maybe there's a better solution, but you could consider rendering a page containing the GA code and triggering the _link() function from there?
It might also be possible to perform an AJAX call on submitting the form (maybe using remote_form_for) and handling the GA redirect in an RJS-response:
page << "_gaq.push(['_link', 'http://example.com/test.html']);"
However, I'm not sure how well that would fit into your application.
When using Google Website Optimizer A/B split test, is there a way I can read a cookie to see which version A or B did the user get? I need to know this from a page other than the control page.
Using JavaScript, you could extract the variation number client side and place it into a cookie, see my post here for details ( http://www.optimisationbeacon.com/testing/how-to-integrate-google-website-optimizer-with-google-analytics/ ), but you'll need the following code to get the combination number (undefined means the test is not running, 0 is A, 1 is B, and so on...):
utmx('combination')
That will get you the variation number. Otherwise, you can extract it from the cookie "__utmx":
222149575.00015140143722727739:2:0
The number on the end, "0" indicates this is version A or the original.
I'm not familiar with any way to parse the _utmx/_utmxx cookies like that.
Your best (and simplest) bet is to cookie them yourself on those various pages, and then access those cookies as you need them. On the control page, be sure to put the cookie-placing code below the control script, so that it doesn't cookie them every time before it redirects.
ASP .NET is allowed
Storing the values in hidden input fields is allowed
Query String is not allowed
POST request is not allowed
It is possible to store JS variables between GET requests ?
I want to reinitialize them on the client using ClientScript.RegisterStartupScript
Can I use cookies for this ?
Are there other posibilities?
Where cookies are stored when Request is made ?
Can I use cookies for this ?
Yes, see this tutorial on using cookies in Javascript.
Are there other posibilities?
If you are not allowed to append anything the URL of your requests, I can't come up with any.
Where cookies are stored when Request is made ?
In the HTTP request header. The aforementioned tutorial will tell you how to read their values from Javascript. On the server side with ASP.Net, you can read cookie values using Request.Cookie["cookieName"] which returns an instance of HttpCookie.
I wouldn't highly recommend this, but the other option is to alter the window.name property.
You can save some minor bits of data here, then retrieve them on the next page load.
Pros:
Quick-n-dirty, but works
Cons:
Messes up any window references for popups/child iframes
Since its a "hack", browser vendors may break this "feature" in the future
Of course if you can exclude all the old browsers, then use Global/Client Session Storage!
At the moment using cookies is your best bet. You can serialize the JavaScript objects to strings, and unserialize them back into objects later. A good choice format is JSON, since it is a subset of JavaScript.
There is also storing objects in Flash.
Storing in Google Gears.
DomStorage
See this library that has an interface to each:
http://pablotron.org/?cid=1557
If you are in control of all aspects of the page, then you can also wrap the page in a top level frame. Then only refresh the child frame. You can then store content in the parent frame.
You can see this used in sites like GMail, and others where the only thing that changes in the URL is outside the #.
You don't even have to change the URL, that part is just put in for Human Friendly URLs. (So you can actually copy and paste URLs as is).