Hello @martin_kabat, @dtod, et alia,
Below is our version which works perfectly, note the use of the UA tracking code, which works fine.
THese are the live feeds it produces with a single course added as a filter: https://lookerstudio.google.com/reporting/a1edcff7-926d-4810-8253-9ed15a3eaa23
I would now like to add all the other available dimensions https://portal.inshosteddata.com/docs and to publish the js file here for all to simply copy and paste (and sync in GA).
I'm going to have a go at it myself today but any guidance would be gratefully received.
Yours sincerely,
@JedKeenan
function g() {
window.addEventListener("load", function () {
ENV.current_user_roles.indexOf("teacher") >= 1 &&
[...document.querySelectorAll(".teacher-only")].forEach((e) => {
(function (e, o, t, a, n, s, r) {
(e.GoogleAnalyticsObject = n),
(e[n] =
e[n] ||
function () {
(e[n].q = e[n].q || []).push(arguments);
}),
(e[n].l = 1 * new Date()),
(s = o.createElement(t)),
(r = o.getElementsByTagName(t)[0]),
(s.async = 1),
(s.src=a),
r.parentNode.insertBefore(s, r);
})(window, document, "script", "https://www.google-analytics.com/analytics.js", "gtag");
function v(e) {
try {
localStorage.removeItem(e), localStorage.removeItem(e + "_expiresIn");
} catch (o) {
return console.log("removeStorage: Error removing key [" + e + "] from localStorage: " + JSON.stringify(o)), !1;
}
return !0;
}
function h(e) {
var o = Date.now(),
t = localStorage.getItem(e + "_expiresIn");
if ((t == null && (t = 0), t < o)) return v(e), null;
try {
var a = localStorage.getItem(e);
return a;
} catch (n) {
return console.log("getStorage: Error reading key [" + e + "] from localStorage: " + JSON.stringify(n)), null;
}
}
function w(e, o, t) {
t == null ? (t = 24 * 60 * 60) : (t = Math.abs(t));
var a = Date.now(),
n = a + t * 1e3;
try {
localStorage.setItem(e, o), localStorage.setItem(e + "_expiresIn", n);
} catch (s) {
return console.log("setStorage: Error setting key [" + e + "] in localStorage: " + JSON.stringify(s)), !1;
}
return !0;
}
async function _(e) {
let t = await (await fetch("/api/v1/users/self/courses?per_page=100")).text();
(t = t.replace("while(1);", "")), (t = JSON.parse(t));
var a = JSON.stringify(t);
w("ga_enrollments", a, null);
var n = f(e, a);
return n;
}
function f(e, o) {
if (o != null) {
let a = JSON.parse(o);
for (var t = 0; t < a.length; t++) if (a[t].id == e) return a[t];
}
return null;
}
function u(e) {
gtag("set", "dimension4", e.id),
gtag("set", "dimension5", e.name),
gtag("set", "dimension6", e.account_id),
gtag("set", "dimension7", e.enrollment_term_id),
gtag("set", "dimension8", e.enrollments[0].type),
gtag("send", "pageview");
}
function m(e) {
var o, t, a, n;
if (
(gtag("create", e, "auto"),
(o = ENV.current_user_id),
(t = ENV.current_user_roles),
gtag("set", "userId", o),
gtag("set", "dimension2", o),
gtag("set", "dimension3", t),
(n = window.location.pathname.match(/\/courses\/(\d+)/)),
n)
) {
(n = n[1]), (a = 0);
try {
let r = h("ga_enrollments");
if (r != null) {
var s = f(n, r);
s === null
? _(n).then((l) => {
l === null ? (gtag("set", "dimension4", n), gtag("send", "pageview")) : u(l);
})
: u(s);
} else
_(n).then((l) => {
l === null ? (gtag("set", "dimension4", n), gtag("send", "pageview")) : u(l);
});
} catch {
if (((a += 1), a > 5)) {
gtag("set", "dimension4", n), gtag("send", "pageview");
return;
}
}
} else gtag("send", "pageview");
}
i();
g();
d();
globalThis.DT_variables = c;
m("UA-XXXXXXXXX-1");
})();