Highlighted

Launching utag.view in Javascript extension

Silver Contributor
Silver Contributor

Hi guys,

I am using a utag.view on a Javascript extension to track page views based on clicks on a page that does not reload (so it's dynamic) , and it works fine the first time you click an element.
On the other hand, if you do more clicks, you get 2, 4, 8... page views for each click. I assume the thing here is that the utag.link, as it loads all extensions, is adding more listeners to the same elements that cause these multiple page views. Here is an example of my approach:

jQuery(document).on("mousedown","#texample", function(){
utag.view({
Page_Name: 'example',
});
});

Is there any way of avoiding this to happen?

Thanks a lot in advance.

Best regards,

Martin McNulty

3 REPLIES 3
Highlighted

Launching utag.view in Javascript extension

Tealium Employee
Hi Martin, Your best bet is to wrap the jQuery in an if statement. Depending on which scope the extension is in you can do something like the following:
utag.runonce = utag.runonce || {};
if (!utag.runonce["ext_mousedown"]) {
  utag.runonce["ext_mousedown"] = 1;
  jQuery(document).on("mousedown","#texample", function(){
    utag.view({Page_Name: 'example'});
  });
}
Adrian
Ask me anything Tealium related or Javascript, or NodeJS.
Please remember to mark solutions as accepted for future searchers.
Highlighted

Launching utag.view in Javascript extension

Silver Contributor
Silver Contributor
Hello Adrian, Thanks a lot for your quick response. It worked!! Martin
Highlighted

Launching utag.view in Javascript extension

Employee Emeritus

Martin,

 

The other option is to simply scope the extension to DOM Ready, instead of All Tags. The DOM Ready scope already has the "if statement" logic built into it so it won't repeatedly add the event listeners.