Onetrust 配合 google tag manager实现 cookie policy

Onetrust 作为一个比较通用的cookie policy解决方案拥有比较好的开箱即用的功能,虽然是收费的,但是用着也不错,接下来就介绍下使用onetrust过程中遇到的一些问题已经如何集成gtm

一:使用restrict tag deployment

1.扫描需要配置Onetrust的网站(onetrust会根据提供的域名自动扫描页面上的cookie已经页面上其他链接对应页面的cookie)

2. 进入扫描完毕的域名并且获取对应的链接(包含token,可以直接使用onetrust提供的cdn或者下载后放在自己的cdn上):


3. 将脚本引入,并且不再直接加载gtm(使用脚本动态加载)

var dataLayer = window.dataLayer ? window.dataLayer:[]; // initializing dataLayer

function OptanonWrapper() {

                var allowList = ['google']; //默认允许google以开启 restrict tag deployment;

                var tagObj = {

                    'gtm.allowlist':allowList,

                    'gtm.blocklist':['ua'] //Block ua since it'll generate the cookie related to GA

                }

                var supportList = OnetrustActiveGroups.split(','); //OnetrustActiveGroups is a global variable provided by onetrust which you can get access to it after script is loaded

                if(supportList.indexOf("C0002")>=0){ //performance cookie

                        //Performance

                        allowList.push('hjtc');

                        allowList.push('html');

                        tagObj['gtm.blocklist']=[];  //Remove 'ua' from block list if user agrees on performance cookie

                        loadElq();

                }else{

                        tagObj['gtm.blocklist'].push('hjtc'); //If user doesn't agree on performance cookie, put hotjar and qualtrics into blocklist

                        tagObj['gtm.blocklist'].push('html');

                }

                if(supportList.indexOf("C0004")>=0){ //advertising cookie       

                    allowList.push('bzi');

                }else{

                    tagObj['gtm.blocklist'].push('bzi');

                }

                if(supportList.indexOf("C0005")>=0){ //social media cookie

                }

            dataLayer.push(tagObj);

            loadGtm(); //Then finally we should loadGTM

        }

      function loadGtm(){

            if(document.querySelector("#gtmid")==null){

                (function(w,d,s,l,i)

                {w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});

                var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';

                j.async=true;j.id='gtmid';j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl+ '>m_auth=m_preview=env-5>m_cookies_win=x';

                f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','');

            }

        }

二:使用gtm trigger

1. 定义variable并且应用到triggers(OnetrustActiveGroups)


2.创建一个trigger (OneTrustGroupsUpdated) 并且设置匹配规则(C0002被选中就触发)


3.创建tag


4. 选择一个trigger


5.一些自定义的脚本可以直接动态注入


REF

https://ntsanov.com/blog/integrate-google-tag-manager-with-an-existing-cookie-consent-form

https://developers.google.com/tag-manager/web/restrict?hl=nb

你可能感兴趣的:(Onetrust 配合 google tag manager实现 cookie policy)