Skip to content

Commit

Permalink
feat: add individual search events (#31)
Browse files Browse the repository at this point in the history
* feat: add individual search events

* doc: fix doc in analyticsa

* fix: undefined uni
  • Loading branch information
KevinWu098 authored Dec 28, 2023
1 parent 84c7a88 commit f530f98
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
11 changes: 11 additions & 0 deletions components/hero/hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions components/search/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand Down
28 changes: 28 additions & 0 deletions lib/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const analyticsEnum = {
search: {
title: "Search Page",
actions: {
SEARCH: "Search Course", // Label is school, value is index of GE
},
},
};

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,
});
}
18 changes: 0 additions & 18 deletions lib/gtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

0 comments on commit f530f98

Please sign in to comment.