Skip to content

Instantly share code, notes, and snippets.

@johnbeadle
Created January 5, 2018 21:15
Show Gist options
  • Select an option

  • Save johnbeadle/0a95541fd5b9a442426f28a4b87ac051 to your computer and use it in GitHub Desktop.

Select an option

Save johnbeadle/0a95541fd5b9a442426f28a4b87ac051 to your computer and use it in GitHub Desktop.
all-in-one tag manager example
<script type="text/javascript">
window.lpTag = window.lpTag || {}; // REQUIRED to ensure lpTag {} exists prior to SDE push
window.lpTag.autoStart = false; // disable AUTOSTART
window.lpTag.site = "90233546"; // replace with your desired account number
window.lpTag=window.lpTag||{},"undefined"==typeof window.lpTag._tagCount?(window.lpTag={site:lpTag.site||"90233546",section:lpTag.section||"",tagletSection:lpTag.tagletSection||null,autoStart:lpTag.autoStart!==!1,ovr:lpTag.ovr||{},_v:"1.7.0",_tagCount:1,protocol:"https:",events:{bind:function(t,e,i){lpTag.defer(function(){lpTag.events.bind(t,e,i)},0)},trigger:function(t,e,i){lpTag.defer(function(){lpTag.events.trigger(t,e,i)},1)}},defer:function(t,e){0==e?(this._defB=this._defB||[],this._defB.push(t)):1==e?(this._defT=this._defT||[],this._defT.push(t)):(this._defL=this._defL||[],this._defL.push(t))},load:function(t,e,i){var n=this;setTimeout(function(){n._load(t,e,i)},0)},_load:function(t,e,i){var n=t;t||(n=this.protocol+"//"+(this.ovr&&this.ovr.domain?this.ovr.domain:"lptag.liveperson.net")+"/tag/tag.js?site="+this.site);var a=document.createElement("script");a.setAttribute("charset",e?e:"UTF-8"),i&&a.setAttribute("id",i),a.setAttribute("src",n),document.getElementsByTagName("head").item(0).appendChild(a)},init:function(){this._timing=this._timing||{},this._timing.start=(new Date).getTime();var t=this;window.attachEvent?window.attachEvent("onload",function(){t._domReady("domReady")}):(window.addEventListener("DOMContentLoaded",function(){t._domReady("contReady")},!1),window.addEventListener("load",function(){t._domReady("domReady")},!1)),"undefined"==typeof window._lptStop&&this.load()},start:function(){this.autoStart=!0},_domReady:function(t){this.isDom||(this.isDom=!0,this.events.trigger("LPT","DOM_READY",{t:t})),this._timing[t]=(new Date).getTime()},vars:lpTag.vars||[],dbs:lpTag.dbs||[],ctn:lpTag.ctn||[],sdes:lpTag.sdes||[],ev:lpTag.ev||[]},lpTag.init()):window.lpTag._tagCount+=1;
// ^^ replace 123456 in line 7 with your account number.
/*--- lpTag.start.loop ---*/
/*******************
Function to loop until the lpTag.start() function is found on the page and can be called to start the LivePerson monitoring
********************/
(function(){
var checkLivePersonTagLoopLimit = 10;
var checkLivePersonTagLoopCounter = 0;
var checkLivePersonTagExistsLoop = setInterval(function(){
if (lpTag && lpTag.start) {
lpTag.isDom = true; //Setting the isDom var to true because the dom already loaded
lpTag.start(); // TURN KEY -- start lpTag
clearInterval(checkLivePersonTagExistsLoop);
console.log('--> LivePerson ~~~ lpTag.start() called ...');
} else if (checkLivePersonTagLoopCounter >= checkLivePersonTagLoopLimit) {
console.log('--> LivePerson ~~~ checkLivePersonTagLoopCounter >= checkLivePersonTagLoopLimit');
clearInterval(checkLivePersonTagExistsLoop);
}
else {
console.log('--> LivePerson ~~~ lpTag.start not yet loaded on the page...');
checkLivePersonTagLoopCounter++;
}
},500);
})();
/*--- end start loop ---*/
</script>
@johnbeadle
Copy link
Author

This is an all in one example which could be loaded within a single "tag" within a tag manager such as GTM or TagCommander.
Because the timing of when the tag manager loads this could miss the crucial "DOMReady" event which the tag normally looks for, we have a loop which ensures the lpTag.start function exists as expected and then manually performs the turn-key operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment