Reputation: 21
I am trying to create a sandbox where I can check if a tracking pixel has successfully executed or not.
Essentially, a successful pixel fire creates a request for an external JavaScript file. When this script runs correctly it creates this:
<script async="true" type="text/javascript" src="a.adroll.com/j/roundtrip.js"></script>
How do I go about detecting that? Any advice?
Upvotes: 1
Views: 2256
Reputation: 707956
If you're trying to detect whether a tracking pixel has been added to your page and successfully loaded, you can find it in the DOM and check if the image has completed loading with the .complete
property.
If you're trying to detect whether some external javascript has been called, you can't do that directly unless the code leaves some state around that you can interrogate indicating it's been called or perhaps hacking the code in question before it's called to replace it with your own function that can log it being called and then call the original function.
Without more details about your problem (the actual code and HTML), we can't offer more specifics.
I see you added a little more info in a comment. If you want to know if a specific script has been loaded, then you can look at that script and see what global symbols it defines and test for the existence of one of those global symbols. If they are not defined, then the script has not been loaded yet. If they are defined, then it has.
For example, if the script (roundtrip.js) defines a global function named __adroll
(which it looks like that script does), you can test for its existence with:
if (window.__adroll) {
// symbol __adroll is defined
}
To test for the existence of this variable in an iframe on the same domain as your host page, you would have to get the window object for that iframe. Using id="iframe"
from your code example, you could do that like this:
var iframe = document.getElementById("iframe_");
var iframWin = iframe.contentWindow || iframe.contentDocument;
if (iframeWin && iframeWin.__adroll) {
// symbol __adroll is defined in the iframe
} else {
// symbol __adroll is either not defined in the iframe or
// the iframe window is not accessible for cross-domain security reasons
}
Upvotes: 2