Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add individual search events #31

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
};