External JS file reference in DTM | Community
Skip to main content
Level 3
December 7, 2016
Solved

External JS file reference in DTM

  • December 7, 2016
  • 6 replies
  • 4565 views

Hi,

We have a Event based rule that calls a function in an external JS file.  We want to add a reference to that JS file from with in DTM.  Is this possible and if so how/where should we add it?

Thanks in advance.

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

One solution would be to create a global page load rule that loads your external script at, for example, "Page Bottom". In the "JavaScript/Third Party Tags" section, add a new "Non-Sequential" script. Then use something like the following to append the library to the page:

var script = document.createElement('script'); script.src = 'http://example.com/js.js'; // your script URL script.onload = function () { //this is only necessary if you need to call something directly after the script loads }; document.head.appendChild(script); // adds external script to page

As long as your Event based rule will fire after this is loaded, you should be good to go.

6 replies

jantzen_b
Adobe Employee
Adobe Employee
December 7, 2016

Hi,

I'm not sure if it would be possible to reference an external js file within DTM. Alternatively, you could host your js script inside DTM in the third party tag section.

Thanks,
Jantzen

m_1985Author
Level 3
December 7, 2016

Thanks for the reply. I did tried like this but it didn't work.

jantzen_b
Adobe Employee
Adobe Employee
December 7, 2016

I'd recommend pulling the code out of the test.js file and placing it into the Third Party Script section directly. This would eliminate the need to reference an external script.

jantzen_b
Adobe Employee
Adobe Employee
December 13, 2016

Hi,

Does my last suggestion solve this issue? If so, would it be acceptable to mark that answer correct?

Thanks,
Jantzen

Steven_Long
Steven_LongAccepted solution
Level 2
December 15, 2016

One solution would be to create a global page load rule that loads your external script at, for example, "Page Bottom". In the "JavaScript/Third Party Tags" section, add a new "Non-Sequential" script. Then use something like the following to append the library to the page:

var script = document.createElement('script'); script.src = 'http://example.com/js.js'; // your script URL script.onload = function () { //this is only necessary if you need to call something directly after the script loads }; document.head.appendChild(script); // adds external script to page

As long as your Event based rule will fire after this is loaded, you should be good to go.

jantzen_b
Adobe Employee
Adobe Employee
December 15, 2016

Steven,

Thanks for the answer! Looks like this is what the original poster was looking for.

Cheers,
Jantzen