Expand my Community achievements bar.

SOLVED

Page load rule and data element question

Avatar

Level 6

I created a script that identifies if a visitor's browser is using adblocker. I added the script to the page load rule (top of page) and it works and returns a console.log for the return statement. However, how would I capture the return statement in a data element? I tried creating a data element with custom code to "return adBlockDetection;" but that didn't seem to work?

Am I missing something?

code added to the custom page code in the page load rule (top of page):

var adBlockDetection = (function() {
var test = document.createElement('div');
  test.innerHTML = ' ';
  test.className = 'adsbox';
  document.body.appendChild(test);
   if (test.offsetHeight === 0) {
     console.log("AdBlocker Status: Active");
     return "AdBlocker Status: Active";
    } else {
     console.log("AdBlocker Status: None");
     return "AdBlocker Status: None";
    }
})();

1 Accepted Solution

Avatar

Correct answer by
Level 6

Ok, I got it working and thanks for pointing out the _satellite.getVar("DATA_ELEMENT_NAME"); as it is very handy when testing.

I just needed the return statement outside of the function and it works perfect.

var adBlockDetection = (function() {
var test = document.createElement('div');
  test.innerHTML = ' '; 
  test.className = 'adsbox'; 
  document.body.appendChild(test); 
   if (test.offsetHeight === 0) {
     console.log("AdBlocker Status: Active");
     return "AdBlocker Status: Active";
    } else {
     console.log("AdBlocker Status: None");
     return "AdBlocker Status: None";
    }
})();

return adBlockDetection;

 

Question: If I add it to load at "top of page" then it triggers as page is loaded versus "bottom of page" is waits until page has fully loaded?

thanks again for the help,

Scott

View solution in original post

3 Replies

Avatar

Level 10

Hi,

Using a data element would be the correct approach. You can place your code in the custom code section and the code will run once the data element is called. If you wanted this code to run on every page, you could place the data element inside one of your DTM tools in the global variables, or you could use a page load rule. 

With that being said, you mentioned that this method doesn't seem to be working. What issue are you facing? Does the data element return a value when called from the console? You can use the command below in the console to call the data element manually.

_satellite.getVar("DATA_ELEMENT_NAME");

 

When you use this command, does the data element return the value you'd expect?

 

Cheers,
Jantzen

Avatar

Correct answer by
Level 6

Ok, I got it working and thanks for pointing out the _satellite.getVar("DATA_ELEMENT_NAME"); as it is very handy when testing.

I just needed the return statement outside of the function and it works perfect.

var adBlockDetection = (function() {
var test = document.createElement('div');
  test.innerHTML = ' '; 
  test.className = 'adsbox'; 
  document.body.appendChild(test); 
   if (test.offsetHeight === 0) {
     console.log("AdBlocker Status: Active");
     return "AdBlocker Status: Active";
    } else {
     console.log("AdBlocker Status: None");
     return "AdBlocker Status: None";
    }
})();

return adBlockDetection;

 

Question: If I add it to load at "top of page" then it triggers as page is loaded versus "bottom of page" is waits until page has fully loaded?

thanks again for the help,

Scott

Avatar

Level 10

Glad to see you were able to get it working.

Yes, the top of page will load the script well before the bottom of page rule. You can see a lot more detail on load order in DTM in our documentation.

https://marketing.adobe.com/resources/help/en_US/dtm/load_order.html

 

Cheers,
Jantzen