PageSourceSearch

https://timwakeling.co.uk/wp-content/plugins/tw-tracking-consent/consent.js?ver=2026.02.07

js timwakeling.co.uk collected 2026-10-04 21:59:05 UTC 7,641 bytes, 248 lines download raw bytes

1// Calculate the site URL
2var siteURL = window.location.origin;
3
4// Cookie functions
5if ( typeof setCookie != 'function' ) {
6	function setCookie( cname, cvalue, exdays ) {
7		var d = new Date();
8		if ( typeof exdays == 'undefined' ) {
9			var exdays = 365; // 1 year if no other expiry is set
10		}
11		d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
12		var expires = "expires="+d.toUTCString();
13		document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/; SameSite=Lax";
14	}
15}
16if ( typeof getCookie != 'function' ) {
17	function getCookie( cname ) {
18		var name = cname + "=";
19		var ca = document.cookie.split(';');
20		for(var i = 0; i < ca.length; i++) {
21			var c = ca[i];
22			while (c.charAt(0) == ' ') {
23				c = c.substring(1);
24			}
25			if (c.indexOf(name) == 0) {
26				return c.substring(name.length, c.length);
27			}
28		}
29		return "";
30	}
31}
32
33// After the page has loaded, but not necessarily after all images and resources have loaded
34jQuery( document ).ready( function( $ ) {
35
36	// Get a list of active trackers (those which have an ID or token defined in Settings)
37	var activeTrackersData = $( '.tracking-consent-message' ).data( 'active-trackers' );
38	if ( typeof activeTrackersData !== 'undefined' && activeTrackersData.length > 0 ) {
39		trackers = activeTrackersData.split( ',' );
40	} else {
41		trackers = [];
42	}
43
44	// Full consent message for recording in DB
45	var consentMessage = $( '.tracking-consent-message .consent-message-text' ).html() + ' ' + $( '.tracking-consent-message .more-tracking-details-text' ).html();
46	
47	// Set tracking consent cookies if the user clicks the "Accept all" button
48	$( '.tracking-consent-message' ).on( 'click touch', '.accept-tracking', function() {
49
50		// Set cookies for all trackers to "accepted", and record this in the DB
51		trackers.forEach( function( tracker ) {
52			setCookie( 'consent_' + tracker, 'accepted' );
53			recordConsentInDB( tracker, consentMessage );
54		});
55
56		// Grant consent for all trackers
57		grantConsent( trackers );
58
59		// Hide the popup and show the para about withdrawing consent
60		$( '.tracking-consent-message' ).remove();
61		$( '.stop-using-tracking-cookies-para' ).show();
62
63	});
64
65	// Set the tracking consent cookie if the user clicks the "Reject all" button
66	$( '.tracking-consent-message' ).on( 'click touch', '.reject-tracking', function() {
67
68		// Set cookies for all trackers to "rejected"
69		trackers.forEach( function( tracker ) {
70			setCookie( 'consent_' + tracker, 'rejected' );
71		});
72
73		// Hide the popup
74		$( '.tracking-consent-message' ).remove();
75
76	});
77
78	// Set the appropriate cookies if the user submits the form with individual tracker choices
79	$( document ).on( 'submit', '.individual-tracking-consent-form', function( e ) {
80
81		e.preventDefault();
82
83		var selected = [];
84
85		trackers.forEach( function( tracker ) {
86			
87			var isChecked = $( e.target ).find( 'input[name*="' + tracker + '"]:checked' ).length;
88
89			if ( isChecked ) {
90
91				setCookie( 'consent_' + tracker, 'accepted' );
92				recordConsentInDB( tracker, consentMessage );
93				selected.push( tracker );
94
95			} else {
96
97				setCookie( 'consent_' + tracker, 'rejected' );
98
99			}
100
101		});
102
103		// Grant the consent for the selected trackers
104		grantConsent( selected );
105
106		// Hide the popup and show the para about withdrawing consent
107		$( '.tracking-consent-message' ).remove();
108		$( '.stop-using-tracking-cookies-para' ).show();
109
110	});
111	
112	// More details link
113	$( '.tracking-consent-message' ).on( 'click touch', '.show-more-tracking-details', function(e) {
114
115		e.preventDefault();
116		$( '.more-tracking-details' ).slideToggle();
117		$( '.show-more-tracking-details span' ).toggle();
118		$( '.show-more-tracking-details' ).toggleClass( 'expanded' );
119
120	});
121
122	// Grant consent automatically if any of the individual cookies are set to "accepted"
123	var acceptedTrackers = [];
124	var rejectedTrackers = [];
125	var missingPreferences = [];
126
127	// Loop through them and check each cookie
128	trackers.forEach( function( tracker ) {
129		var consent = getCookie( 'consent_' + tracker );
130		if ( consent === 'accepted' ) {
131			acceptedTrackers.push( tracker );
132		} else if ( consent === 'rejected' ) {
133			rejectedTrackers.push( tracker );
134		} else {
135			missingPreferences.push( tracker );
136		}
137	});
138
139	// Grant consent for whichever ones were accepted
140	if ( acceptedTrackers.length > 0 ) {
141		grantConsent( acceptedTrackers );
142		// Show the "stop using tracking cookies" para
143		$( '.stop-using-tracking-cookies-para' ).show();
144	}
145
146	// If we don't have a preference for all trackers yet, show the message
147	if ( missingPreferences.length > 0 ) {
148		jQuery( '.tracking-consent-message' ).show();
149	}
150	
151	// Revoke permission to track using a button with "stop-setting-cookies" class
152	$( document ).on( 'click touch', '.stop-using-tracking-cookies', function(e) {
153		
154		e.preventDefault();
155		
156		// Set all tracking cookies to "rejected"
157		trackers.forEach( function( tracker ) {
158			setCookie( 'consent_' + tracker, 'rejected' );
159		});
160		
161		// Replace the message with text saying we've done this
162		$( '.stop-using-tracking-cookies-para' ).html( 'Consent withdrawn. We will no longer set tracking cookies, or read the ones already set. We have set essential cookies to remember this preference. If you want to delete existing cookies, go to your browser settings, where the option to clear cookies is usually under "Privacy".' );
163		
164	});
165	
166});
167
168function recordConsentInDB( tracker, consentMessage ) {
169
170    jQuery.ajax({
171        type: 'POST',
172        dataType: 'json',
173        url: siteURL + '/wp-admin/admin-ajax.php',
174        data: {
175            action: 'record_cookie_consent',
176            consent_for: tracker,
177            consent_message: consentMessage
178        },
179        success: function( result ) {
180
181            if ( typeof result !== 'undefined' && result == true ) {
182                console.log( 'Recorded consent for ' + tracker );
183            } else {
184                console.log( 'Problem recording consent for ' + tracker );
185            }
186
187        },
188        error: function ( xhr, ajaxOptions, thrownError ) {
189            console.log( 'Problem recording consent for ' + tracker );
190            console.log( xhr.status );
191            console.log( thrownError );
192        }
193    });
194
195}
196
197function grantConsent( selected ) {
198
199    // If nothing passed, treat as "accept all"
200    var trackers = selected || [ 'google_analytics', 'google_ads', 'meta_pixel', 'zoho_salesiq' ];
201
202    // Google Analytics
203    if ( trackers.includes( 'google_analytics' ) ) {
204        gtag( 'consent', 'update', {
205            'analytics_storage': 'granted'
206        });
207    }
208
209	// Google Ads
210	if ( trackers.includes( 'google_ads' ) ) {
211		gtag( 'consent', 'update', {
212			'ad_user_data': 'granted',
213			'ad_personalization': 'granted',
214			'ad_storage': 'granted'
215		});
216	}
217
218    // Meta Pixel
219    if ( trackers.includes( 'meta_pixel' ) ) {
220        fbq( 'consent', 'grant' );
221    }
222
223    // Zoho SalesIQ
224	if ( trackers.includes( 'zoho_salesiq' ) ) {
225
226		// Get the Zoho SalesIQ widget code from the data attribute
227		var zohoToken = jQuery( '.tracking-consent-message' ).data( 'zoho_wc_token' );
228		if ( typeof zohoToken === 'undefined' || zohoToken.length === 0 ) {
229			console.log( 'Zoho SalesIQ widget token not found in data attribute' );
230			return;
231		}
232
233		if ( window.__zoho_loaded ) return; // prevent double loading
234		window.__zoho_loaded = true;
235
236		var d = document;
237		var s = d.createElement('script');
238		s.type = 'text/javascript';
239		s.id = 'zsiqscript';
240		s.defer = true;
241		s.src = 'https://salesiq.zohopublic.eu/widget?wc=' + zohoToken;
242		d.getElementsByTagName('head')[0].appendChild(s);
243
244	}
245
246    console.log( 'Granted tracking consent for ', trackers.join( ', ' ) );
247
248}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.