Unable to trigger Analytics from js | Community
Skip to main content
September 27, 2017

Unable to trigger Analytics from js

  • September 27, 2017
  • 1 reply
  • 3453 views

Hi, I am trying to implement Adobe Analytics in my website which is run by CMS (Adobe Experience Manager to be specific), and I have built a component which user can place into the website content and then choose whether to generate the Staging or Production code, and upon selection the component will append the script to the header and footer.

The script which does the appending looks like this:

<script>

     var analytics = document.createElement('script');

     analytics.src ='//assets.adobedtm.com/someID-staging.js';

     document.head.prepend(analytics);

</script>

and similar for the footer portion.

However, even though the script is seen as added properly to the <head> tag, Adobe Debugger is not detecting it.

Please help.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Adobe Employee
September 27, 2017

There are 2 things which might be causing this:

1) Important thing here is the time at which this dynamic addition of the script is happening. Please ensure this script gets loaded  on the page well before, or, at the time when DOM gets created. Something like document.ready() in Jquery, or, something like below  if using plain Javascript:

document.addEventListener('DOMContentLoaded', function(){
  
// your code goes here
}, false);

2) Try appending http:// as the prefix to the DTM Embed URL you are using.

analytics.src ='http://assets.adobedtm.com/someID-staging.js';

chuaaaAuthor
September 28, 2017

I think 1) is most likely the issue. But I think I might doing it wrong in that if even I put it like the below in the <body>:

document.addEventListener('DOMContentLoaded', function(){

var analytics = document.createElement('script');

analytics.src ='http://assets.adobedtm.com/someID-staging.js';

document.head.prepend(analytics);

}, false);

...The Analytics script still does not get executed because it's essentially added to the <head> after head has already been loaded. It's more about how to get DOM to execute the Analytics script after it's appended.