{"id":9644,"date":"2023-08-13T11:22:13","date_gmt":"2023-08-13T09:22:13","guid":{"rendered":"https:\/\/via-internet.de\/blog\/?p=9644"},"modified":"2023-08-13T15:56:26","modified_gmt":"2023-08-13T13:56:26","slug":"daily-nicepage-add-ga-and-cookies","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2023\/08\/13\/daily-nicepage-add-ga-and-cookies\/","title":{"rendered":"Daily: Nicepage &#8211; Add GA and Cookies"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/nicepage.com\/\" data-type=\"link\" data-id=\"https:\/\/nicepage.com\/\">Nicepage<\/a> is an awesome Web Page Builder with 400+ Features and 10000+ Templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the features is to connect to Google Analytics, another is a Cookie Consent Plugin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unfortunately, i didn&#8217;t succeed in getting both together work. The goal was, only to load the GA code if the consent plugin allowed this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So i created a workaround:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>i didn&#8217;t configure the Google Tag Manger in the Settings<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"341\" src=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.37.31-1024x341.png\" alt=\"\" class=\"wp-image-9652\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.37.31-1024x341.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.37.31-300x100.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.37.31-768x255.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.37.31-1536x511.png 1536w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.37.31.png 1912w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>i add custom code to check cookie state and load Google Tag Manager<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add HTML Code to Site:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"461\" src=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.19.49-1024x461.png\" alt=\"\" class=\"wp-image-9645\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.19.49-1024x461.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.19.49-300x135.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.19.49-768x346.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.19.49-1536x692.png 1536w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.19.49.png 1914w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script>    \n\twindow.dataLayer = window.dataLayer || [];\n    function gtag(){\n    \tconsole.log('gtag: arguments=', arguments);\n    \tdataLayer.push(arguments);\n    }\n    \n    function setupGoogleTagManager(w,d,s,l,i){\n        console.log('google tag manager: setup with', i);\n        \n        w[l]=w[l]||[];\n        w[l].push({'gtm.start': new Date().getTime(), event:'gtm.js'});\n        var f=d.getElementsByTagName(s)[0],\n            j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';\n        \n        j.async=true;j.src='https:\/\/www.googletagmanager.com\/gtm.js?id='+i+dl;\n        f.parentNode.insertBefore(j,f);\n    }\n    \n\t\n\tfunction setCookie(cname, cvalue, exdays) {\n  \t\tconst d = new Date();\n  \t\td.setTime(d.getTime() + (exdays*24*60*60*1000));\n  \t\tlet expires = \"expires=\"+ d.toUTCString();\n  \t\tdocument.cookie = cname + \"=\" + cvalue + \";\" + expires + \";path=\/\";\n\t}\n\t\n    function getCookie(cname) {\n      let name = cname + \"=\";\n      let decodedCookie = decodeURIComponent(document.cookie);\n      let ca = decodedCookie.split(';');\n      for(let i = 0; i &lt;ca.length; i++) {\n        let c = ca[i];\n        while (c.charAt(0) == ' ') {\n          c = c.substring(1);\n        }\n        if (c.indexOf(name) == 0) {\n          return c.substring(name.length, c.length);\n        }\n      }\n      return \"\";\n    }\n    \n    function buildConsentCookie(consent) {\n\t\tconsole.log('buildConsentCookie: consent=', consent);\n\t    const consentCookie = {\n    \t    'functionality_storage': consent.necessary ? 'granted' : 'denied',\n        \t'security_storage': consent.necessary ? 'granted' : 'denied',\n        \t'ad_storage': consent.marketing ? 'granted' : 'denied',\n        \t'analytics_storage': consent.analytics ? 'granted' : 'denied',\n        \t'personalization': consent.preferences ? 'granted' : 'denied',\n    \t};\t\n        \n    \tconsole.log('buildConsentCookie: consentCookie=', consentCookie);\n    \treturn consentCookie\n    }\n    \n    function setConsent(consent) {\n    \tconsole.log('setConsent: ', consent);\n    \tconst consentCookie = buildConsentCookie(consent);\n\t\tgtag('consent', 'default', consentCookie);  \n    \n\t\tconsole.log('store consentMode: ',consentCookie);\n    \tlocalStorage.setItem('consentMode', JSON.stringify(consentCookie));\n    }\n    \n    if(localStorage.getItem('consentMode') === null){\n    \tconst consent = {\n    \t    necessary: false,\n    \t    marketing: false,\n    \t    analytics: false,\n    \t    preferences: false,\n    \t};\n        consentCookie = buildConsentCookie(consent);\n    \tgtag('consent', 'default', consentCookie);\n    } else {\n    \tconsentCookie = JSON.parse(localStorage.getItem('consentMode'));\n    \tgtag('consent', 'default', consentCookie);\n    }\n&lt;\/script><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add HTML Code to Cookies Section<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.20.42-1024x502.png\" alt=\"\" class=\"wp-image-9646\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.20.42-1024x502.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.20.42-300x147.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.20.42-768x376.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.20.42-1536x753.png 1536w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.20.42.png 1918w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const consent = {\n\tnecessary: true,\n\tmarketing: true,\n\tanalytics: true,\n\tpreferences: true,\n};\n\nsetCookie('u-gdpr-cookie', true, 180);\nsetConsent(consent);\nsetupGoogleTagManager(window,document,'script','dataLayer','GTM-#######');<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Nicepage is an awesome Web Page Builder with 400+ Features and 10000+ Templates. One of the features is to connect to Google Analytics, another is a Cookie Consent Plugin. Unfortunately, i didn&#8217;t succeed in getting both together work. The goal was, only to load the GA code if the consent plugin allowed this. So i created a workaround: Add HTML Code to Site: Add HTML Code to Cookies Section<\/p>\n","protected":false},"author":1,"featured_media":9648,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1,156],"tags":[],"class_list":["post-9644","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allgemein","category-nicepage"],"jetpack_featured_media_url":"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2023\/08\/Bildschirmfoto-2023-08-13-um-11.23.17.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=9644"}],"version-history":[{"count":6,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9644\/revisions"}],"predecessor-version":[{"id":9658,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9644\/revisions\/9658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media\/9648"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=9644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=9644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=9644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}