Skip to content

Commit 69803d4

Browse files
feat: add global dataLayer and event tracking functionality (#75)
1 parent d06af71 commit 69803d4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

.vitepress/theme/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ import { wagmiAdapter } from '@/utils/wagmiConfig';
1414
import ChainSelector from '@/components/ChainSelector.vue';
1515
import './style.css';
1616

17+
declare global {
18+
interface Window {
19+
dataLayer: any[];
20+
axeptioSettings: {
21+
clientId: string;
22+
cookiesVersion: string;
23+
};
24+
}
25+
}
26+
1727
export default {
1828
extends: DefaultTheme,
1929
Layout,
@@ -33,5 +43,32 @@ export default {
3343
googleAnalytics({
3444
id: 'GTM-P7KSD4T',
3545
});
46+
47+
if (typeof window !== 'undefined') {
48+
// Ensure dataLayer exists
49+
window.dataLayer = window.dataLayer || [];
50+
51+
// Define a map of event types and their corresponding actions
52+
const eventMap = {
53+
connectWallet: 'hw_connectWallet',
54+
protectData: 'hw_protectData',
55+
grantAccess: 'hw_grantAccess',
56+
claimVoucher: 'hw_claimVoucher',
57+
};
58+
59+
// Add a global click listener
60+
document.addEventListener('click', (event) => {
61+
if (event.target instanceof Element) {
62+
// Iterate through eventMap to check which event matches
63+
Object.keys(eventMap).forEach((key) => {
64+
if ((event.target as Element).matches(`[data-track="${key}"]`)) {
65+
window.dataLayer.push({
66+
event: eventMap[key],
67+
});
68+
}
69+
});
70+
}
71+
});
72+
}
3673
},
3774
} satisfies Theme;

0 commit comments

Comments
 (0)