Test
- (optional) Edit code to have custom domain
- Paste code into console on any page
- Select iframe in console, and paste same code.
- Click reload in iframe, and repeat step 3.
Test
| (function(){ | |
| var ENABLE_FIX = false, | |
| prefix = 'SEER_SameSite_Test_', | |
| kTrackingIndex = prefix+'TrackerIndex', | |
| //trackerIndex = window[prefix+'TrackerIndex'] = window[prefix+'TrackerIndex'] || 1, | |
| isInIframe = window !== window.top; | |
| window[kTrackingIndex] = window[kTrackingIndex] || 0; | |
| var setGaCookie = function( name, domain, clientId ){ | |
| var val = 'GA1.'+(domain.split(".").length)+'.'+clientId; | |
| var expires = new Date(); | |
| expires.setFullYear(expires.getFullYear()+2); | |
| document.cookie = name+'='+val+'; expires='+expires.toUTCString()+'; domain=.'+domain+'; path=/; SameSite=None; Secure'; | |
| } | |
| var customTask = function(model){ | |
| try{ | |
| var ga = window[window['GoogleAnalyticsObject']], | |
| tracker = ga.getByName(model.get('name')), | |
| //origSendHitTask = model.get('sendHitTask'), | |
| linkerParam = tracker.get('linkerParam'), | |
| CID = model.get('clientId'), | |
| cookieName, cookieDomain; | |
| appendText( CID ? 'CID: '+CID : 'No client id!' ); | |
| if( CID ){} | |
| if( isInIframe ){ | |
| if( window.name == CID ){ | |
| appendText( 'CID persisted in iframe' ); | |
| document.body.style.backgroundColor = 'green'; | |
| cookieName = model.get('cookieName') || '_ga'; | |
| cookieDomain = model.get('cookieDomain'); | |
| if( cookieDomain=='auto' ) | |
| cookieDomain = (function(){ | |
| var i,h, | |
| tmp_cookie=cookieName+'_tld_check_tmp=cookie', | |
| hostname = document.location.hostname.split('.'); | |
| for(i=hostname.length-1; i>=0; i--) { | |
| h = hostname.slice(i).join('.'); | |
| document.cookie = tmp_cookie + ';domain=.' + h + ';'; | |
| if(document.cookie.indexOf(tmp_cookie)>-1){ | |
| // We were able to store a cookie! This must be it | |
| document.cookie = tmp_cookie.split('=')[0] + '=;domain=.' + h + ';expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
| return h; | |
| } | |
| } | |
| })(); | |
| ENABLE_FIX && setGaCookie( cookieName, cookieDomain, CID ); | |
| }else{ | |
| appendText( 'CID NOT persisted' ); | |
| document.body.style.backgroundColor = 'red'; | |
| } | |
| appendText( '<a href="/reload-test/?noredirect">Reload</a>' ); | |
| }else{ | |
| addIframe( 'https://searchchurch.com/?noredirect&'+linkerParam, CID ); | |
| } | |
| }catch(ex){console.error('customTask',ex)} | |
| }; | |
| var getCookie = function(name) { | |
| var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)'); | |
| return v ? v[2] : null; | |
| } | |
| var appendText = function( text, style ){ | |
| window.console && console.debug && console.debug('['+prefix+window[kTrackingIndex]+']',text); | |
| var el = document.body; | |
| var newEl = document.createElement('p'); | |
| newEl.style="color:inherit;margin:5px;padding:0;"+ style;//( style?, style:''); | |
| //newEl.appendChild(document.createTextNode( text )); | |
| newEl.innerHTML = text; | |
| el.appendChild(newEl); | |
| } | |
| var showCid = function( prefix ){ | |
| appendText( (prefix?prefix+': ':'')+'_ga: '+getCookie('_ga') ); | |
| } | |
| var addIframe = function( url, CID ){ | |
| appendText( 'Iframe URL: <a href="'+url+'" target="_blank_'+url+'" style="color:inherit">'+url+'</a>', 'color:red;' ); | |
| var iframe = document.createElement('iframe'); | |
| iframe.id = url; | |
| iframe.src = url; | |
| iframe.name = CID; | |
| document.body.appendChild(iframe); | |
| //appendText( 'Iframe created.' ); | |
| } | |
| var HEREDOC = function( fn ){ | |
| return fn.toString().match( /\/\*[\n\l]?([\s\S]*?)\*\//m )[1]; | |
| }; | |
| var injectCss = function( css, removeLineComments ){ | |
| var el = document.createElement('style'); | |
| el.innerHTML = css.replace(/[\s\n]\/\/\s?(.*)$/mg, removeLineComments ? '' : '/* $1 */' ); | |
| document.head.appendChild( el ); | |
| } | |
| var gaAllTrackers = function(){ | |
| var ga = window[window['GoogleAnalyticsObject']]; | |
| var args = [].slice.apply(arguments); | |
| var cmd = args[0]; | |
| for( var i=0; i<window[kTrackingIndex]; i++ ){ | |
| args[0] = prefix+i+'.'+cmd; | |
| ga.apply( ga, args ); | |
| } | |
| } | |
| window[prefix] || (function(){ | |
| window[prefix] = 1; | |
| document.body.innerHTML = ''; | |
| injectCss( HEREDOC( function(){/* | |
| html { | |
| margin: 0 !important; | |
| } | |
| html body { | |
| margin: 0 !important; | |
| padding: 0 !important; | |
| font-size:14px !important; | |
| line-height:1 !important; | |
| color: black !important; | |
| background: white; | |
| font-family: monospace; | |
| } | |
| iframe{ | |
| position:static;height:150px;margin:0 5px 5px; width: 80%; | |
| border:1px solid red; | |
| } | |
| */})); | |
| (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
| (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
| m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
| })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
| appendText('Current URL: '+window.location.href); | |
| //showCid('Init'); | |
| //ga(function(){ showCid('ga()'); }); | |
| })(); | |
| (function(){ | |
| window["_gaUserPrefs"] = { ioo : function(){} }; // Disable GA opt-out | |
| ga('create', 'UA-11111-1', { | |
| name: prefix+(window[kTrackingIndex]++), | |
| allowLinker:true, | |
| //sendHitTask: customTask, | |
| }); | |
| gaAllTrackers('set','customTask',customTask); | |
| gaAllTrackers('send','pageview'); | |
| })(); | |
| })(); |
| var gaCustomTask = function(model){ | |
| // Set SameSite-compliant GA cookie inside secure iframes. | |
| // Based on analytics.js cookie format as of January 2020. | |
| // Details: https://seer.is/2Op2N66 | |
| if( window !== window.top && location.protocol == 'https:' ){ | |
| (function(domain){ | |
| if( 'auto' == ( domain = model.get('cookieDomain') ) ) | |
| domain = (function getTLD(){ | |
| var cookieName = '_seer_gtm_fw_detect_tld', globalNamespacePageloadCache = cookieName; | |
| return window[globalNamespacePageloadCache] || ( window[globalNamespacePageloadCache] = (function(){ | |
| var i, domain, parts = location.hostname.split('.'), cKeyValPair, c; | |
| for( i=parts.length-1; i>=0; i-- ){ | |
| domain = parts.slice(i).join('.'); | |
| cKeyValPair = cookieName+'_'+domain+'=1'; | |
| document.cookie = c = cKeyValPair+';domain=.'+domain+';path=/; SameSite=None; Secure'; | |
| if( document.cookie.indexOf(cKeyValPair)>-1 ){ | |
| document.cookie = c+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
| return domain; | |
| } | |
| } | |
| return location.hostname; | |
| })()); | |
| })(); | |
| document.cookie = model.get('cookieName') | |
| +'=GA1.'+(domain.split(".").length)+'.'+model.get('clientId') | |
| +'; expires='+(new Date( +new Date() + model.get('cookieExpires')*1e3 )).toUTCString() | |
| +'; domain=.'+domain+'; path=/; SameSite=None; Secure'; | |
| })(); | |
| } | |
| }; |