-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.tsx
69 lines (61 loc) · 2.79 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { ReactElement, useContext } from 'react'
import Script from 'next/script'
import * as PiwikPro from '@piwikpro/react-piwik-pro'
import { PiwikProProps } from '../types'
import { isValidUrl } from '../functions/isValidUrl'
const PiwikProContext = React.createContext(PiwikPro)
export const PiwikProProvider: React.FC<PiwikProProps> = ({
accountName,
children,
containerId,
containerUrl,
nonce
}): ReactElement | null => {
if (!containerId) {
console.error('Empty container-id for Piwik Pro.')
}
if (accountName) {
console.warn(
'The accountName parameter is deprecated and will be removed in the future. '
)
}
if (!containerUrl) {
console.error('Empty container-url for Piwik Pro.')
}
if (!children) {
console.error(
'Was not possible to access Document interface. Make sure this module is running on a Browser with access to Document interface.'
)
}
const correctAccountUrl =
accountName && isValidUrl(accountName)
? accountName
: `https://${accountName}.containers.piwik.pro`
const correctContainerUrl = containerUrl || correctAccountUrl
return (
<>
<Script
id={`piwik-tag-manager-${containerId}`}
strategy='afterInteractive'
nonce={nonce}
>
{`
(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=/"}
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="${correctContainerUrl}/containers/"+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}');
`}
</Script>
<PiwikProContext.Provider value={PiwikPro}>
{children}
</PiwikProContext.Provider>
</>
)
}
export const usePiwikPro = (): typeof PiwikPro => {
return useContext(PiwikProContext)
}