feat(analytics): Google Analytics・同意モード・一部機能のトラッキング実装 (MisskeyIO#784)

This commit is contained in:
あわわわとーにゅ 2024-11-06 01:28:14 +09:00 committed by GitHub
parent 2f4c48bbe6
commit fcfd004c38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
53 changed files with 805 additions and 113 deletions

View file

@ -0,0 +1,61 @@
/* eslint-disable id-denylist */
// buffering usage report data for 1 minute, then sending it to the server
// POST /api/usage [ { t: number, e: string, i: string, a: string } ]
// t: timestamp
// e: event type
// i: event initiator
// a: action
import { generateClientTransactionId } from '@/scripts/misskey-api.js';
import { miLocalStorage } from '@/local-storage.js';
import { GtagConsentParams } from 'vue-gtag';
import { instance } from '@/instance.js';
export interface UsageReport {
t: number;
e: string;
i: string;
a: string;
}
let disableUsageReport = !instance.googleAnalyticsId;
const usageReportBuffer: UsageReport[] = [];
let usageReportBufferTimer: number | null = null;
export function usageReport(data: UsageReport) {
if (disableUsageReport) return;
if (usageReportBuffer.length > 0) {
const last = usageReportBuffer[usageReportBuffer.length - 1];
if (last.t === data.t && last.e === data.e && last.a === data.a) return;
}
usageReportBuffer.push(data);
if (usageReportBufferTimer === null) {
usageReportBufferTimer = window.setTimeout(() => {
sendUsageReport();
}, 60 * 1000);
}
}
export function sendUsageReport() {
if (usageReportBuffer.length === 0) return;
const data = usageReportBuffer.splice(0, usageReportBuffer.length);
usageReportBufferTimer = null;
if ((miLocalStorage.getItemAsJson('gtagConsent') as GtagConsentParams)?.ad_user_data !== 'granted') {
console.log('Usage report is not sent because the user has not consented to sharing data about ad interactions.');
disableUsageReport = true;
return;
}
window.fetch('/api/usage', {
method: 'POST',
body: JSON.stringify(data),
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
'X-Client-Transaction-Id': generateClientTransactionId('misskey'),
},
});
}