Trouble with the javascript extention

Gold Contributor
Gold Contributor

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 

 

2 REPLIES 2

Trouble with the javascript extention

Tealium Expert
Tealium Expert

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

Trouble with the javascript extention

Gold Contributor
Gold Contributor

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 :)

 

 

Public