Tracking Complex Interactions in Dynamically Added Cross-Domain Iframes – Google Analytics & Iframes, Pt 3



Welcome to part three of our series on tracking user behavior within iframes. In Part One, we discussed how to track simple user interactions within cross-domain iframes by using the postMessage API. In Part Two, we discussed tracking complex interactions when the iframe in question is on the same domain as the parent frame. Today, we’ll be tackling tracking user interactions cross-domain when the iframe is added dynamically using JavaScript.


For this to work, you need to be able to add unadulterated code on the iframe and the page the iframe is inserted on. If you’ve got a third-party service you’d like to track, and you can’t insert code snippets on their pages, this will not work and you’re out of luck. If you can’t add code to the iframe, you can’t measure interactions with it, period. Do not pass GO, do not collect $200. Sorry.

The Challenge

Just like in Part 2, we’re still facing one important challenge; the Client ID for all of our GA hits, a value stored in the user’s _ga cookie, must be identical in order to properly track complex interactions within iframes. The default Client ID consists of a random, unsigned 32-bit integer and a timestamp rounded to the nearest second; here’s yours:

Just kidding, you don’t have JavaScript enabled (or you’ve blocked us from setting the _ga cookie)!

No matter what your approach, you must ensure the Client ID is the same for the hits being sent from your parent frame and the hits being sent from your iframe. If you send hits with two different client IDs, you’ll wind up with two different sessions, and worse, two entirely different, irreconcilable users within Google Analytics.

Getting Started

Many applications dynamically append iframes during the course of user interaction, like a displaying a form when the user clicks ‘Apply Now’. The code for this might look something like the below.

If your iframe is being dynamically appended to the page in this or a similar manner, your easiest option is to transmit your user’s Client ID using a dynamically appended query parameter. To do this, you’ll need to adjust the code that creates and appends your iframe. You have two options for transmitting your Client ID: you can use the native linkerParam that Google Analytics uses for cross-domain Client ID Transmission OR you can create your own arbitrary parameter and retrieve and transmit the Client ID manually.

Another Quick Caveat

For this method of tracking to work, you’ll need to be able to edit the code that appends the iframe to your site. If that code is off-limits to you, there’s still hope; look for Part Four in our series, where we’ll discuss tracking embedded iframes using postMessage.

Using linkerParam

linkerParam is a native mechanism for cross-domain transmission of Google Analytics Client IDs. It is attractive to use because it is Google-supported and has robust safeguards to prevent accidental Client ID pollution. To use linkerParam, you’ll need to retrieve it from the Google Analytics tracker running on your site. Here’s a code snippet you can use to retrieve the linkerParam value:

Note: the above will fail if invoked before Google Analytics has loaded.

You then dynamically append the linkerParam value to your iframes src attribute:

Next, you’ll need to enable allowLinker on your tracking code in your iframe. If you’re using hard-coded Universal Analytics code, adjust your snippet like below:

In Google Tag Manager, you’ll need to add the Fields to Set allowLinker, and set the value to true.

Once you’ve configured these items, run a test while using the Google Analytics Debug Chrome Extension. In the logging output, you should see the following line:

Loaded linker parameter: 1.241566660.710478958.1442863401

Using Your Own Parameter

If you cannot use the linkerParam functionality, you can also create your own parameter and deliver the client ID that way. In order to this, you’ll need to extract the Client ID from the parent frame. There are two ways to do this: asking GA directly, or extracting it from the _ga cookie.

Then, you’ll want to append the Client ID to the parameter of your choice, which you’ll append in turn to your iframe’s src attribute. You’ll need to take one more step, though; add an additional value to your parameter to prevent a user from sharing that value with another third party. Google Analytics does this with linkerParam – the first of the three dot-separated numbers is a hash of the clients User Agent and a timestamp, which it then checks against when the client loads the other domain. The linkerParam will be ignored if the value was generated more than 2 minutes ago or the UA doesn’t line up. You might feel comfortable just using a timestamp, for this particular use case.

You’ll have to add logic to check that value in your iframe, too.

Once you’ve verified your Client ID, you’ll need to instruct Google Analytics to set the Client ID within the iframe to the Client ID that you’ve transmitted. If you’re using hard-coded Universal Analytics, adjust your code accordingly:

If you’re using Google Tag Manager, you’ll want to set your parameter-extracting code into a Custom JS Variable, and then set the Field to Set clientId to its value.

Dan Wilkerson is a Software Engineer at LunaMetrics. He is passionate about web technology, measurement, and analysis. Dan is the winner of the 1999 Forge Road Elementary School Science Fair for his groundbreaking report on how magnets work. (ICP, take note.) Dan has worked at LunaMetrics in social media, as our marketing manager, and now in our analytics department.

  • coccoinomane

    Thank you very much Dan for this great series… very useful indeed!

    May I ask your opinion on a related matter? On my website I have a dynamically-loaded iframe from Facebook. I would like to track clicks on a “Book now” button therein as if they happened on my domain, rather than on I guess I am out of luck, since I can’t control whether Google Analytics is installed on Facebook?

    • Dan Wilkerson

      You are correct; you need to be able to add code within the iframe in order to track interactions therein, as browser security policies prevent observing interactions within cross-domain iframes.


  • Huong Nguyen

    The same question like coccoinomane
    Hope the author gets read soon 🙂
    Thank you

    • Dan Wilkerson

      Hi Huong! Here’s my response to coccoinomane:

      You are correct; you need to be able to add code within the iframe in order to track interactions therein, as browser security policies prevent observing interactions within cross-domain iframes.

      Hope this helps!


      • Huong Nguyen

        Thanks Dan,
        And we have no ways to track links click or action submit in iframe?if we just have google analytics code and google tag manager container for parent site?

        • Dan Wilkerson

          Yes, there is no way to do so. You cannot track any interactions in an iframe without being able to add code.


  • dave

    Hi Dan, my scenario is that we accept donations for various charitable causes in a shopping cart setup. our thankyou page is generated in an iFrame. to track the conversions i need to pass transaction details through a data layer into GTM. i dont see you discussing passing data layer info in the iframe. any ideas on best approach?

Contact Us.


24 S. 18th Street, Suite 100,
Pittsburgh, PA 15203

Follow Us



We'll get back to you
in ONE business day.