Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: tiktok analytics

Nevo David17bde5858c58
diff --git a/apps/frontend/src/components/analytics/chart-social.tsx b/apps/frontend/src/components/analytics/chart-social.tsx
index 801732559..a8c817336 100644
--- a/apps/frontend/src/components/analytics/chart-social.tsx
+++ b/apps/frontend/src/components/analytics/chart-social.tsx
@@ -5,6 +5,7 @@ import DrawChart from 'chart.js/auto';
import { TotalList } from '@gitroom/frontend/components/analytics/stars.and.forks.interface';
import { chunk } from 'lodash';
import useCookie from 'react-use-cookie';
+
function mergeDataPoints(data: TotalList[], numPoints: number): TotalList[] {
const res = chunk(data, Math.ceil(data.length / numPoints));
return res.map((row) => {
@@ -14,32 +15,63 @@ function mergeDataPoints(data: TotalList[], numPoints: number): TotalList[] {
};
});
}
+
export const ChartSocial: FC<{
data: TotalList[];
+ color?: 'purple' | 'green' | 'blue';
}> = (props) => {
- const { data } = props;
+ const { data, color = 'purple' } = props;
const [mode] = useCookie('mode', 'dark');
const list = useMemo(() => {
return mergeDataPoints(data, 7);
}, [data]);
const ref = useRef<any>(null);
const chart = useRef<null | DrawChart>(null);
+
+ const colorSchemes = {
+ purple: {
+ start: 'rgba(97, 43, 211, 0.8)',
+ end: 'rgba(97, 43, 211, 0.1)',
+ border: 'rgb(97, 43, 211)',
+ },
+ green: {
+ start: 'rgba(50, 213, 131, 0.8)',
+ end: 'rgba(50, 213, 131, 0.1)',
+ border: 'rgb(50, 213, 131)',
+ },
+ blue: {
+ start: 'rgba(29, 155, 240, 0.8)',
+ end: 'rgba(29, 155, 240, 0.1)',
+ border: 'rgb(29, 155, 240)',
+ },
+ };
+
+ const colors = colorSchemes[color];
+
useEffect(() => {
- const gradient = ref.current
- .getContext('2d')
- .createLinearGradient(0, 0, 0, ref.current.height);
- gradient.addColorStop(0, 'rgb(90,46,203)'); // Start color with some transparency
- gradient.addColorStop(1, 'rgb(65, 38, 136, 1)');
+ const ctx = ref.current.getContext('2d');
+ const gradient = ctx.createLinearGradient(0, 0, 0, ref.current.height);
+ gradient.addColorStop(0, colors.start);
+ gradient.addColorStop(1, colors.end);
+
chart.current = new DrawChart(ref.current!, {
type: 'line',
options: {
maintainAspectRatio: false,
responsive: true,
+ animation: {
+ duration: 750,
+ easing: 'easeOutQuart',
+ },
+ interaction: {
+ mode: 'index',
+ intersect: false,
+ },
layout: {
padding: {
left: 0,
right: 0,
- top: 0,
+ top: 4,
bottom: 0,
},
},
@@ -60,19 +92,43 @@ export const ChartSocial: FC<{
legend: {
display: false,
},
+ tooltip: {
+ enabled: true,
+ backgroundColor: mode === 'dark' ? '#1e1d1d' : '#fff',
+ titleColor: mode === 'dark' ? '#fff' : '#000',
+ bodyColor: mode === 'dark' ? '#9c9c9c' : '#777',
+ borderColor: mode === 'dark' ? '#2b2b2b' : '#e7e9eb',
+ borderWidth: 1,
+ padding: 10,
+ cornerRadius: 8,
+ displayColors: false,
+ titleFont: {
+ size: 12,
+ weight: 'normal',
+ },
+ bodyFont: {
+ size: 14,
+ weight: 'bold',
+ },
+ },
},
},
data: {
labels: list.map((row) => row.date),
datasets: [
{
- borderColor: mode === 'dark' ? '#fff' : '#000',
- // @ts-ignore
+ borderColor: colors.border,
+ borderWidth: 2,
label: 'Total',
backgroundColor: gradient,
fill: true,
- // @ts-ignore
data: list.map((row) => row.total),
+ tension: 0.4,
+ pointRadius: 0,
+ pointHoverRadius: 6,
+ pointHoverBackgroundColor: colors.border,
+ pointHoverBorderColor: mode === 'dark' ? '#1e1d1d' : '#fff',
+ pointHoverBorderWidth: 2,
},
],
},
@@ -81,5 +137,6 @@ export const ChartSocial: FC<{
chart?.current?.destroy();
};
}, []);
+
return <canvas className="w-full h-full" ref={ref} />;
};
diff --git a/apps/frontend/src/components/launches/statistics.tsx b/apps/frontend/src/components/launches/statistics.tsx
index 9054ec523..ff136f877 100644
--- a/apps/frontend/src/components/launches/statistics.tsx
+++ b/apps/frontend/src/components/launches/statistics.tsx
@@ -102,22 +102,41 @@ export const StatisticsModal: FC<{
</Select>
</div>
</div>
- <div className="grid grid-cols-3 gap-[20px]">
- {analyticsData.map((p: AnalyticsData, index: number) => (
- <div key={`analytics-${index}`} className="flex">
- <div className="flex-1 bg-newTableHeader rounded-[8px] py-[10px] px-[16px] gap-[10px] flex flex-col">
- <div className="flex items-center gap-[14px]">
- <div className="text-[20px]">{p.label}</div>
- </div>
- <div className="flex-1">
- <div className="h-[156px] relative">
- <ChartSocial data={p.data} key={`chart-${index}`} />
+ <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-[16px]">
+ {analyticsData.map((p: AnalyticsData, index: number) => {
+ const colorVariants = ['purple', 'green', 'blue'] as const;
+ const color = colorVariants[index % colorVariants.length];
+ return (
+ <div key={`analytics-${index}`} className="group">
+ <div className="flex flex-col h-full bg-newTableHeader border border-newTableBorder rounded-[12px] overflow-hidden transition-all duration-200 hover:border-[#612bd3]/50">
+ <div className="flex items-center justify-between px-[16px] pt-[14px] pb-[8px]">
+ <div className="flex items-center gap-[10px]">
+ <div
+ className={`w-[8px] h-[8px] rounded-full ${
+ color === 'purple' ? 'bg-[#612bd3]' : ''
+ } ${color === 'green' ? 'bg-[#32d583]' : ''} ${
+ color === 'blue' ? 'bg-[#1d9bf0]' : ''
+ }`}
+ />
+ <span className="text-[15px] font-medium text-newTableText">
+ {p.label}
+ </span>
+ </div>
+ </div>
+ <div className="flex-1 px-[12px] py-[8px]">
+ <div className="h-[120px] relative">
+ <ChartSocial data={p.data} color={color} key={`chart-${index}`} />
+ </div>
+ </div>
+ <div className="px-[16px] pb-[14px]">
+ <div className="text-[36px] leading-[42px] font-semibold tracking-tight">
+ {totals[index]}
+ </div>
</div>
</div>
- <div className="text-[50px] leading-[60px]">{totals[index]}</div>
</div>
- </div>
- ))}
+ );
+ })}
</div>
</div>
)}
diff --git a/apps/frontend/src/components/platform-analytics/render.analytics.tsx b/apps/frontend/src/components/platform-analytics/render.analytics.tsx
index e56a6a4da..c1cf6ca5b 100644
--- a/apps/frontend/src/components/platform-analytics/render.analytics.tsx
+++ b/apps/frontend/src/components/platform-analytics/render.analytics.tsx
@@ -5,6 +5,168 @@ import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { ChartSocial } from '@gitroom/frontend/components/analytics/chart-social';
import { LoadingComponent } from '@gitroom/frontend/components/layout/loading';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
+
+interface AnalyticsDataItem {
+ label: string;
+ data: Array<{ total: number; date: string }>;
+ average?: boolean;
+ percentageChange?: number;
+}
+
+const TrendIndicator: FC<{ value: number; average?: boolean }> = ({
+ value,
+ average,
+}) => {
+ if (value === 0) return null;
+
+ const isPositive = value > 0;
+ const displayValue = Math.abs(value).toFixed(1);
+
+ return (
+ <div
+ className={`flex items-center gap-[4px] text-[13px] font-medium ${
+ isPositive ? 'text-[#32d583]' : 'text-[#f97066]'
+ }`}
+ >
+ <svg
+ width="12"
+ height="12"
+ viewBox="0 0 12 12"
+ fill="none"
+ className={isPositive ? '' : 'rotate-180'}
+ >
+ <path
+ d="M6 2.5L10 7.5H2L6 2.5Z"
+ fill="currentColor"
+ />
+ </svg>
+ <span>
+ {displayValue}
+ {average ? 'pp' : '%'}
+ </span>
+ </div>
+ );
+};
+
+const AnalyticsCard: FC<{
+ item: AnalyticsDataItem;
+ total: string | number;
+ index: number;
+}> = ({ item, total, index }) => {
+ const colorVariants = ['purple', 'green', 'blue'] as const;
+ const color = colorVariants[index % colorVariants.length];
+
+ const hasMultipleDataPoints = item.data.length > 1;
+
+ return (
+ <div className="group relative">
+ <div
+ className={`
+ flex flex-col h-full
+ bg-newTableHeader
+ border border-newTableBorder
+ rounded-[12px]
+ overflow-hidden
+ transition-all duration-200
+ hover:border-[#612bd3]/50
+ `}
+ >
+ {/* Header */}
+ <div className="flex items-center justify-between px-[16px] pt-[14px] pb-[8px]">
+ <div className="flex items-center gap-[10px]">
+ <div
+ className={`
+ w-[8px] h-[8px] rounded-full
+ ${color === 'purple' ? 'bg-[#612bd3]' : ''}
+ ${color === 'green' ? 'bg-[#32d583]' : ''}
+ ${color === 'blue' ? 'bg-[#1d9bf0]' : ''}
+ `}
+ />
+ <span className="text-[15px] font-medium text-newTableText">
+ {item.label}
+ </span>
+ </div>
+ {item.percentageChange !== undefined && (
+ <TrendIndicator value={item.percentageChange} average={item.average} />
+ )}
+ </div>
+
+ {/* Content */}
+ {hasMultipleDataPoints ? (
+ <>
+ {/* Chart */}
+ <div className="flex-1 px-[12px] py-[8px]">
+ <div className="h-[120px] relative">
+ <ChartSocial data={item.data} color={color} key={`chart-${index}`} />
+ </div>
+ </div>
+
+ {/* Value */}
+ <div className="px-[16px] pb-[14px]">
+ <div className="text-[36px] leading-[42px] font-semibold tracking-tight">
+ {total}
+ </div>
+ </div>
+ </>
+ ) : (
+ /* Single value display */
+ <div className="flex-1 flex flex-col items-center justify-center py-[32px] px-[16px]">
+ <div className="text-[48px] leading-[56px] font-semibold tracking-tight">
+ {total}
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
+ );
+};
+
+const EmptyState: FC<{ onRefresh: () => void }> = ({ onRefresh }) => {
+ const t = useT();
+
+ return (
+ <div className="col-span-full flex flex-col items-center justify-center py-[48px] px-[24px] bg-newTableHeader border border-newTableBorder rounded-[12px]">
+ <div className="w-[48px] h-[48px] mb-[16px] rounded-full bg-[#612bd3]/10 flex items-center justify-center">
+ <svg
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ className="text-[#612bd3]"
+ >
+ <path d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
+ <path d="M12 8v4l2 2" />
+ </svg>
+ </div>
+ <p className="text-[15px] text-newTableText text-center mb-[12px]">
+ {t(
+ 'this_channel_needs_to_be_refreshed',
+ 'This channel needs to be refreshed to display analytics'
+ )}
+ </p>
+ <button
+ onClick={onRefresh}
+ className="inline-flex items-center gap-[6px] px-[16px] py-[8px] text-[14px] font-medium text-white bg-[#612bd3] hover:bg-[#5023b8] rounded-[8px] transition-colors"
+ >
+ <svg
+ width="16"
+ height="16"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ >
+ <path d="M23 4v6h-6M1 20v-6h6" />
+ <path d="M3.51 9a9 9 0 0114.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0020.49 15" />
+ </svg>
+ {t('refresh_channel', 'Refresh Channel')}
+ </button>
+ </div>
+ );
+};
+
export const RenderAnalytics: FC<{
integration: Integration;
date: number;
@@ -12,6 +174,7 @@ export const RenderAnalytics: FC<{
const { integration, date } = props;
const [loading, setLoading] = useState(true);
const fetch = useFetch();
+
const load = useCallback(async () => {
setLoading(true);
const load = (
@@ -20,6 +183,7 @@ export const RenderAnalytics: FC<{
setLoading(false);
return load;
}, [integration, date]);
+
const { data } = useSWR(`/analytics-${integration?.id}-${date}`, load, {
refreshInterval: 0,
refreshWhenHidden: false,
@@ -29,16 +193,17 @@ export const RenderAnalytics: FC<{
refreshWhenOffline: false,
revalidateOnMount: true,
});
+
const refreshChannel = useCallback(
(
- integration: Integration & {
+ integrationData: Integration & {
identifier: string;
}
) =>
async () => {
const { url } = await (
await fetch(
- `/integrations/social/${integration.identifier}?refresh=${integration.internalId}`,
+ `/integrations/social/${integrationData.identifier}?refresh=${integrationData.internalId}`,
{
method: 'GET',
}
@@ -51,62 +216,38 @@ export const RenderAnalytics: FC<{
const t = useT();
- const total = useMemo(() => {
- return data?.map((p: any) => {
+ const totals = useMemo(() => {
+ return data?.map((p: AnalyticsDataItem) => {
const value =
- (p?.data.reduce((acc: number, curr: any) => acc + curr.total, 0) || 0) /
+ (p?.data.reduce((acc: number, curr: { total: number }) => acc + curr.total, 0) || 0) /
(p.average ? p.data.length : 1);
if (p.average) {
return value.toFixed(2) + '%';
}
- return value;
+ return new Intl.NumberFormat().format(Math.round(value));
});
}, [data]);
+
if (loading) {
return (
- <>
+ <div className="flex items-center justify-center py-[48px]">
<LoadingComponent />
- </>
+ </div>
);
}
+
return (
- <div className="grid grid-cols-3 gap-[20px]">
+ <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-[16px]">
{data?.length === 0 && (
- <div>
- {t(
- 'this_channel_needs_to_be_refreshed',
- 'This channel needs to be refreshed,'
- )}
- <div
- className="underline hover:font-bold cursor-pointer"
- onClick={refreshChannel(integration as any)}
- >
- {t('click_here_to_refresh', 'click here to refresh')}
- </div>
- </div>
+ <EmptyState onRefresh={refreshChannel(integration as any)} />
)}
- {data?.map((p: any, index: number) => (
- <div key={`pl-${index}`} className="flex">
- <div className="flex-1 bg-newTableHeader rounded-[8px] py-[10px] px-[16px] gap-[10px] flex flex-col">
- <div className="flex items-center gap-[14px]">
- <div className="text-[20px]">{p.label}</div>
- </div>
- {p.data.length > 1 ? (
- <>
- <div className="flex-1">
- <div className="h-[156px] relative">
- <ChartSocial {...p} key={`p-${index}`} />
- </div>
- </div>
- <div className="text-[50px] leading-[60px]">{total[index]}</div>
- </>
- ) : (
- <div className="flex-1 flex flex-col items-center justify-center min-h-[216px]">
- <div className="text-[64px] leading-[72px] font-medium">{total[index]}</div>
- </div>
- )}
- </div>
- </div>
+ {data?.map((item: AnalyticsDataItem, index: number) => (
+ <AnalyticsCard
+ key={`analytics-${index}`}
+ item={item}
+ total={totals[index]}
+ index={index}
+ />
))}
</div>
);
diff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts
index 6f8f74c3e..032ee1dbe 100644
--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts
@@ -651,7 +651,7 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
}
result.push({
- label: 'Recent Views',
+ label: 'Views',
percentageChange: 0,
data: [{ total: String(totalViews), date: today }],
});
@@ -691,6 +691,29 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
): Promise<AnalyticsData[]> {
const today = dayjs().format('YYYY-MM-DD');
+ const post = await (
+ await this.fetch(
+ 'https://open.tiktokapis.com/v2/post/publish/status/fetch/',
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json; charset=UTF-8',
+ Authorization: `Bearer ${accessToken}`,
+ },
+ body: JSON.stringify({
+ publish_id: postId,
+ }),
+ },
+ '',
+ 0,
+ true
+ )
+ ).json();
+
+ if (!post?.data?.publicaly_available_post_id?.[0]) {
+ return [];
+ }
+
try {
// Query video details using the video ID
const response = await this.fetch(
@@ -702,7 +725,9 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
- filters: { video_ids: [postId] },
+ filters: {
+ video_ids: post?.data?.publicaly_available_post_id.map(String),
+ },
}),
}
);
diff --git a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
index e96395ae8..6d2151df8 100644
--- a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
@@ -511,7 +511,7 @@ export class XProvider extends SocialAbstract implements SocialProvider {
}
const until = dayjs().endOf('day');
- const since = dayjs().subtract(date, 'day');
+ const since = dayjs().subtract(date > 100 ? 100 : date, 'day');
const [accessTokenSplit, accessSecretSplit] = accessToken.split(':');
const client = new TwitterApi({