Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda