-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2135 from alphagov/add-rum-to-public-layout
Add real user metrics using Speedcurve's LUX
- Loading branch information
Showing
8 changed files
with
1,166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/assets/javascripts/govuk_publishing_components/rum-loader.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(function() { | ||
var marker = document.getElementsByTagName('script')[0] | ||
var scripts = [ | ||
'<%= path_to_javascript("govuk_publishing_components/vendor/lux/lux-polyfill") %>', | ||
'<%= path_to_javascript("govuk_publishing_components/vendor/lux/lux") %>' | ||
] | ||
var parsedCookie = (function() { | ||
try { | ||
var cookies = document.cookie.split(';') | ||
|
||
for (var i = 0; i < cookies.length; i++) { | ||
var cookie = cookies[i].replace(/\s/g, '') // Remove all whitespace. | ||
var cookieName = 'cookies_policy=' | ||
|
||
if (cookie.indexOf(cookieName) === 0) { | ||
return JSON.parse(cookie.substring(cookieName.length)) | ||
} | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
|
||
return {} | ||
})() | ||
|
||
if (parsedCookie.usage === true) { | ||
for (var i = 0; i < scripts.length; i++) { | ||
var script = document.createElement('script') | ||
script.src = scripts[i] | ||
script.async = true | ||
script.defer = true | ||
|
||
marker.parentNode.insertBefore(script, marker) | ||
} | ||
} | ||
})() |
159 changes: 159 additions & 0 deletions
159
app/assets/javascripts/govuk_publishing_components/vendor/lux/lux-polyfill.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* eslint-disable */ | ||
|
||
/* | ||
* Part of Speedcurve's LUX script, which allows us to run real user metrics on | ||
* pages using the public layout template. | ||
* | ||
* See docs/real-user-metrics.md for more information. | ||
*/ | ||
|
||
LUX = (function () { | ||
var a = | ||
"undefined" !== typeof LUX && "undefined" !== typeof LUX.gaMarks | ||
? LUX.gaMarks | ||
: []; | ||
var d = | ||
"undefined" !== typeof LUX && "undefined" !== typeof LUX.gaMeasures | ||
? LUX.gaMeasures | ||
: []; | ||
var j = "LUX_start"; | ||
var k = window.performance; | ||
var l = | ||
"undefined" !== typeof LUX && LUX.ns | ||
? LUX.ns | ||
: Date.now | ||
? Date.now() | ||
: +new Date(); | ||
if (k && k.timing && k.timing.navigationStart) { | ||
l = k.timing.navigationStart; | ||
} | ||
function f() { | ||
if (k && k.now) { | ||
return k.now(); | ||
} | ||
var o = Date.now ? Date.now() : +new Date(); | ||
return o - l; | ||
} | ||
function b(n) { | ||
if (k) { | ||
if (k.mark) { | ||
return k.mark(n); | ||
} else { | ||
if (k.webkitMark) { | ||
return k.webkitMark(n); | ||
} | ||
} | ||
} | ||
a.push({ name: n, entryType: "mark", startTime: f(), duration: 0 }); | ||
return; | ||
} | ||
function m(p, t, n) { | ||
if ("undefined" === typeof t && h(j)) { | ||
t = j; | ||
} | ||
if (k) { | ||
if (k.measure) { | ||
if (t) { | ||
if (n) { | ||
return k.measure(p, t, n); | ||
} else { | ||
return k.measure(p, t); | ||
} | ||
} else { | ||
return k.measure(p); | ||
} | ||
} else { | ||
if (k.webkitMeasure) { | ||
return k.webkitMeasure(p, t, n); | ||
} | ||
} | ||
} | ||
var r = 0, | ||
o = f(); | ||
if (t) { | ||
var s = h(t); | ||
if (s) { | ||
r = s.startTime; | ||
} else { | ||
if (k && k.timing && k.timing[t]) { | ||
r = k.timing[t] - k.timing.navigationStart; | ||
} else { | ||
return; | ||
} | ||
} | ||
} | ||
if (n) { | ||
var q = h(n); | ||
if (q) { | ||
o = q.startTime; | ||
} else { | ||
if (k && k.timing && k.timing[n]) { | ||
o = k.timing[n] - k.timing.navigationStart; | ||
} else { | ||
return; | ||
} | ||
} | ||
} | ||
d.push({ name: p, entryType: "measure", startTime: r, duration: o - r }); | ||
return; | ||
} | ||
function h(n) { | ||
return c(n, g()); | ||
} | ||
function c(p, o) { | ||
for (i = o.length - 1; i >= 0; i--) { | ||
var n = o[i]; | ||
if (p === n.name) { | ||
return n; | ||
} | ||
} | ||
return undefined; | ||
} | ||
function g() { | ||
if (k) { | ||
if (k.getEntriesByType) { | ||
return k.getEntriesByType("mark"); | ||
} else { | ||
if (k.webkitGetEntriesByType) { | ||
return k.webkitGetEntriesByType("mark"); | ||
} | ||
} | ||
} | ||
return a; | ||
} | ||
return { mark: b, measure: m, gaMarks: a, gaMeasures: d }; | ||
})(); | ||
LUX.ns = Date.now ? Date.now() : +new Date(); | ||
LUX.ac = []; | ||
LUX.cmd = function (a) { | ||
LUX.ac.push(a); | ||
}; | ||
LUX.init = function () { | ||
LUX.cmd(["init"]); | ||
}; | ||
LUX.send = function () { | ||
LUX.cmd(["send"]); | ||
}; | ||
LUX.addData = function (a, b) { | ||
LUX.cmd(["addData", a, b]); | ||
}; | ||
LUX_ae = []; | ||
window.addEventListener("error", function (a) { | ||
LUX_ae.push(a); | ||
}); | ||
LUX_al = []; | ||
if ( | ||
"function" === typeof PerformanceObserver && | ||
"function" === typeof PerformanceLongTaskTiming | ||
) { | ||
var LongTaskObserver = new PerformanceObserver(function (c) { | ||
var b = c.getEntries(); | ||
for (var a = 0; a < b.length; a++) { | ||
var d = b[a]; | ||
LUX_al.push(d); | ||
} | ||
}); | ||
try { | ||
LongTaskObserver.observe({ type: ["longtask"] }); | ||
} catch (e) {} | ||
} |
Oops, something went wrong.