From c3c202fa3bdfe20109b6d2fdd2c2706c69b9d2c2 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Wed, 27 Dec 2023 19:25:40 -0800 Subject: [PATCH 1/3] feat: add individual search events --- components/hero/hero.tsx | 11 +++++++++++ components/search/search.tsx | 12 ++++++++++++ lib/analytics.ts | 28 ++++++++++++++++++++++++++++ lib/gtag.ts | 18 ------------------ 4 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 lib/analytics.ts diff --git a/components/hero/hero.tsx b/components/hero/hero.tsx index 9ca6726..1c3ec0c 100644 --- a/components/hero/hero.tsx +++ b/components/hero/hero.tsx @@ -4,6 +4,7 @@ import { UNIVERSITY_GE } from "@/lib/constants"; import React, { ChangeEvent, FormEvent, useState } from "react"; import { FaChevronDown, FaSearch } from "react-icons/fa"; import { useRouter } from "next/navigation"; +import { analyticsEnum, logAnalytics } from "@/lib/analytics"; interface DropdownComponentProps { defaultValue: string; @@ -49,6 +50,16 @@ const Hero = () => { const handleSubmit = (e: FormEvent) => { e.preventDefault(); + + logAnalytics({ + category: analyticsEnum.search.title, + action: analyticsEnum.search.actions.SEARCH, + label: university, + value: UNIVERSITY_GE.university.findIndex((item) => { + return item.includes(ge); + }), + }); + router.push( `/search?uni=${encodeURIComponent( university, diff --git a/components/search/search.tsx b/components/search/search.tsx index c4e1c5d..8b3ba60 100644 --- a/components/search/search.tsx +++ b/components/search/search.tsx @@ -12,6 +12,9 @@ import SearchBlurb from "./blurb"; import { filterData } from "./filterUtils"; import { UNIVERSITY_GE } from "@/lib/constants"; +import * as gtag from "@/lib/gtag"; +import { analyticsEnum, logAnalytics } from "@/lib/analytics"; + export interface CollegeObject { sendingInstitution: string; courseCode: string; @@ -169,6 +172,15 @@ const Search = () => { } }; + logAnalytics({ + category: analyticsEnum.search.title, + action: analyticsEnum.search.actions.SEARCH, + label: university, + value: UNIVERSITY_GE.university.findIndex((item) => { + return item.includes(ge); + }), + }); + fetchData(); }, [university, ge]); diff --git a/lib/analytics.ts b/lib/analytics.ts new file mode 100644 index 0000000..72e3d6c --- /dev/null +++ b/lib/analytics.ts @@ -0,0 +1,28 @@ +export const analyticsEnum = { + search: { + title: "Search Page", + actions: { + SEARCH: "Search Course", // Label is school, value is + }, + }, +}; + +interface AnalyticsProps { + category: string; + action: string; + label?: string; + value?: number; +} + +export function logAnalytics({ + category, + action, + label, + value, +}: AnalyticsProps) { + (window as any).gtag("event", action, { + event_category: category, + event_label: label, + value: value, + }); +} diff --git a/lib/gtag.ts b/lib/gtag.ts index bdbc79d..f6d014b 100644 --- a/lib/gtag.ts +++ b/lib/gtag.ts @@ -8,21 +8,3 @@ export const pageview = (url: string) => { page_path: url, }); }; - -export const event = ({ - action, - category, - label, - value, -}: { - action: string; - category: string; - label: string; - value: number; -}) => { - (window as any).gtag("event", action, { - event_category: category, - event_label: label, - value: value, - }); -}; From cca3afbbd08d1675674b0edb33942549d703bcdb Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Wed, 27 Dec 2023 19:26:59 -0800 Subject: [PATCH 2/3] doc: fix doc in analyticsa --- lib/analytics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/analytics.ts b/lib/analytics.ts index 72e3d6c..fcb4bc2 100644 --- a/lib/analytics.ts +++ b/lib/analytics.ts @@ -2,7 +2,7 @@ export const analyticsEnum = { search: { title: "Search Page", actions: { - SEARCH: "Search Course", // Label is school, value is + SEARCH: "Search Course", // Label is school, value is index of GE }, }, }; From f8e4b0b7fcfd5c7f66a5742a40f16c9a92496022 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Wed, 27 Dec 2023 19:28:37 -0800 Subject: [PATCH 3/3] fix: undefined uni --- components/hero/hero.tsx | 2 +- components/search/search.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/hero/hero.tsx b/components/hero/hero.tsx index 1c3ec0c..95cd26e 100644 --- a/components/hero/hero.tsx +++ b/components/hero/hero.tsx @@ -55,7 +55,7 @@ const Hero = () => { category: analyticsEnum.search.title, action: analyticsEnum.search.actions.SEARCH, label: university, - value: UNIVERSITY_GE.university.findIndex((item) => { + value: UNIVERSITY_GE[university].findIndex((item) => { return item.includes(ge); }), }); diff --git a/components/search/search.tsx b/components/search/search.tsx index 8b3ba60..f0089d4 100644 --- a/components/search/search.tsx +++ b/components/search/search.tsx @@ -176,7 +176,7 @@ const Search = () => { category: analyticsEnum.search.title, action: analyticsEnum.search.actions.SEARCH, label: university, - value: UNIVERSITY_GE.university.findIndex((item) => { + value: UNIVERSITY_GE[university].findIndex((item) => { return item.includes(ge); }), });