Measuring Page Load Time of single-page app with GA - google-analytics

I'm using Google Analytics in an SPA. For any virtual page redirects (like an AJAX call to refresh the body of the page), I'm getting a page load time of 0ms. Is there a way to track how long that takes, just as if it was a full page refresh? I'm hoping to include how long it takes for the AJAX call and also the time to download and display images that are loaded as a result.

As you have found, Google Analytics will not provide page timings for SPA's. This includes if you increase the site speed sample rate to 100. This is because Google Analytics calculate the page timings using the Navigation Timing API.
For example, DOM loaded would be:
$(document).ready(console.log((Date.now() -
performance.timing.domComplete)/1000))
To over come this problem, you will need to use custom metrics. The solution has three steps.
1) Set up a custom metric in GA.
Go to Admin > Property > Custom Definitions > Custom Metric.
Create a new Custom Metric, with the scope of Hit and the formatting type of time. Note: Specify time in seconds, but it appears as hh:mm:ss in your reports.
2) Set up a timer.
You will need to capture the time when you want to start the measurement of page load time.
An example solution to this might be by decorating all of your internal links, e.g:
$('a[href*="stackoverflow"]').click(function(){
time1 = Date.now()
})
3) Send the time eclipsed (in sec) to Google Analytics on the virtual pageview event.
When the virtual pageview event occurs (which triggers your virtual pageviews), retrieve the difference between the current time (Date.now()) and the time which the timer was started (time1).
Using Google Tag Manager, a custom javascript variable can be created as below:
function(){
return (Date.now() - time1)/1000
}
This value then needs to be sent with the pageview, against the custom metric index set up in step1.
ga('send', 'pageview', {
'metricX': pageLoadSpeed
});
Using the custom metric along with calculated metrics (e.g. {{virtualPageTimings}}/{{pageViews}}, you will be able to calculate your average virtual page timings.
Bonus:
To make the measurement more accurate, set up a secondary custom metric to count the number of virtual pageviews. This will make sure that pageviews which users are directly navigating to are not taken into consideration.
To do this, create a custom metric with the scope hit and the formatting integer.
Then with every virtual pageview, send the value 1 against the custom metric index. E.g:
ga('send', 'pageview', {
'metricX': pageLoadSpeed,
'metricX': 1
});
This allows for the calculated metric:
{{virtualPageTimings}}/{{virtualPageViews}}

If you checkout the Google Analytics docs, you can find out about the siteSpeedSampleRate option, which basically allows you to turn on your site tracking beacons for a percentage of your users.
By default this value is at 1, but I'm assuming you might want to turn it to 100. It might affect a bit in term of network usage since it will have to transfer more data to GA, so take that into account depending on your users and how they access your website (through mobile, bad coverage in some countries...).
You'll have to modify your tracking code to integrate something like the following:
ga('create', 'UA-XXXX-Y', { siteSpeedSampleRate: 10 })

You can send the timing manually, As everything on Google Analytics. But it's a little bit tricky to do,if I'm honest I would no do it unleast it's necessary. All the data on the time report are based on a hit called timing, this hit is send after the pageView and contains the following information.
If you can see my example, I forced the tool to send the hit, just after the pageview goes another hit with a special list of parameters.
plt : Specifies the time it took for a page to load. The value is in milliseconds.
pdt : Specifies the time it took for the page to be downloaded. The value is in milliseconds.
dns : Specifies the time it took to do a DNS lookup.The value is in milliseconds.
rrt : Specifies the time it took for any redirects to happen. The value is in milliseconds.
srt : Specifies the time it took for the server to respond after the connect time. The value is in milliseconds.
tcp : Specifies the time it took for a TCP connection to be made. The value is in milliseconds.
dit : Used to send a random number in GET requests to ensure browsers and proxies don't cache hits. It should be sent as the final parameter of the request since we've seen some 3rd party internet filtering software add additional parameters to HTTP requests incorrectly. This value is not used in reporting.
clt : pecifies the time it took for the DOMContentLoaded Event to fire. The value is in milliseconds.
More info of this parameters on : https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters and
You can see more info of this hit on
https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings
So what happens now?, if I launch another pageview in this SPA, the second pageview on the same page will not carry this hit and you will ever get 0 of loadtime. You can use the command as the official documentation but if you use it you will notice that is not the same hit (i have to double check that). Other option is send it manually using the command 'send' and attaching the desire information. Check your pageview hit structure to be sure that your timming is actually attaching to your previous hit.
The comand to send the timming after the pageview is send will be something like that, use the &dl parameter or 'dp' parameter to attach the timming to the ajax page.
ga('send', {
hitType: 'timing',
'&plt': 1,
'&pdt': 1,
'&dns': 1,
'&rrt': 1,
'&srt': 1,
'&dit': 1,
'&clt': 1,
'&dl': 'http://cl.edreams.com/',
});
Now all the values '1' needs to be updated for the correct one, now how to determine the time of each parameter not sure at all. Also remember that the sampling by default is only for the 1% of the sessions, send this hit only in a few cases.
Disclaimer : This is a very experimental implementation, we are forcing the Js to send unexpected information. Test it well before to pass it to a final project
Greetings

Related

How do I handle hashtags in regular expression for google analytics?

I have a URL goal I'm trying to track as a destination in google analytics. Let's say its https://foo.bar.com/#/home.
Using "Equal to" I get nothing. Using "Regular expression" When I type it in as a destination goal foo.bar.com/#/home I get nothing.
./home 22.31% conversion rate over last 7 days
.#/home 8.14% conversion rate
.bar.com/#/home 0 conversion rate
.bar.com#/home 8.14% conversion rate
.foo.bar.com#/home captures nothing
foo is dynamic so I only want the urls with both start with foo and have .bar.com/#.home.
Is there something about the # thats the issue?
EDIT
now i'm trying foo\.bar.com/*/home and I'm getting 8.5%. Is that correct?
Hashes are not tracked in GA by default, so if you go to your real-time reports (or even just by analysing your collect hits via GA Debugger or dev tools), you'll see that whenever a hash appears in your page path, everything from the hash forward will be removed. This explains why you see 0% conversion rates for some of your attempts. You would have to send a virtual pageview instead and modify the destination url, so something like this:
ga('send', 'pageview', '/vpv/new/destination/path');
And then in your goal destination, you can use the new path of /vpv/new/destination/path.

CustomDimension send via ga('set',...)

I have two buttons, to set gender.
For each gender button I've set up a customDimension like ga('set', 'dimension1', 'male'); or ga('set', 'dimension1', 'female');
In the Javascript Console from FireBug I see that the ga() command is executed without errors. In GoogleAnalytics I have set a customDimension
Must I send this data specific via ga('send',...... or is this not needed??
If yes, which parameters do I have to set?
I can't see any received Data in GoogleAnalytics GUI and I have waited about 48 hours.
Custom Metrics and Dimensions must always be sent with an interaction hit, else they will not get recorded.
In addition, fields set with ga('set'... must be followed by an interaction hit - "set" in this case literally means "set this field for subsequent use in interaction hits". The difference between using set and passing the custom dimensions via the configuration object of an interaction hit (pageviews, events etc) is that "set" will affect all following hits while passing the custom dimension/metric as a parameter to a hit will only affect that specific hit.
So if you use ga('set', 'dimension1', 'male') and you have after that one pageview and two events the dimension will be recorded three times (not so much a problem with custom dimensions, potentially a big problem with custom metrics).
If you do instead:
ga('send', 'pageview', {
'dimension1': 'male'
});
the dimension will be send only once.
But no matter how you do it, the data will only be sent along hit data, so you need a pageview, event or transaction if you want any results.

How do I pass a parameter from my site into google analytics?

Let's say I have a site and the user comes into it with a parameter:
http://example.com&url=blahblahblah
How do I go about passing along the url value from the parameter into Google Analytics?
1) User comes to the page with a url in the params
2) User clicks a download link with a ga tracking code attached to it which was generated from ga account like this:
http://example.com/download/param1=dkljdf&_ga=1.149898996.39207121.1424368466
You have to create a custom Dimension and a metric for that.
About custom Dimensions and metrics:
https://developers.google.com/analytics/devguides/platform/customdimsmets
After you have created a Dimension, you can add metrics to it by view, in example.
Follow the steps here for Universal Analytics:
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Note that due to not have 10 point of reputation, I wrote the a "_" in http like "ht_tp"
BUT:
I think what you want to know is the number os visitors that clicks a download link in your site that comes from, lets say "blahblahblah" as web origin or other methods.
For that, you have the param utm_source that you can receive directly in the url.
So instead of ht_tp://example.com&url=origin you should receive ht_tp://example.com&utm_source=origin
In this way, you have no care about it. Analytics is going to take care for you so you can get a report of clicks by source.
Or, just use the referer in case all the incoming visitors are from webs:
ga('set', 'referrer', 'ht_tp://example.com');
And a final option, to use Events:
_gaq.push(['_trackEvent', 'ReferencedVisitors', jsVarWhereYouHaveTheOrigin]);

Universal Google Analytics virtual pageview location

Situation
On our site we implement virtual pageviews for ajax filters and page scrollings.
When user open Rent page on our site:
real url: "/rent"
send to GA:
ga("send", "pageview", {page: "/rent/"})
When user scroll to second page:
we change real url via js: "/rent/?page=2"
send to GA (we care only about main page):
ga("send", "pageview", {page: "/rent/"})
Or when user drills down:
we change real url via js: "/rent/appartment/?page=2"
send to GA (we care about path):
ga("send", "pageview", {page: "/rent/appartment"})
But we noticed that really GA code does not take current real url (window.location) each time we call ga, but uses first time location. So when user drill down GA code send:
GET collect?...&t=pageview&dl=http://example.com/rent&dp=/rent/appartment&...
....
Referrer: http://example.com/rent/appartment
Here dl parameter is location of our page (not really current location) and dp is a page parameter in ga call. Notice that referrer is ok.
We decided to provide ga with real location (like in referrer) and change code to:
ga("send", "pageview", {page: "/rent/appartment", location: "http://example.com/rent/appartment"})
Problem
From now on I was satisfied, but was not guys that uses GA to analyze Paid Search effectiveness: bounce rate raised drammaticaly (something like 20% to 70%).
Drilling to the problem I've noticed that GA "loses" user when we remove campaign parameters (utm...) and send location without them.
Questions
Should I care about real location in this situation? How does location affects pageviews? How can I workaround or solve this problem?
Additional information
Seems like the main problem is that we drop CPC parameters when we change location for GA:
landing page: /rent?utm_source=...&... (or gclid for Google)
scroll down to second page: /rent?page=2 - there are no CPC parameters.
Additional information here: https://support.google.com/analytics/answer/1714454?hl=en
But I'm still can't figure out an appropriate solution.
When you send a hit to GA with analytics.js, it's going to send the data it has stored on the tracker object that you created when you invoked ga('create', ...);. You can override those values by passing in an object (as you did), but if no overrides are specified, that tracker data is used.
When the tracker object is created it collects data about the current page (e.g. url, title, window size, etc) and stores that information. It does not recollect that info before you send hits. You have to update it if that data changes.
That means if you're updating the page information in an AJAX site and want analytics.js to "remember" that you've updated the page, you'll have to set it on the tracker. The advantage to setting it on the tracker is that if you send other hit types (e.g. events, social, exceptions), you won't have to specify those new values each time.
So, instead of doing:
ga("send", "pageview", {page: "/rent/appartment"})
Do this:
ga("set", {page: "/rent/appartment"});
ga("send", "pageview");
Now, if you send an event later, the event will be associated with the apartment page.
UPDATE: there is now an official guide for how to properly track single page applications with Google Analytics.

Can I manually tell google the gclid?

Here's the scenario: visitor 1 (V1) clicks on an ad and gets a gclid as part of their __utmz cookie. V1 passes the url to visitor 2 (V2) on another computer. V2 visits the page from the specified url and therefore does not get a gclid. V2 completes the conversion, but adwords does not record the conversion as there is no gclid for V2.
If I can track all the above with some confidence, can I send the gclid from V1 to google when V2 completes their conversion?
Can I overwrite the __utmz cookie or can I add any javascript prior to the adwords conversion tracking script?
If I understand correctly, what you are trying to do is to maintain the campaign information if the user that was exposed to the ad sends the link to someone else. This is a major problem with user tracking in general and I see couple of methods that could help you to circumvent that.
First, gclid is used for adwords tracking. Theoretically you can save it in a cookie and then push it as a parameter to every URL that V1 is visiting and this way to maintain the campaign information when she shares the URL. This is possible, but the downside is that the data will appear as a new session of V1, not a new visitor (V2). From Google documentation:
...User A then copies this URL (containing the auto-tagging parameter)
and posts it in another location such as a public discussion board
(forum) or a social network, or sends it in an email or chat window to
other recipients.
Next, User B clicks this link (containing the gclid value) and arrives
at your website. Even though User B has never been to the website
before, this user will have the first session attributed to the same
click id (gclid) value that was assigned to the original User A. In
this case, you see new sessions (caused by User B) from an old
campaign.
Unfortunately there is no workaround to this scenario at the moment.
(source: https://support.google.com/analytics/answer/4588454?hl=en)
The downside however is that you fix a solution only for Google Adwords, while you may have campaigns on different platforms and sites that do not employ the gclid, but for which you use the UTM parameters.
Another solution I've stumble upon in the past is this one: http://www.lunametrics.com/blog/2013/10/02/direct-monster-fix-dark-social/ This script adds a unique query string parameter to every URL. If this URL is shared, in your GA reports it will show data in a custom dimension that denotes the user that shared the URL (V1) and those that used that URL (V2). This way you can analyze the "power of sharing". The downside is that you don't see the conversion under the campaign in question on GA. Never tried this script, only read about it.
Finally, you can make your own cookie and store the campaign information there. Then, you can append it with a parameter to every page that V1 views on your site. If V1 shares it, the campaign information will pass with this parameter to V2. The downside is that you will not see the conversion under the campaign in question on GA, only the parameter. However, you can see in your conversions report how many of campaign-->referral conversions you had by looking at the URL that led to the conversions (in the Reverse Goal Path, for instance). In addition, you are able to track this way not only Adwords campaigns with referrals, but any campaign. Try this project: https://github.com/dm-guy/utm-alternative. This project will help you with creating your own campaign tracking, but not with pushing it to the URL.
I work on AdWords conversion tracking. Here's how I'd do it using AdWords Conversion Import...
If the GCLID is passed in the URL to V2, then the site can grab the GCLID for V2's visit and store it in a cookie (provided you craft some JS to do so).
Then, when the conversion occurs, you've got another bit of JS that collects the cookie and stores it somewhere along with the date/time of the conversion and the type of conversion (this type should match an equivalent conversion type that you've defined in your AdWords account).
Once you have this process in place, you can batch the conversion records up daily and import it into AdWords via the conversion import service (https://support.google.com/adwords/answer/2998031).
The gclid value pertains to Adwords and Adwords only. To give credit to the converting gclid, you need to send the _ga cookie value in the URL query as such:
foo.com?_ga=1.2.xxxxxxxx.xxxxxxxxx
The x's above represent the clientID and this value needs to be cookied using JS:
<script>
function() {
var coo = window.location.href.split("_ga=")[1].split('.');
var cid = coo[2]+"."+coo[3];
return cid;
}
</script>
Have this above the UA tracking code:
<script>
ga('create', 'UA-xxxxxx-xx', {'clientId' : '<?php echo implode(".",array_slice(explode(".", $_GET["_ga"]), -2, 2)); ?>'});
</script>
Remember you must return the _ga cookie value in the URL in order for it fire off on this page. What's clever is, if you can record to a database IP and the associated _ga cookie value, you can track a user across different browsers and/or devices PROVIDED they are using the same IP address.

Resources