Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
🧐 gtm added, events created (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetilhn authored Feb 28, 2023
1 parent 901f817 commit 3dd3a0f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions components/earthquakes-chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@ const getChartData = () => {
oldX = x;
});
};
const { $gtm } = useNuxtApp();
const route = useRoute();
const openChartDetailModal = () => {
emit("openChartDetailModal");
$gtm.pushUIEvent({
eventCategory: "Earthquakes Chart - Click",
eventAction: activeEarthquake.Region,
eventLabel: route.path,
});
};
onMounted(() => {
getChartData();
Expand Down
6 changes: 6 additions & 0 deletions components/partials/bottom-bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,17 @@ type MenuType = {
};
const router = useRouter();
const route = useRoute();
const { $gtm } = useNuxtApp();
const activeMenu = computed(() => {
return route.path;
});
const click = (menu: MenuType) => {
router.push(menu.path);
$gtm.pushUIEvent({
eventCategory: "Bottom Bar - Click",
eventAction: menu.path,
eventLabel: route.path,
});
};
</script>
<style lang="scss" scoped>
Expand Down
5 changes: 5 additions & 0 deletions middleware/gtm-event.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default defineNuxtRouteMiddleware((to) => {
if (process.server) return;
const nuxtApp = useNuxtApp();
nuxtApp.$gtm.pushVirtualPageViewEvent(to.path);
});
4 changes: 4 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default defineNuxtConfig({
{
src: "@/plugins/dayjs.plugin.ts",
},
{
src: "@/plugins/gtm.plugin.ts",
mode: "client",
},
],
modules: ["@pinia/nuxt"],
app: {
Expand Down
14 changes: 14 additions & 0 deletions plugins/gtm.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default defineNuxtPlugin((nuxtApp) => {
const inject = (w: Window, d: Document, s: string, l: string, i: string) => {
w[l] = w[l] || [];
w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
var f = d.getElementsByTagName(s)[0],
j: HTMLScriptElement = d.createElement(s),
dl = l != "dataLayer" ? "&l=" + l : "";
j.async = true;
j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
f.parentNode.insertBefore(j, f);
};
inject(window, document, "script", "dataLayer", "G-DY1PV5SYM8");
nuxtApp.$gtm = gtmUtil;
});
7 changes: 7 additions & 0 deletions types/gtm.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type UIEventObject = {
event: string;
eventCategory: string;
eventAction: string;
eventLabel: string;
eventNonInteraction: boolean;
};
34 changes: 34 additions & 0 deletions utils/gtm.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { UIEventObject } from "~~/types/gtm.type";

const pushUIEvent = ({
eventCategory,
eventAction,
eventLabel,
eventNonInteraction = false,
}: UIEventObject): void => {
if (process.server) return;
window?.dataLayer?.push({
event: "ui",
eventCategory,
eventAction,
eventLabel,
eventNonInteraction,
});
};

const pushVirtualPageViewEvent = (path: string): void => {
if (process.server) return;
window?.dataLayer?.push({
event: "vpv.Event",
eventCategory: "Virtual PageView",
eventAction: "Virtual PageView",
eventLabel: path,
eventValue: 0,
eventNonInteraction: false,
});
};

export default {
pushUIEvent,
pushVirtualPageViewEvent,
};

0 comments on commit 3dd3a0f

Please sign in to comment.