Skip to content

Commit

Permalink
feat: analytics events
Browse files Browse the repository at this point in the history
* chore: standard formatting

* chore: format

* feat: analytics events
  • Loading branch information
ricardocasares authored Feb 5, 2021
1 parent c5b2b5f commit a2688a7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
23 changes: 21 additions & 2 deletions src/lib/hooks/useFavorites.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import { useContext } from "react";
import { event } from "@/lib/utils";
import { FavoritesContext } from "@/lib/context/favorites";

export function useFavorites() {
const { favs, setFavs } = useContext(FavoritesContext);

const add = (id: string) => setFavs([...favs, id]);
const remove = (id: string) => setFavs(favs.filter((f) => id !== f));
const add = (id: string) => {
setFavs([...favs, id]);
event({
action: "fav",
label: "Radio faved",
category: "favorites",
value: id,
});
};

const remove = (id: string) => {
setFavs(favs.filter((f) => id !== f));
event({
action: "unfav",
label: "Radio unfaved",
category: "favorites",
value: id,
});
};

const isFaved = (id: string) => favs.includes(id);

return { favs, add, remove, isFaved };
Expand Down
64 changes: 41 additions & 23 deletions src/lib/hooks/usePlayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useContext } from "react";
import { event } from "@/lib/utils";
import { PlayerContext } from "@/lib/context/player";
import { AudioElementContext } from "@/lib/context/audio";
import { Station } from "@/lib/graphql/gqless/generated";
Expand All @@ -16,6 +17,14 @@ export function usePlayer() {
const load = (station: Station) => {
if (audio.current.src !== station.url) {
audio.current.src = station.url;

event({
action: "load",
label: "Radio load",
category: "player",
value: station.stationuuid,
});

setState((state) => ({
...state,
error: false,
Expand All @@ -29,6 +38,7 @@ export function usePlayer() {

const stop = () => {
audio.current.src = EMPTY_AUDIO;

setState({
error: false,
paused: false,
Expand All @@ -39,13 +49,22 @@ export function usePlayer() {
};

const onPlaying = () =>
setState((state) => ({
...state,
error: false,
paused: false,
loading: false,
playing: true,
}));
setState((state) => {
event({
action: "play",
label: "Radio playing",
category: "player",
value: state?.station?.stationuuid,
});

return {
...state,
error: false,
paused: false,
loading: false,
playing: true,
};
});

const onPause = () =>
setState((state) => ({
Expand Down Expand Up @@ -75,26 +94,25 @@ export function usePlayer() {
}));

const onError = () =>
setState((state) => ({
...state,
error: true,
paused: false,
loading: false,
playing: false,
}));
setState((state) => {
event({
action: "error",
label: "Player error",
category: "player",
value: state?.station?.stationuuid,
});

const onEnded = () =>
setState((state) => ({
...state,
error: false,
paused: false,
playing: false,
station: null,
}));
return {
...state,
error: true,
paused: false,
loading: false,
playing: false,
};
});

useEffect(() => {
audio.current.addEventListener("error", onError);
audio.current.addEventListener("ended", onEnded);
audio.current.addEventListener("pause", onPause);
audio.current.addEventListener("playing", onPlaying);
audio.current.addEventListener("loadstart", onLoadStart);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ export function mapValueToRange(
return ((n - a) * (y - x)) / (b - a) + x;
}

export const pageview = (url) => {
export const pageview = (page_path: string) => {
// @ts-expect-error
window.gtag("config", process.env.NEXT_PUBLIC_GA_TRACKING_ID, {
page_path: url,
page_path,
});
};

export const event = ({ action, category, label, value }) => {
// @ts-expect-error
window.gtag("event", action, {
event_category: category,
value,
event_label: label,
value: value,
event_category: category,
});
};

1 comment on commit a2688a7

@vercel
Copy link

@vercel vercel bot commented on a2688a7 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.