Getting rid of fb_xd_fragment in Google Analytics

Sometimes some strange URLs may appear on your content reports with the query parameter fb_xd_fragment. It’s caused by Facebook social widgets.

In this post you’ll find out if you have this problem, how it happens and how to get rid of it.

Do you have this problem?

Do you have any Facebook widgets on your page? Like buttons, share buttons… doesn’t matter. If you use a Facebook widget and Google Analytics you are probably affected by this issue.

Just go to you content report and do a quick search for fb_xd_fragment.

content report showing fb_xd_fragment pages

OMG, what is all that trash in my content report?

This is a side effect caused by a workaround used by Facebook to support old browsers.

Every time you insert a Facebook widget you’re opening an iframe inside your website and inside that iframe there’s a page on the facebook.com domain. This page inside the iframe needs to communicate with the parent page in order to show you those nice Facebook ‘likes’ counters.

So when you have a ‘like’ button, Facebook has to do some cross-domain communication through the iframe. For security reasons, it’s not an easy job. You can’t give an iframed page access to all of the pages on your website or else you’d be opening a huge security hole in your website. New browsers support the postMessage interface to securely communicate with iframes.

Older browsers (most notably MSIE 6 and 7 and Firefox 2) don’t support postMessage and just block all that frames try to send to the parent page. The workaround used by Facebook and many others that attempted the same, is kind of oldschool but works. Instead of trying to talk to the parent page, they open up a new iframe, inside the iframe, pointing to the parent page. This allows them to send commands to the parent page via query parameters.

I like to call this "The Facebook iFrameception" effect

Ok enough with the I.T. talk. What do I do?

Ignoring that parameter on Google Analytics Profile configuration won’t solve the problem. Since that just removes the parameter, while you should be removing those pageviews altogether.

The right fix for this is to implement a channel URL. This is a workaroundprovided by the Facebook API to work around issues like this. You must providea URL that has the code bellow. So instead of duplicating your page inside ahidden iframe, Facebook will open that page inside a hidden iframe. This ismuch faster, since this page is very small, and doesn’t count pageviews twicebecause you haven’t put a GATC on that page.

It doesn’t just fix the issue but also speeds up the page loading on yourwebsite for the browsers that don’t support postMessage.

To implement it just create a page somewhere on your domain with the following content: