This post explains the approach to use Google Tag Manager(GTM) to include the dynamic scripts as part of Adobe Experience Manager(AEM) websites.
Create a free google tag manager account if one is not available
Install the GTM header and body scripts to the AEM website
Copy the head script to the header file and body script to body footer(start of body section)
Define new tag:
Select the Tag Configuration as Custom HTML and enter the name for tag
Enter the required custom script into the tag configuration - script file or the script text can be added(while adding the script text, please note the special characters should be escaped)
<script>
var s = document.createElement("script");
s.innerHTML = "alert('Custom Script from GTM');"; // add the custom script content
//s.src="test.js";// specify the script file instead of script content
document.head.appendChild(s);
</script>
Adding multi line script - example
s.innerHTML = "document.onreadystatechange = function(){ "+
"if(document.readyState=='loaded' || document.readyState=='complete') "+
"alert('test');"+
"}";
Add Trigger
Select All Pages(Configure the trigger based on the requirement)
Select the tag firing option under Advanced Settings as Once per Page
Save the configuration
Preview the configurations:
The GTM Debugger displays the tags details on preview mode.
Now the script is included in the website
Publish the tag after successful testing
No comments:
Post a Comment
If you have any doubts or questions, please let us know.