Skip to content

Commit

Permalink
honor cookies in gtag
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsalerno committed Jun 27, 2023
1 parent 2ff12ef commit f8fcd59
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ https://www.ericsalerno.com/

Not much to read here, sorry. This is just a github page on my vanity domain.

This website uses `ihavecookies.js` for GDPR cookies: https://github.com/ketanmistry/ihavecookies
## This website uses

* `jQuery` https://jquery.com/
* `js-cookie/js-cookie` https://github.com/js-cookie/js-cookie
* `ihavecookies.js` https://github.com/ketanmistry/ihavecookies
13 changes: 5 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ <h2>(Professional|Hobbyist|Freelance) (web|app|game) developer.</h2>
</div>
<div class="home-tier">
<div class="home-footer">
&copy;2020 Eric Salerno - <a href="https://www.salernolabs.com/privacy">Privacy Policy</a>
&copy;2020 Eric Salerno -
<a href="https://www.salernolabs.com/privacy">Privacy Policy</a> - <a href="javascript:;" id="update-preferences">Update Preferences</a>
</div>
</div>
</div>
Expand All @@ -52,16 +53,12 @@ <h2>(Professional|Hobbyist|Freelance) (web|app|game) developer.</h2>
src="https://code.jquery.com/jquery-3.7.0.min.js"
integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g="
crossorigin="anonymous"></script>
<script type="text/javascript" src="/resources/main.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js.cookie.min.js"></script>
<script type="text/javascript" src="/resources/jquery.ihavecookies.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-L4Q3RWN7DP"></script>
<script type="text/javascript" src="/resources/main.js"></script>
<!-- Google tag (gtag.js) -->
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-L4Q3RWN7DP');
</script>
</body>
</html>
31 changes: 29 additions & 2 deletions resources/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
/**
* Hello!
*/
$(document).ready(function() {
function debug(message) {
if (typeof console != 'undefined') {
console.log('Hello friend!');
console.log(message);
}
}
function initializeGa4(disableAnalyticsCookies = false) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

if (disableAnalyticsCookies) {
debug('gtag minus cookies');
gtag("consent", "default", {
ad_storage: "denied",
analytics_storage: "denied",
functionality_storage: "denied",
personalization_storage: "denied",
security_storage: "denied"
});
}

gtag('config', 'G-L4Q3RWN7DP');
}

$(document).ready(function() {
debug('Hello friend');

var options = {
title:"Cookies & Privacy",
message:"This website uses cookies to ensure you get the best experience on our website.",
Expand All @@ -16,4 +38,9 @@ $(document).ready(function() {
$('#update-preferences').click(function(){
$('body').ihavecookies(options, 'reinit');
});

var cookieControl = JSON.parse(Cookies.get('cookieControlPrefs') || "['analytics']");
var disallowAnalyticsCookies = cookieControl.includes('analytics') === false;

initializeGa4(disallowAnalyticsCookies);
});

0 comments on commit f8fcd59

Please sign in to comment.