Skip to content

Commit

Permalink
refactor: use gtm triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
ogous committed Sep 13, 2024
1 parent a0c95c2 commit 64a3640
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion web/src/components/buttons/donateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import FavedIcon from "#/images/icons/faved.svg";
import Loader from "../loader";
import { config } from "@/config";
import useClickAnimation from "@/hooks/useClickAnimation";
import { sendGTMEvent } from "@next/third-parties/google";

export default function DonateButton() {
const account = useActiveAccount();
Expand All @@ -28,7 +29,7 @@ export default function DonateButton() {
if (account) {
startAnimation();
incrementDonationClicks(selectedStream.hash);
window.gtag("event", "button_click", {
sendGTMEvent({
event_category: "donation",
event_label: "cat_donation",
value: config.features.web3.donation.clickUnit,
Expand Down
13 changes: 12 additions & 1 deletion web/src/components/googleAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client";
import Script from "next/script";
import { GoogleTagManager } from "@next/third-parties/google";
import { useActiveAccount } from "thirdweb/react";
import { useEffect } from "react";

export const grantedConsent = {
ad_storage: "granted",
Expand All @@ -20,6 +22,15 @@ export const deniedConsent = {

export default function GoogleAnalytics() {
const gtmId = "GTM-KWBPC355";
const account = useActiveAccount();

useEffect(() => {
if (account?.address) {
window.localStorage.setItem("walletAddress", account.address);
} else {
window.localStorage.removeItem("walletAddress");
}
}, [account?.address]);

return (
<>
Expand All @@ -36,7 +47,7 @@ export default function GoogleAnalytics() {
if (localStorage.getItem('userId') != null) {
window.dataLayer.push({
'user_id': localStorage.getItem('userId')
'walletAddress': localStorage.getItem('walletAddress')
});
}
`}
Expand Down
5 changes: 3 additions & 2 deletions web/src/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Account } from "thirdweb/wallets";
import toast from "react-hot-toast";
import { prepareContractCall, sendTransaction, toUnits } from "thirdweb";
import { streams } from "@/streams";
import { sendGTMEvent } from "@next/third-parties/google";
interface UserStore {
favs: string[];
favStream: (streamHash: string) => void;
Expand Down Expand Up @@ -130,7 +131,7 @@ export async function handleDonation(id: string, account: Account) {
});

await sendTransaction({ transaction, account });
window.gtag("event", "bg-action", {
sendGTMEvent({
event_category: "donation",
event_label: "cat_donation_completed",
value: config.features.web3.donation.clickUnit * donation,
Expand All @@ -142,7 +143,7 @@ export async function handleDonation(id: string, account: Account) {
updateDonationStatus(id, "success");
} catch (e) {
updateDonationStatus(id, "error", e);
window.gtag("event", "bg-action", {
sendGTMEvent({
event_category: "donation",
event_label: "cat_donation_failed",
value: config.features.web3.donation.clickUnit * donation,
Expand Down

0 comments on commit 64a3640

Please sign in to comment.