vue-gtag
Version:
Global Site Tag (gtag.js) plugin for Vue
290 lines (289 loc) • 7.44 kB
JavaScript
import { ref as N } from "vue";
function l() {
return typeof window > "u" || typeof document > "u";
}
async function R(e, t) {
return new Promise((n, i) => {
if (l())
return n();
const c = document.head, a = document.createElement("script");
if (a.async = !0, a.src = e, a.type = (t == null ? void 0 : t.type) ?? "text/javascript", t != null && t.defer && (a.defer = !0), t != null && t.nonce && a.setAttribute("nonce", t.nonce), t != null && t.preconnect) {
const r = document.createElement("link"), f = new URL(e);
r.href = f.origin, r.rel = "preconnect", c.appendChild(r);
}
c.appendChild(a), a.onload = () => n(), a.onerror = (r) => i(r);
});
}
function v(e) {
return e !== null && typeof e == "object" && !Array.isArray(e);
}
function j(e, t) {
const n = { ...e };
for (const i in t)
if (Object.prototype.hasOwnProperty.call(t, i)) {
const c = t[i], a = e[i];
v(c) && v(a) ? n[i] = j(a, c) : n[i] = c;
}
return n;
}
const S = {
resource: {
url: "https://www.googletagmanager.com/gtag/js",
inject: !0
},
dataLayerName: "dataLayer",
gtagName: "gtag",
groupName: "default",
initMode: "auto"
};
let p = { ...S };
function u() {
return p;
}
function C(e) {
p = j(p, e);
}
function o(...e) {
const { dataLayerName: t, gtagName: n } = u();
l() || (window[t] = window[t] || [], window[n] = function() {
window[t].push(arguments);
}, window[n](...e));
}
function O(e) {
const { tagId: t, additionalAccounts: n } = u();
if (t && (o("config", t, e), !!n))
for (const i of n)
o("config", i.tagId, e);
}
function s(e, t) {
o("consent", e, t);
}
function _(e = "default") {
s(e, {
ad_user_data: "granted",
ad_personalization: "granted",
ad_storage: "granted",
analytics_storage: "granted"
});
}
function w(e = "default") {
s(e, {
ad_user_data: "denied",
ad_personalization: "denied",
ad_storage: "denied",
analytics_storage: "denied"
});
}
function z(e) {
O({
custom_map: e
});
}
function g(e, t) {
const { groupName: n, additionalAccounts: i } = u();
t.send_to === void 0 && (i != null && i.length) && (t.send_to = n), o("event", e, t);
}
function E(e, t) {
g(e, t);
}
function L(e) {
g("exception", e);
}
function x(e) {
o("set", "linker", e);
}
function h(e, t) {
t ? window[e] = t : delete window[e];
}
function A(e, t) {
const { tagId: n, additionalAccounts: i } = u();
if (!l() && (h(`ga-disable-${e ?? n}`, t), !(!(i != null && i.length) || e)))
for (const c of i)
h(`ga-disable-${c.tagId}`, t);
}
function M(e) {
A(e, !0);
}
function G(e) {
A(e, void 0);
}
function V(e, t) {
const n = t.endsWith("/") ? t : `${t}/`, i = e.startsWith("/") ? e.substring(1) : e;
return `${n}${i}`;
}
function I(e) {
var i;
const { pageTracker: t } = u();
let n;
if (typeof e == "string")
n = {
page_path: e
};
else if ("path" in e) {
const c = (t == null ? void 0 : t.router.options.history.base) ?? "", a = t != null && t.useRouteFullPath ? e.fullPath : e.path;
n = {
...e.name ? { page_title: e.name } : {},
page_path: t != null && t.useRouterBasePath ? V(a, c) : a
};
} else
n = e;
n.page_location === void 0 && (n.page_location = window.location.href), n.send_page_view === void 0 && (n.send_page_view = (t == null ? void 0 : t.sendPageView) ?? !0), n.page_path !== "/" && ((i = n.page_path) != null && i.endsWith("/")) && (n.page_path = n.page_path.slice(0, -1)), o("event", "page_view", n);
}
function $(e) {
const { appName: t } = u();
let n = {};
typeof e == "string" ? n.screen_name = e : "path" in e ? n.screen_name = e.name ?? e.path : n = e, t && (n == null ? void 0 : n.app_name) === void 0 && (n.app_name = t), o("event", "screen_view", n);
}
function W(...e) {
o("set", ...e);
}
function B(e) {
g("timing_complete", e);
}
const D = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
config: O,
consent: s,
consentDeniedAll: w,
consentGrantedAll: _,
customMap: z,
ecommerce: E,
event: g,
exception: L,
linker: x,
optIn: G,
optOut: M,
pageview: I,
query: o,
screenview: $,
set: W,
time: B
}, Symbol.toStringTag, { value: "Module" }));
function P(e = {}) {
return {
send_page_view: !1,
anonymize_ip: !0,
...e
};
}
function U() {
var f, m;
const {
tagId: e,
config: t,
groupName: n,
linker: i,
additionalAccounts: c,
hooks: a,
consentMode: r
} = u();
if (e) {
if ((f = a == null ? void 0 : a["config:init:before"]) == null || f.call(a), r === "granted" ? _() : r === "denied" && w(), i && x(i), o("js", /* @__PURE__ */ new Date()), o("config", e, P(t)), c)
for (const y of c)
o(
"config",
y.tagId,
P({
groups: n,
...y.config
})
);
(m = a == null ? void 0 : a["config:init:after"]) == null || m.call(a);
}
}
function q(e) {
var n;
const { pageTracker: t } = u();
return t != null && t.exclude ? typeof t.exclude == "function" ? t.exclude(e) : (n = t.exclude) == null ? void 0 : n.some(({ name: i, path: c } = {}) => i && i === e.name || c && c === e.path) : !1;
}
function b(e) {
var c, a;
const { pageTracker: t, hooks: n } = u();
if (q(e))
return;
(c = n == null ? void 0 : n["router:track:before"]) == null || c.call(n, e);
let i;
if (t != null && t.template && (i = typeof t.template == "function" ? t.template(e) : t.template), t != null && t.useScreenview) {
const r = i && "screen_name" in i ? i : e;
$(r);
} else {
const r = i && "page_path" in i ? i : e;
I(r);
}
(a = n == null ? void 0 : n["router:track:after"]) == null || a.call(n, e);
}
async function F() {
const { pageTracker: e } = u();
if (!(e != null && e.router))
return;
const { router: t } = e;
await t.isReady(), b(t.currentRoute.value), t.afterEach((n, i) => {
n.path !== i.path && b(n);
});
}
async function d() {
var a, r;
const { resource: e, dataLayerName: t, tagId: n, pageTracker: i, hooks: c } = u();
if (n && (U(), i != null && i.router && F(), !!e.inject))
try {
await R(`${e.url}?id=${n}&l=${t}`, {
preconnect: e.preconnect,
defer: e.defer,
nonce: e.nonce
}), (a = c == null ? void 0 : c["script:loaded"]) == null || a.call(c);
} catch (f) {
(r = c == null ? void 0 : c["script:error"]) == null || r.call(c, f);
}
}
const K = "_ga";
function X() {
const e = N(
l() ? !1 : document.cookie.includes(K)
), t = async () => {
await d(), _("update"), window.location.reload();
}, n = () => {
w("update");
}, i = async (c) => {
await d(), s("update", c), window.location.reload();
};
return e.value && d(), {
hasConsent: e,
acceptAll: t,
rejectAll: n,
acceptCustom: i
};
}
function H() {
const { initMode: e } = u();
e !== "manual" && d();
}
function J(e) {
C(e), H();
}
function Y(e) {
return J(e), (t) => {
t.config.globalProperties.$gtag = D;
};
}
export {
d as addGtag,
O as config,
J as configure,
s as consent,
w as consentDeniedAll,
_ as consentGrantedAll,
Y as createGtag,
z as customMap,
E as ecommerce,
g as event,
L as exception,
x as linker,
G as optIn,
M as optOut,
I as pageview,
o as query,
$ as screenview,
W as set,
B as time,
X as useConsent
};