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. Though really a Palmyra, PA expat, Dan insists he's a "non-native" 'Burgher. Approaching social early, then marketing, exigencies of native targeting soon entangled him in numbers. Dan is happy to have ended up living in Pittsburgh and (accidentally) as a self-taught and avid programmer.

  • 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?

    • Pepe Sagara

      Hi Dave.
      Did you find the answer to this trouble? I can’t find info to this problem,
      Hope you can help me.

  • Dan, I have been working on some implementations similar to this and recently discovered some peculiar behavior. As you have outlined here, and as we have done in the past, we fire the syncing hit on the iframe as a pageview. In some scenarios, we have not wanted to effect the bounce rate, so we wanted to fire the syncing hit as a non-interaction event. Although the cookies on the browser between the parent page and the iframe match, we have seen some peculiar behavior on the session in GA (source/medium not preserved, multiple sessions in the user explorer, inaccurate session counts, etc.)

    I’d love to pick your brain on what effect a non-interaction hit would have instead of pageview and if non-interaction events can’t be used reliably, what could be done to preserve the bounce rate.

    • There is a post in the GACP partner forums as well with a bit more info…

  • Samses5th

    So. We are building a reservation system product. This will be an iFrame that customer installs to their website. Would conversion tracking work if we have a field for e.g. the customers Facebook pixel or AdWords Conversion Tag in the system back end? After we add it to a thank you -page. Or can we somehow add some event to the “make reservation” -button without too much hassle for the customer?

    • Dan Wilkerson

      All GA tracking depends on sending the same Client ID to the same Property. If I were you I’d expose an API on the parent frame to bind to some events, postMessage them from the child frame, then redirect the user post-handling.

Contact Us.


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

Follow Us



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