Category Archives: Google Analytics

Recording AJAX Requests with Google Analytics

Hey everyone,

I’ve just finished setting this up on an AJAX-heavy site I’m working on. It’s all pretty easy to setup, copy the following code and use it to replace your standard analytics code:


	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	ga('create', 'UA-xxxxxxxx-x', '');
	ga('send', 'pageview');		

Becomes this:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

And the last step is to add this JavaScript whereever your ajax request is being made, for instance: _gaq.push([‘_trackPageview’, ‘/contents/get_content_by_id/’ + id]);

        //Prepare ajax
		mtehod: 'GET',
		url: '/contents/get_content_by_id/' + id,
		dataType: 'JSON',
		success: function(data){

			//Handle response

			//Push view to analytics
			_gaq.push(['_trackPageview', '/contents/get_content_by_id/' + id]);
		error: function(){

			//Remove loader and display error
			remove_loader(target_selector, null);

Just add the line commented Push view to analytics and you’re done. To test it, I just used to the real-time analytics – it shows up straight away. If you run into any trouble the docs are pretty decent: – Google Analytics Referral

Well, today I was looking through the Google Analytics overview of the and I noticed a referral I hadn’t seen before from

I did a bit of Googling and it turns out that this is actually something Microsoft uses within IE and MSN. Apparently, whenever someone clicks on a link in msn the service will check the link against a list of unsafe sites and warn the user if anything is amiss.

It just so happens that I’d sent a link to the blog to a friend on msn – so I guess everything checks out. Here’s a bit of a blurb from the FAQ on Microsoft’s Site:

SmartScreen Filter is a feature in Internet Explorer that helps detect phishing websites. SmartScreen Filter can also help protect you from downloading or installing malware (malicious software).

SmartScreen Filter helps to protect you in three ways:

As you browse the web, it analyses webpages and determines if they have any characteristics that might be suspicious. If it finds suspicious webpages, SmartScreen will display a message giving you an opportunity to provide feedback and advising you to proceed with caution.

SmartScreen Filter checks the sites you visit against a dynamic list of reported phishing sites and malicious software sites. If it finds a match, SmartScreen Filter will show you a warning notifying you that the site has been blocked for your safety.

SmartScreen Filter checks files that you download from the web against a list of reported malicious software sites and programs known to be unsafe. If it finds a match, SmartScreen Filter will warn you that the download has been blocked for your safety. SmartScreen Filter also checks the files that you download against a list of files that are well known and downloaded by many Internet Explorer users. If the file that you’re downloading isn’t on that list, SmartScreen Filter will warn you.

Anyway, that’s all I’ve got for now – Good Luck!