- TLC Home Home
- Discussions Discussions
- Documentation Documentation
- Knowledge Base Knowledge Base
- Education Education
- Blog Blog
- Support Desk Support Desk
11-20-2018 11:57 PM - edited 11-21-2018 12:42 AM
Hi guys i'm having some trouble with the javascrip extention. I've tried sevral aproaches and different loadrules for the extention.
If i try the default one. My script seems to loop and starts sending a lot of calls. If i chose "run once" the script does nothing.
try{ var brytercontainer = document.querySelector('[id="klp-switch-111"]'); //finds the switch element i need eventlistener on var accordionelements = document.querySelectorAll(".klp-accordion__header");// finds all the accordion elements i need eventlisteners on } catch(error){ utag.DB("Error UID 153 " + "(" + error.name + ": " + error.message + ")"); } function klikkbrytercontainer(){ setTimeout(function(){ utag.link({ event_category: "BilAssistent", event_action: "Click", event_label: document.querySelector(".active").textContent, event_value: 0, })},50)} // setts time out to allow the element to change on click function klikksporsmaal(el) { utag.link({ event_category: "BilAssistent", event_action: el.childNodes[1].textContent, event_label: el.innerText.trim(), event_value: 0,}); } try{ brytercontainer.addEventListener("click", klikkbrytercontainer); accordionelements.forEach(el => {el.addEventListener("click", function(){klikksporsmaal(el)})}); } catch (error){ utag.DB("Error UID 153 " + "(" + error.name + ": " + error.message + ")"); }
this is my script as it is added to the javascript extention
There is no error messages posted to utag.DB
Solved! Go to Solution.
11-21-2018 02:15 AM
Hi @Bknapstad,
Try scoping the extension to DOM Ready, rather than All Tags. And then possibly put a bit more in the way of debugging into the code to track whether the event handlers are being registered, when the functions are being invoked etc., just to validate that it's all working and when.
Cheers,
James
11-21-2018 03:23 AM
Thank you.
The problem was that the elements i wanted to track was not loaded when i tried to define my vars.
I did'nt know I could scope to dom ready so i made my own dom checker and now it works, but ill try to set the scope instead.
I do need to up my debuging game :)
Copyright All Rights Reserved © 2008-2023