Just a thought, but you can set a cookie in the "vcl_deliver" routine. Something like that:
sub vcl_deliver { if (obj.hits > 0) { set resp.http.Set-Cookie = "VarnishHit=Yes;Path=/;"; } return (deliver); }
It basically says: if obj has more than one hit, set a cookie by saying so. You will need to make sure that you do not write any other cookies, so maybe just connect this to your existing Set-Cookies if you use cookies. For more information about obj.hits objects, see here: https://www.varnish-cache.org/docs/3.0/reference/vcl.html
Here is the important line:
obj.hits The approximate number of times the object was delivered. A value of 0 indicates cache miss. This variable is also available in vcl_deliver.
This will give you access to this information from Javascript using the document.cookie variable. I believe jQuery has several plugins to make it easier, here is what I found: https://github.com/carhartl/jquery-cookie on Google. Once you can check for a cookie in JS, you can use the GA API to register the event. Hope this helps.
source share