Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Pass custom JS variable from ifram to the parent frame GTM

I have looked everywhere for the answer, whether on stackoverlfow or elsewhere I have never succeeded. Currently I have a site that hosts an iframe (2 different domains). Actually I can send custom event from iframe to parent who work well. But now it's not an event that I want to send but the value of a variable.

Currently in the iframe I have string which is dynamically generated that I get in a custom JS variable

Now I would like to send this variable to my parent page so that it can be used as a label in Analytics.

An example on how my events work for the parent / iframe :

IFRAME:

  try {
      var postObject = JSON.stringify ({
        event: 'iframeSave'
      });
      parent.postMessage (postObject, 'https: //www.iframe.en/');
} catch (e) {
  window.console && window.console.log (e);
}

PARENT LISTENER

(function (window) {

    addEvent (window, 'message', function (message) {
      try {
      var data = JSON.parse (message.data);
      var dataLayer = window.dataLayer || (window.dataLayer = []);
      if (data.event) {
        dataLayer.push ({
          'event': data.event,
          'postMessageData': data
        });
      }
      } catch (e) {}
    });

    // Cross-browser event listener
    function addEvent (el, evt, fn) {
      if (el.addEventListener) {
        el.addEventListener (evt, fn);
      } else if (el.attachEvent) {
        el.attachEvent ('on' + evt, function (evt) {
          fn.call (el, evt);
        });
      } else if (typeof el ['on' + evt] === 'undefined' || el ['on' + evt] === null) {
        el ['on' + evt] = function (evt) {
          fn.call (el, evt);
        };
      }
    }

  }) (window);

and the custom event trigger for my GA tag on the parent page: iframeSave

Thank you

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!