Onetrust?作為一個(gè)比較通用的cookie policy解決方案擁有比較好的開箱即用的功能,雖然是收費(fèi)的,但是用著也不錯(cuò),接下來就介紹下使用onetrust過程中遇到的一些問題已經(jīng)如何集成gtm
一:使用restrict tag deployment
1.掃描需要配置Onetrust的網(wǎng)站(onetrust會(huì)根據(jù)提供的域名自動(dòng)掃描頁面上的cookie已經(jīng)頁面上其他鏈接對(duì)應(yīng)頁面的cookie)

2. 進(jìn)入掃描完畢的域名并且獲取對(duì)應(yīng)的鏈接(包含token,可以直接使用onetrust提供的cdn或者下載后放在自己的cdn上):
<script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js"?type="text/javascript"?charset="UTF-8"?data-domain-script="xxxxxxx"></script>
3. 將腳本引入,并且不再直接加載gtm(使用腳本動(dòng)態(tài)加載)
var?dataLayer = window.dataLayer ? window.dataLayer:[];?// initializing dataLayer
function?OptanonWrapper() {
????????????????var?allowList = ['google'];?//默認(rèn)允許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=<placeholder>m_preview=env-5>m_cookies_win=x';
????????????????f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','<placeholder>');
????????????}
????????}
二:使用gtm trigger
1. 定義variable并且應(yīng)用到triggers(OnetrustActiveGroups)

2.創(chuàng)建一個(gè)trigger (OneTrustGroupsUpdated) 并且設(shè)置匹配規(guī)則(C0002被選中就觸發(fā))

3.創(chuàng)建tag

4. 選擇一個(gè)trigger

5.一些自定義的腳本可以直接動(dòng)態(tài)注入

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