mirror of
https://github.com/adityatelange/hugo-PaperMod.git
synced 2025-05-29 22:15:19 -04:00
add cookie banner
This commit is contained in:
parent
30a2cc7a65
commit
4759ecd011
4 changed files with 137 additions and 0 deletions
|
@ -163,7 +163,11 @@
|
|||
|
||||
{{- /* Misc */}}
|
||||
{{- if hugo.IsProduction | or (eq site.Params.env "production") }}
|
||||
{{- if eq .Site.Params.cookieEnabled true }}
|
||||
{{- template "partials/templates/cookie-consent.html" . }}
|
||||
{{- else }}
|
||||
{{- template "_internal/google_analytics.html" . }}
|
||||
{{- end -}}
|
||||
{{- template "partials/templates/opengraph.html" . }}
|
||||
{{- template "partials/templates/twitter_cards.html" . }}
|
||||
{{- template "partials/templates/schema_json.html" . }}
|
||||
|
|
75
layouts/partials/templates/cookie-consent.html
Normal file
75
layouts/partials/templates/cookie-consent.html
Normal file
|
@ -0,0 +1,75 @@
|
|||
<div id="cookie-notice">
|
||||
<span>{{- i18n "cookie_message" | default "I would like to use third party cookies and scripts to improve the functionality of this website." }}</span>
|
||||
<a id="cookie-notice-accept" class="btn btn-primary btn-sm">{{- i18n "cookie_approve" | default "Approve" }}</a>
|
||||
<a id="cookie-notice-deny" class="btn btn-primary btn-sm">{{- i18n "cookie_deny" | default "Deny" }}</a>
|
||||
<a href="{{ .Permalink }}privacy" class="btn btn-primary btn-sm">{{- i18n "cookie_more_info" | default "More Info" }}</a></div>
|
||||
<script>
|
||||
function createCookie(name,value,days) {
|
||||
var expires = "";
|
||||
if (days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + (days*24*60*60*1000));
|
||||
expires = "; expires=" + date.toUTCString();
|
||||
}
|
||||
document.cookie = name + "=" + value + expires + "; path=/";
|
||||
}
|
||||
function readCookie(name) {
|
||||
var nameEQ = name + "=";
|
||||
var ca = document.cookie.split(';');
|
||||
for(var i=0;i < ca.length;i++) {
|
||||
var c = ca[i];
|
||||
while (c.charAt(0)==' ') c = c.substring(1,c.length);
|
||||
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function eraseCookie(name) {
|
||||
createCookie(name,"",-1);
|
||||
}
|
||||
|
||||
if(readCookie('cookie-notice-option')=='true') {
|
||||
{{ if hugo.IsProduction | or (eq site.Params.env "production") }}
|
||||
|
||||
function loadScriptAsync(scriptSrc, callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Not a valid callback for async script load');
|
||||
}
|
||||
var script = document.createElement('script');
|
||||
script.onload = callback;
|
||||
script.src = scriptSrc;
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
|
||||
{{- if site.GoogleAnalytics }}
|
||||
let googleAnalyticsCode = {{ site.GoogleAnalytics }};
|
||||
{{- end }}
|
||||
|
||||
/* This is the part where you call the above defined function and "calls back"
|
||||
your code which gets executed after the script has loaded */
|
||||
//REPLACE WITH YOUR GA TAG!
|
||||
loadScriptAsync('https://www.googletagmanager.com/gtag/js?id=' + googleAnalyticsCode, function () {
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag() { dataLayer.push(arguments); }
|
||||
gtag('js', new Date());
|
||||
gtag('config', googleAnalyticsCode, { 'anonymize_ip': true }); //REPLACE WITH YOUR GA TAG!
|
||||
})
|
||||
|
||||
{{ end }}
|
||||
} else if (readCookie('cookie-notice-option')!='false'){
|
||||
document.getElementById('cookie-notice').style.display = 'block';
|
||||
}
|
||||
|
||||
document.getElementById('cookie-notice-accept').addEventListener("click",function() {
|
||||
createCookie('cookie-notice-option','true',31);
|
||||
document.getElementById('cookie-notice').style.display = 'none';
|
||||
location.reload();
|
||||
});
|
||||
|
||||
document.getElementById('cookie-notice-deny').addEventListener("click",function() {
|
||||
createCookie('cookie-notice-option','false',31);
|
||||
document.getElementById('cookie-notice').style.display = 'none';
|
||||
location.reload();
|
||||
});
|
||||
|
||||
</script>
|
||||
</div>
|
Loading…
Add table
Add a link
Reference in a new issue