Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda