digitalmarketplace-frontend
Version:
Digital Marketplace Frontend contains assets and components used in Digital Marketplace projects
168 lines (162 loc) • 5.45 kB
JavaScript
const GOOGLE_TAG_MANAGER_ID = 'GTM-WCGFX3GW';
const googleTagManagerInit = (w, d, s, l, i) => {
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
const f = d.getElementsByTagName(s)[0];
const j = d.createElement(s);
const dl = '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
};
const loadGoogleTagManager = () => googleTagManagerInit(window, document, 'script', 'dataLayer', GOOGLE_TAG_MANAGER_ID);
function assign(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (key === '__proto__') continue;
target[key] = source[key];
}
}
return target;
}
var defaultConverter = {
read: function read(value) {
if (value[0] === '"') {
value = value.slice(1, -1);
}
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
},
write: function write(value) {
return encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent);
}
};
function init(converter, defaultAttributes) {
function set(name, value, attributes) {
if (typeof document === 'undefined') {
return;
}
attributes = assign({}, defaultAttributes, attributes);
if (typeof attributes.expires === 'number') {
attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
}
if (attributes.expires) {
attributes.expires = attributes.expires.toUTCString();
}
name = encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent).replace(/[()]/g, escape);
var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}
return document.cookie = name + '=' + converter.write(value, name) + stringifiedAttributes;
}
function get(name) {
if (typeof document === 'undefined' || arguments.length && !name) {
return;
}
var cookies = document.cookie ? document.cookie.split('; ') : [];
var jar = {};
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var value = parts.slice(1).join('=');
try {
var found = decodeURIComponent(parts[0]);
if (!(found in jar)) jar[found] = converter.read(value, found);
if (name === found) {
break;
}
} catch (_unused) {}
}
return name ? jar[name] : jar;
}
return Object.create({
set: set,
get: get,
remove: function remove(name, attributes) {
set(name, '', assign({}, attributes, {
expires: -1
}));
},
withAttributes: function withAttributes(attributes) {
return init(this.converter, assign({}, this.attributes, attributes));
},
withConverter: function withConverter(converter) {
return init(assign({}, this.converter, converter), this.attributes);
}
}, {
attributes: {
value: Object.freeze(defaultAttributes)
},
converter: {
value: Object.freeze(converter)
}
});
}
var api = init(defaultConverter, {
path: '/'
});
var GrantType;
(function (GrantType) {
GrantType["GRANTED"] = "granted";
GrantType["NOT_GRANTED"] = "not granted";
})(GrantType || (GrantType = {}));
const cookieUpdateOptions = [
{
cookieName: 'usage',
cookiePrefixes: ['_ga', '_gi']
},
{
cookieName: 'glassbox',
cookiePrefixes: ['_cls']
}
];
const DOMAINS = [
'.marketplace.team',
'.digitalmarketplace.service.gov.uk',
'www.applytosupply.digitalmarketplace.service.gov.uk'
];
// When not in production we want to delete cookies in localhost too
const getDomains = () => {
if (window.location.hostname === 'localhost') {
return DOMAINS.concat(['localhost']);
}
return DOMAINS;
};
const getCookiePreferences = () => {
const defaultCookieSettings = '{"usage":false,"glassbox":false}';
return JSON.parse(api.get('cookie_preferences_dmp') ?? defaultCookieSettings);
};
const removeUnwantedCookies = () => {
const cookieList = Object.keys(api.get());
const cookiesToRemove = ['digitalmarketplace_cookie_settings_viewed', 'digitalmarketplace_google_analytics_enabled', 'digitalmarketplace_cookie_options_v1', 'dm_cookies_policy'];
const cookiePreferences = getCookiePreferences();
const cookiePrefixes = [];
cookieUpdateOptions.forEach((cookieUpdateOption) => {
if (!cookiePreferences[cookieUpdateOption.cookieName])
cookiePrefixes.push(...cookieUpdateOption.cookiePrefixes);
});
for (let i = 0; i < cookieList.length; i++) {
const cookieName = cookieList[i];
if (cookiePrefixes.some((cookiePrefix) => cookieName.startsWith(cookiePrefix)))
cookiesToRemove.push(cookieName);
}
getDomains().forEach((domain) => {
cookiesToRemove.forEach((cookieName) => { api.remove(cookieName, { path: '/', domain: domain }); });
});
};
const initGoogleAnalytics = () => {
loadGoogleTagManager();
removeUnwantedCookies();
};
export { initGoogleAnalytics };
//# sourceMappingURL=init-google-analytics.bundle.mjs.map