Skip to content

Instantly share code, notes, and snippets.

@tmahesh
Created February 18, 2011 00:56
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tmahesh/833066 to your computer and use it in GitHub Desktop.
Save tmahesh/833066 to your computer and use it in GitHub Desktop.
How fast is my site? integrate boomerang and google analytics
<script type="text/javascript">
BOOMR.init({
beacon_url: "/boomerang.gif",
BW: {
enabled: false
}
});
BOOMR.subscribe('before_beacon', trackInAnalytics);
var pageType = "homepage"; // customize this
var pageTitle = "Measuring pageSpeed in GA"; // customize this
function trackInAnalytics(beacon) {
try {
if (!beacon.t_done || beacon.t_done < 0) return;
var timeTaken = beacon.t_done;
_gaq.push(['_trackEvent', pageType + 'PageLoad', getBucket(timeTaken), pageTitle, timeTaken]);
} catch (e) {}
}
function getBucket(timeTaken) {
var bucketString;
var bucket = [1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 9000, 10000, 15000, 20000, 30000, 45000, 60000];
for (var b = 0; b < bucket.length; b++) {
if (timeTaken < bucket[b]) {
bucketString = '< ' + bucket[b] / 1000 + 's';
break;
}
}
if (!bucketString) bucketString = '> ' + bucket[bucket.length - 1] / 1000 + 's';
return bucketString;
}
</script>
@tmahesh
Copy link
Author

tmahesh commented Feb 18, 2011

@SGudbrandsson
Copy link

Two things to note:

  1. If you're using the updated Google Analytics, you may want to replace the code
    _gaq.push(['_trackEvent', pageType + 'PageLoad', getBucket(timeTaken), pageTitle, timeTaken]);
    with
    ga("send", "event", pageType+"PageLoad", getBucket(timeTaken), pageTitle, timeTaken)

Also, make sure to include rt.js rather than bw.js because bw.js doesn't trigger the beacon for some weird reason.
Took me about two hours to play around and debug to get this working.

@SGudbrandsson
Copy link

... and after a little testing I noticed that bounce rate immediately went to 0% after sending these events .. NOT what I wanted!

If you send the event (using either the old ga.js or the new universal analytics), you'll notice that the bounce rate immediately goes close to 0%. Meaning that both _gaq.push and ga("send", "event" ...) will kill the bounce rate measurement.

I didn't want to kill the bounce rate, I wanted to measure it (and other metrics) against the load time to get a baseline, then improve the load time and measure the effects.

Fortunately, you can instruct the event to NOT affect the bounce rate, as mentioned here: http://www.lunametrics.com/blog/2014/05/06/noninteraction-events-google-analytics/

Here is an updated version of the ga send and gaq push that will make sure GA won't change the bounce effects:
_gaq.push(['_trackEvent', pageType + 'PageLoad', getBucket(timeTaken), pageTitle, timeTaken], true);
ga("send", "event", pageType+"PageLoad", getBucket(timeTaken), pageTitle, timeTaken, {'nonInteraction': 1})

Hope this helps y'all :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment