Revert "Merge pull request #3238 from alphagov/cookies-update"

This reverts commit eec4bec761, reversing
changes made to 64480e2fff.
This commit is contained in:
Leo Hemsted
2020-01-15 14:40:48 +00:00
parent 04b9fa8927
commit 66db735e09
36 changed files with 116 additions and 1610 deletions

View File

@@ -1,62 +0,0 @@
(function (window) {
"use strict";
window.GOVUK = window.GOVUK || {};
// Stripped-down wrapper for Google Analytics, based on:
// https://github.com/alphagov/static/blob/master/doc/analytics.md
const Analytics = function (config) {
window.ga('create', config.trackingId, config.cookieDomain, config.name, { 'cookieExpires': config.expires * 24 * 60 * 60 });
window.ga('set', 'anonymizeIp', config.anonymizeIp);
window.ga('set', 'displayFeaturesTask', config.displayFeaturesTask);
window.ga('set', 'transport', config.transport);
};
Analytics.load = function () {
/* jshint ignore:start */
(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','//www.google-analytics.com/analytics.js','ga');
/* jshint ignore:end */
};
Analytics.prototype.trackPageview = function (path, title, options) {
// strip UUIDs
const page = (window.location.pathname + window.location.search).replace(
/[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}/g, '…'
);
window.ga('send', 'pageview', page);
};
// https://developers.google.com/analytics/devguides/collection/analyticsjs/events
Analytics.prototype.trackEvent = function (category, action, options) {
options = options || {};
var evt = {
eventCategory: category,
eventAction: action
};
if (options.label) {
evt.eventLabel = options.label;
delete options.label;
}
if (typeof options === 'object') {
$.extend(evt, options);
}
window.ga('send', 'event', evt);
};
window.GOVUK.Analytics = Analytics;
})(window);

View File

@@ -1,41 +0,0 @@
(function (window) {
"use strict";
window.GOVUK = window.GOVUK || {};
const trackingId = 'UA-75215134-1';
// Disable analytics by default
window[`ga-disable-${trackingId}`] = true;
const initAnalytics = function () {
// guard against being called more than once
if (!('analytics' in window.GOVUK)) {
window[`ga-disable-${trackingId}`] = false;
// Load Google Analytics libraries
window.GOVUK.Analytics.load();
// Configure profiles and make interface public
// for custom dimensions, virtual pageviews and events
window.GOVUK.analytics = new GOVUK.Analytics({
trackingId: trackingId,
cookieDomain: 'auto',
anonymizeIp: true,
displayFeaturesTask: null,
transport: 'beacon',
name: 'GOVUK.analytics',
expires: 365
});
// Track initial pageview
window.GOVUK.analytics.trackPageview();
}
};
window.GOVUK.initAnalytics = initAnalytics;
})(window);

View File

@@ -1,15 +0,0 @@
(function (window) {
"use strict";
function hasConsentFor (cookieCategory) {
const consentCookie = window.GOVUK.getConsentCookie();
if (consentCookie === null) { return false; }
if (!(cookieCategory in consentCookie)) { return false; }
return consentCookie[cookieCategory];
}
window.GOVUK.hasConsentFor = hasConsentFor;
})(window);

View File

@@ -1,97 +1,16 @@
window.GOVUK = window.GOVUK || {};
window.GOVUK.Modules = window.GOVUK.Modules || {};
(function () {
"use strict";
(function (Modules) {
function CookieBanner () { }
var root = this;
if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }
CookieBanner.clearOldCookies = function () {
// clear any cookies set by the previous version
var oldCookies = ['seen_cookie_message', '_ga', '_gid'];
GOVUK.addCookieMessage = function () {
var message = document.getElementById('global-cookie-message'),
hasCookieMessage = (message && GOVUK.cookie('seen_cookie_message') === null);
for (var i = 0; i < oldCookies.length; i++) {
if (window.GOVUK.cookie(oldCookies[i])) {
var cookieString = oldCookies[i] + '=;expires=' + new Date() + ';domain=' + window.location.hostname.replace(/^www\./, '.') + ';path=/';
document.cookie = cookieString;
}
if (hasCookieMessage) {
message.style.display = 'block';
GOVUK.cookie('seen_cookie_message', 'yes', { days: 28 });
}
};
CookieBanner.prototype.start = function ($module) {
this.$module = $module[0];
this.$module.hideCookieMessage = this.hideCookieMessage.bind(this);
this.$module.showConfirmationMessage = this.showConfirmationMessage.bind(this);
this.$module.setCookieConsent = this.setCookieConsent.bind(this);
this.$module.cookieBanner = document.querySelector('.notify-cookie-banner');
this.$module.cookieBannerConfirmationMessage = this.$module.querySelector('.notify-cookie-banner__confirmation');
this.setupCookieMessage();
};
CookieBanner.prototype.setupCookieMessage = function () {
this.$hideLink = this.$module.querySelector('button[data-hide-cookie-banner]');
if (this.$hideLink) {
this.$hideLink.addEventListener('click', this.$module.hideCookieMessage);
}
this.$acceptCookiesLink = this.$module.querySelector('button[data-accept-cookies=true]');
if (this.$acceptCookiesLink) {
this.$acceptCookiesLink.addEventListener('click', () => this.$module.setCookieConsent(true));
}
this.$rejectCookiesLink = this.$module.querySelector('button[data-accept-cookies=false]');
if (this.$rejectCookiesLink) {
this.$rejectCookiesLink.addEventListener('click', () => this.$module.setCookieConsent(false));
}
this.showCookieMessage();
};
CookieBanner.prototype.showCookieMessage = function () {
// Show the cookie banner if not in the cookie settings page
if (!this.isInCookiesPage()) {
var hasCookiesPolicy = window.GOVUK.cookie('cookies_policy');
if (this.$module && !hasCookiesPolicy) {
this.$module.style.display = 'block';
}
}
};
CookieBanner.prototype.hideCookieMessage = function (event) {
if (this.$module) {
this.$module.style.display = 'none';
}
if (event.target) {
event.preventDefault();
}
};
CookieBanner.prototype.setCookieConsent = function (analyticsConsent) {
window.GOVUK.setConsentCookie({ 'analytics': analyticsConsent });
this.$module.showConfirmationMessage(analyticsConsent);
this.$module.cookieBannerConfirmationMessage.focus();
if (analyticsConsent) { window.GOVUK.initAnalytics(); }
};
CookieBanner.prototype.showConfirmationMessage = function (analyticsConsent) {
var messagePrefix = analyticsConsent ? 'Youve accepted analytics cookies.' : 'You told us not to use analytics cookies.';
this.$cookieBannerMainContent = document.querySelector('.notify-cookie-banner__wrapper');
this.$cookieBannerConfirmationMessage = document.querySelector('.notify-cookie-banner__confirmation-message');
this.$cookieBannerConfirmationMessage.insertAdjacentText('afterbegin', messagePrefix);
this.$cookieBannerMainContent.style.display = 'none';
this.$module.cookieBannerConfirmationMessage.style.display = 'block';
};
CookieBanner.prototype.isInCookiesPage = function () {
return window.location.pathname === '/cookies';
};
Modules.CookieBanner = CookieBanner;
})(window.GOVUK.Modules);
}).call(this);

View File

@@ -1,84 +0,0 @@
window.GOVUK = window.GOVUK || {};
window.GOVUK.Modules = window.GOVUK.Modules || {};
(function (Modules) {
function CookieSettings () {}
CookieSettings.prototype.start = function ($module) {
this.$module = $module[0];
this.$module.submitSettingsForm = this.submitSettingsForm.bind(this);
document.querySelector('form[data-module=cookie-settings]')
.addEventListener('submit', this.$module.submitSettingsForm);
this.setInitialFormValues();
};
CookieSettings.prototype.setInitialFormValues = function () {
var currentConsentCookie = window.GOVUK.getConsentCookie('consent');
if (!currentConsentCookie) { return; }
var radioButton;
if (currentConsentCookie.analytics) {
radioButton = document.querySelector('input[name=cookies-analytics][value=on]');
} else {
radioButton = document.querySelector('input[name=cookies-analytics][value=off]');
}
radioButton.checked = true;
};
CookieSettings.prototype.submitSettingsForm = function (event) {
event.preventDefault();
var formInputs = event.target.querySelectorAll("input[name=cookies-analytics]");
var options = {};
for ( var i = 0; i < formInputs.length; i++ ) {
var input = formInputs[i];
if (input.checked) {
var value = input.value === "on" ? true : false;
options.analytics = value;
break;
}
}
window.GOVUK.setConsentCookie(options);
this.showConfirmationMessage();
if(window.GOVUK.hasConsentFor('analytics')) {
window.GOVUK.initAnalytics();
}
return false;
};
CookieSettings.prototype.showConfirmationMessage = function () {
var confirmationMessage = document.querySelector('div[data-cookie-confirmation]');
var previousPageLink = document.querySelector('.cookie-settings__prev-page');
var referrer = CookieSettings.prototype.getReferrerLink();
document.body.scrollTop = document.documentElement.scrollTop = 0;
if (referrer && referrer !== document.location.pathname) {
previousPageLink.href = referrer;
previousPageLink.style.display = "block";
} else {
previousPageLink.style.display = "none";
}
confirmationMessage.style.display = "block";
};
CookieSettings.prototype.getReferrerLink = function () {
return document.referrer ? new URL(document.referrer).pathname : false;
};
Modules.CookieSettings = CookieSettings;
})(window.GOVUK.Modules);

View File

@@ -1,22 +1,22 @@
(function(window) {
(function(Modules) {
"use strict";
window.GOVUK.Modules.TrackError = function() {
Modules.TrackError = function() {
this.start = function(component) {
if (!('analytics' in window.GOVUK)) return;
if (!('ga' in window)) return;
window.GOVUK.analytics.trackEvent(
ga(
'send',
'event',
'Error',
$(component).data('error-type'),
{
'label': $(component).data('error-label')
}
$(component).data('error-label')
);
};
};
})(window);
})(window.GOVUK.Modules);

View File

@@ -1,17 +1,8 @@
// used by the cookie banner component
(function () {
"use strict";
(function (root) {
'use strict';
window.GOVUK = window.GOVUK || {};
var DEFAULT_COOKIE_CONSENT = {
'analytics': false
};
var COOKIE_CATEGORIES = {
'_ga': 'analytics',
'_gid': 'analytics'
};
var root = this;
if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }
/*
Cookie methods
@@ -28,129 +19,38 @@
Deleting a cookie:
GOVUK.cookie('hobnob', null);
*/
window.GOVUK.cookie = function (name, value, options) {
if (typeof value !== 'undefined') {
if (value === false || value === null) {
return window.GOVUK.setCookie(name, '', { days: -1 });
GOVUK.cookie = function (name, value, options) {
if(typeof value !== 'undefined'){
if(value === false || value === null) {
return GOVUK.setCookie(name, '', { days: -1 });
} else {
// Default expiry date of 30 days
if (typeof options === 'undefined') {
options = { days: 30 };
}
return window.GOVUK.setCookie(name, value, options);
return GOVUK.setCookie(name, value, options);
}
} else {
return window.GOVUK.getCookie(name);
return GOVUK.getCookie(name);
}
};
window.GOVUK.getConsentCookie = function () {
var consentCookie = window.GOVUK.cookie('cookies_policy');
var consentCookieObj;
if (consentCookie) {
try {
consentCookieObj = JSON.parse(consentCookie);
} catch (err) {
return null;
}
if (typeof consentCookieObj !== 'object' && consentCookieObj !== null) {
consentCookieObj = JSON.parse(consentCookieObj);
}
} else {
return null;
GOVUK.setCookie = function (name, value, options) {
if(typeof options === 'undefined') {
options = {};
}
return consentCookieObj;
var cookieString = name + "=" + value + "; path=/";
if (options.days) {
var date = new Date();
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
cookieString = cookieString + "; expires=" + date.toGMTString();
}
if (document.location.protocol == 'https:'){
cookieString = cookieString + "; Secure";
}
document.cookie = cookieString;
};
window.GOVUK.setConsentCookie = function (options) {
var cookieConsent = window.GOVUK.getConsentCookie();
if (!cookieConsent) {
cookieConsent = JSON.parse(JSON.stringify(DEFAULT_COOKIE_CONSENT));
}
for (var cookieType in options) {
cookieConsent[cookieType] = options[cookieType];
// Delete cookies of that type if consent being set to false
if (!options[cookieType]) {
for (var cookie in COOKIE_CATEGORIES) {
if (COOKIE_CATEGORIES[cookie] === cookieType) {
window.GOVUK.cookie(cookie, null);
if (window.GOVUK.cookie(cookie)) {
document.cookie = cookie + '=;expires=' + new Date() + ';domain=' + window.location.hostname.replace(/^www\./, '.') + ';path=/';
}
}
}
}
}
window.GOVUK.setCookie('cookies_policy', JSON.stringify(cookieConsent), { days: 365 });
};
window.GOVUK.checkConsentCookieCategory = function (cookieName, cookieCategory) {
var currentConsentCookie = window.GOVUK.getConsentCookie();
// If the consent cookie doesn't exist, but the cookie is in our known list, return true
if (!currentConsentCookie && COOKIE_CATEGORIES[cookieName]) {
return true;
}
currentConsentCookie = window.GOVUK.getConsentCookie();
// Sometimes currentConsentCookie is malformed in some of the tests, so we need to handle these
try {
return currentConsentCookie[cookieCategory];
} catch (e) {
console.error(e);
return false;
}
};
window.GOVUK.checkConsentCookie = function (cookieName, cookieValue) {
// If we're setting the consent cookie OR deleting a cookie, allow by default
if (cookieName === 'cookies_policy' || (cookieValue === null || cookieValue === false)) {
return true;
}
if (COOKIE_CATEGORIES[cookieName]) {
var cookieCategory = COOKIE_CATEGORIES[cookieName];
return window.GOVUK.checkConsentCookieCategory(cookieName, cookieCategory);
} else {
// Deny the cookie if it is not known to us
return false;
}
};
window.GOVUK.setCookie = function (name, value, options) {
if (window.GOVUK.checkConsentCookie(name, value)) {
if (typeof options === 'undefined') {
options = {};
}
var cookieString = name + '=' + value + '; path=/';
if (options.days) {
var date = new Date();
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
cookieString = cookieString + '; expires=' + date.toGMTString();
}
if (document.location.protocol === 'https:') {
cookieString = cookieString + '; Secure';
}
document.cookie = cookieString;
}
};
window.GOVUK.getCookie = function (name) {
var nameEQ = name + '=';
GOVUK.getCookie = function (name) {
var nameEQ = name + "=";
var cookies = document.cookie.split(';');
for (var i = 0, len = cookies.length; i < len; i++) {
for(var i = 0, len = cookies.length; i < len; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) === ' ') {
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1, cookie.length);
}
if (cookie.indexOf(nameEQ) === 0) {
@@ -159,5 +59,4 @@
}
return null;
};
}(window));
}).call(this);

View File

@@ -1,10 +1,6 @@
window.GOVUK.Frontend.initAll();
window.GOVUK.Modules.CookieBanner.clearOldCookies();
if (window.GOVUK.hasConsentFor('analytics')) {
window.GOVUK.initAnalytics();
}
$(() => GOVUK.addCookieMessage());
$(() => $("time.timeago").timeago());