Skip to content

Commit

Permalink
Merge pull request #17 from PiwikPRO/feature/PPI-690-update-tag-manag…
Browse files Browse the repository at this point in the history
…er-snippet

Feature/ppi 690 update tag manager snippet
  • Loading branch information
lysy-vlc authored Dec 14, 2023
2 parents b728f75 + 38444cc commit 440d72f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ dist
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# ide
.idea
30 changes: 16 additions & 14 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { PiwikProWindow } from "../interfaces/piwikpro.window";
import { PiwikProWindow } from '../interfaces/piwikpro.window'

function init(containerId: string, containerUrl: string, nonce?: string) {
if (!containerId) {
console.error('Empty tracking code for Piwik Pro.');
return;
console.error('Empty tracking code for Piwik Pro.')
return
}

if (!containerUrl) {
console.error('Empty tracking URL for Piwik Pro.');
return;
console.error('Empty tracking URL for Piwik Pro.')
return
}

if (!document) {
console.error('Was not possible to access Document interface. Make sure this module is running on a Browser w/ access do Document interface.');
console.error(
'Was not possible to access Document interface. Make sure this module is running on a Browser w/ access do Document interface.'
)
}

const s: HTMLScriptElement = document.createElement('script');
s.async = false;
const s: HTMLScriptElement = document.createElement('script')
s.async = false
if (nonce) {
s.setAttribute("nonce", nonce);
s.setAttribute('nonce', nonce)
}
s.text = `(function(window, document, dataLayerName, id) {
window[dataLayerName]=window[dataLayerName]||[],window[dataLayerName].push({start:(new Date).getTime(),event:"stg.start"});var scripts=document.getElementsByTagName('script')[0],tags=document.createElement('script');
function stgCreateCookie(a,b,c){var d="";if(c){var e=new Date;e.setTime(e.getTime()+24*c*60*60*1e3),d="; expires="+e.toUTCString()}document.cookie=a+"="+b+d+"; path=/"}
function stgCreateCookie(a,b,c){var d="";if(c){var e=new Date;e.setTime(e.getTime()+24*c*60*60*1e3),d="; expires="+e.toUTCString();f="; SameSite=Strict"}document.cookie=a+"="+b+d+f+"; path=/"}
var isStgDebug=(window.location.href.match("stg_debug")||document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?14:-1);
var qP=[];dataLayerName!=="dataLayer"&&qP.push("data_layer_name="+dataLayerName),isStgDebug&&qP.push("stg_debug");var qPString=qP.length>0?("?"+qP.join("&")):"";
tags.async=!0,tags.src="${containerUrl}/containers/"+id+".js"+qPString,scripts.parentNode.insertBefore(tags,scripts);
tags.async=!0,tags.src="${containerUrl}/"+id+".js"+qPString,scripts.parentNode.insertBefore(tags,scripts);
!function(a,n,i){a[n]=a[n]||{};for(var c=0;c<i.length;c++)!function(i){a[n][i]=a[n][i]||{},a[n][i].api=a[n][i].api||function(){var a=[].slice.call(arguments,0);"string"==typeof a[0]&&window[dataLayerName].push({event:n+"."+i+":"+a[0],parameters:[].slice.call(arguments,1)})}}(i[c])}(window,"ppms",["tm","cm"]);
})(window, document, 'dataLayer', '${containerId}')`;
})(window, document, 'dataLayer', '${containerId}')`

const head: HTMLHeadElement = document.getElementsByTagName('body')[0];
head.appendChild(s);
const head: HTMLHeadElement = document.getElementsByTagName('body')[0]
head.appendChild(s)
}

export const IS_DEBUG =
Expand Down

0 comments on commit 440d72f

Please sign in to comment.