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: metrics integration #5

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@
"activeTab",
"identity",
"*://*.twitch.tv/*",
"https://twitch-extension.danielheart.dev/*",
"https://tbp.danielheart.dev/*",
"https://*.danielheart.dev/*",
"*://localhost/*",
"storage"
]
Expand Down
67 changes: 51 additions & 16 deletions src/resources/components/stats/stats.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
import { useStorage } from "@plasmohq/storage/dist/hook";
import { useEffect, useMemo, useState } from "react";
import { getMetrics } from "~services/user/user-consumer-service";
import type { AccessTokenResponse, MetricsResponse } from "~types/types";

const Stats = () => {
const statsData = {
hoursWatched: 254,
messages: 420,
topChannels: [
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
],
topCategories: [
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
],
};
const [authorization] = useStorage<AccessTokenResponse>("accessToken");

const [stats, setStats] = useState<MetricsResponse>();

useEffect(() => {
getMetrics(authorization?.access_token)
.then((res) => {
console.log(res);
DanielHe4rt marked this conversation as resolved.
Show resolved Hide resolved
setStats(res);
})
.catch((err) => {
console.error(err);
});
}, [authorization]);

const statsData = useMemo(() => {
if (!stats) {
return {
hoursWatched: 420,
messages: 420,
topChannels: [
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
],
topCategories: [
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
"https://static-cdn.jtvnw.net/jtv_user_pictures/f5c84939-a415-4654-b5da-60ff968280e6-profile_image-150x150.png",
],
};
}
DanielHe4rt marked this conversation as resolved.
Show resolved Hide resolved
return {
hoursWatched: stats.main_metrics.minutes_watched
? stats.main_metrics.minutes_watched * 60
DanielHe4rt marked this conversation as resolved.
Show resolved Hide resolved
: 0,
messages: 420,
topChannels: stats.user_metrics_by_channel.slice(0, 4).map((channel) => {
return `https://twitch-cdn.danielheart.dev/u/${channel.channel_id}.png`;
}),
topCategories: stats.user_metrics_by_category
.slice(0, 4)
.map((channel) => {
return `https://twitch-cdn.danielheart.dev/c/${channel.category_id}.png`;
}),
};
}, [stats]);
DanielHe4rt marked this conversation as resolved.
Show resolved Hide resolved

const StatItem = ({ label, value }) => (
<div className="w-1/2">
Expand Down
5 changes: 5 additions & 0 deletions src/scripting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export default class Kernel {
console.log("TBP: Checking for category changes...");
const channelName = extractUsername(window.location.href);

if (!channelName) {
return;
}

setTimeout(() => {
const categoryElement = document.querySelector(CATEGORY_ELEMENT_SELECTOR);
if (categoryElement) {
Expand All @@ -97,6 +101,7 @@ export default class Kernel {
}
// @ts-ignore
interval = setInterval(() => {
console.log(currentCategory);
DanielHe4rt marked this conversation as resolved.
Show resolved Hide resolved
browser.storage.sync.get("accessToken").then((res) => {
const authorization: AccessTokenResponse = JSON.parse(
res.accessToken,
Expand Down
20 changes: 19 additions & 1 deletion src/services/user/user-consumer-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { env } from "~config/env";
import type { AccessTokenResponse, TwitchUser, User } from "~types/types";
import type {
AccessTokenResponse,
MetricsResponse,
TwitchUser,
User,
} from "~types/types";

const API_URL = env.data.CONSUMER_API_URL;
const API_VERSION = env.data.CONSUMER_API_VERSION;
Expand Down Expand Up @@ -78,3 +83,16 @@ export async function sendHeartbeat(
body: JSON.stringify(payload),
});
}

export async function getMetrics(authentication: string) {
const uri = `${BASE_URL}/metrics/by-user`;

const response = await fetch(uri, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: authentication,
},
});
return (await response.json()) as MetricsResponse;
}
25 changes: 25 additions & 0 deletions src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,28 @@ export interface Account {
created_at: string;
updated_at: string;
}

// ----- Metrics
export interface UserMetrics {
user_id: number;
minutes_watched: number | null;
messages_count: number | null;
}

export interface UserMetricsByCategory {
user_id: number;
minutes_watched: number | null;
category_id: string;
}

export interface UserMetricsByChannel {
user_id: number;
minutes_watched: number | null;
channel_id: string;
}

export interface MetricsResponse {
main_metrics: UserMetrics;
user_metrics_by_channel: UserMetricsByChannel[];
user_metrics_by_category: UserMetricsByCategory[];
}
7 changes: 3 additions & 4 deletions src/utils/regex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const watchablePages = [
/^https:\/\/dashboard\.twitch\.tv\/u\/([^\/]+)\/stream-manager$/,
/^https:\/\/www\.twitch\.tv\/embed\/([^\/]+)\/chat.*$/,
const watchableUserPages = [
/^https:\/\/www\.twitch\.tv\/([^\/]+)$/,
//TODO: check if there's any other page link that should be included here
];

export function extractUsername(url) {
for (const regex of watchablePages) {
for (const regex of watchableUserPages) {
const match = url.match(regex);
if (match) {
return match[1]; // Extracted username
Expand Down