Last active
October 28, 2019 13:49
-
-
Save thyngster/001737e8f2b2c75e59741a87be10bb7b to your computer and use it in GitHub Desktop.
Small Snippet to parse Macros on GTM Debug Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Small Snippet to Parse GTM Macros on GTM Debug Interface | |
// Idea From Simo's Post: #GTMTips: The Mysterious .macro() Call In Custom HTML Tags | |
// URL: https://www.simoahava.com/gtm-tips/mysterious-macro-call-custom-html-tags/ | |
// Current Version, does not take care of current variable type, all is replaces as str. | |
(function() { | |
const iframe = document.querySelector('iframe[src="about:blank"]'); | |
iframe.contentDocument.querySelectorAll('.CodeMirror-code').forEach(function(e) { | |
if (e.querySelectorAll('.CodeMirror-line').length > 0) { | |
let _code = [...e.querySelectorAll('pre.CodeMirror-line')].map(function(e) { | |
return e.textContent; | |
}).join(''); | |
let match = _code.match(/google_tag_manager\[\"[A-Z]*\-[A-Z0-9]*\"\]\.macro\([0-9*]{1,5}\)/); | |
if (match) { | |
const regex = /google_tag_manager\[\"[A-Z]*\-[A-Z0-9]*\"\]\.macro\([0-9*]{1,5}\)/gm; | |
let str = _code; | |
let m; | |
while ((m = regex.exec(str)) !== null) { | |
if (m.index === regex.lastIndex) { | |
regex.lastIndex++; | |
} | |
m.forEach((match)=>{ | |
const value = eval("parent." + match); | |
str = str.replace(match, `"${value}"`); | |
} | |
); | |
} | |
iframe.contentDocument.querySelectorAll('.CodeMirror-code')[0].innerText = str; | |
} | |
} | |
}); | |
} | |
)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment