I have some pages on my website that are intended for other users to embed those pages on their own webpages using iframes. My iframed pages have a Google Analytics tracking code and each time they are embedded on someone elses page, it counts as a normal page view, per Google's recommendation.

However, in some cases on my own website I need to add the same iframed pages, mostly as an example for other users so they know how to properly add the iframes to their own websites.

So this results in an inflated page view count in GA because my webpage gets a page view and then my iframe I add also gets a page view, resulting in 2 page views for a single page.

How can I avoid this problem?


1 Answer 1


You could add a little JavaScript to check whether the GoogleAnalytics should be activated on that page or not.

First, you can detect that the IFrame page is an IFrame with the following:

if(window.self != window.top)
    // IFrame code here

If you know that a page is an IFrame, you don't need that specific if().

Now you can check the domain of the current window (window.self.location) and the location of the top window (window.top.location). If it has the same origin, then it is the same website. I have not tested across browsers, but that should work on Firefox:

if(window.self.location.origin != window.top.location.origin)
    // Put the Google Analytics JavaScript code here

That way, you won't even ping Google Analytics if it is on your website. On someone else website, the origin will be different and the Google Analytics code will kick in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.