fix: 支持将 site_token 解析为 site_tag 以兼容 Cloudflare RUM 查询
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
1 parent
d54133d44d
commit
e9a5177a2f
1 file changed
+33
-2
@@ -36,6 +36,36 @@ async function graphql<T>(
|
|||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getSiteTagByToken(
|
||||||
|
apiToken: string,
|
||||||
|
accountId: string,
|
||||||
|
siteToken: string
|
||||||
|
): Promise<string> {
|
||||||
|
if (siteToken.length !== 32) return siteToken; // It's probably already a site_tag
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`https://api.cloudflare.com/client/v4/accounts/${accountId}/rum/site_info`,
|
||||||
|
{
|
||||||
|
headers: { Authorization: `Bearer ${apiToken}` }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (!response.ok) return siteToken;
|
||||||
|
const data = (await response.json()) as {
|
||||||
|
success: boolean;
|
||||||
|
result: { site_tag: string; site_token: string }[];
|
||||||
|
};
|
||||||
|
if (data.success && Array.isArray(data.result)) {
|
||||||
|
const matched = data.result.find(
|
||||||
|
(s) => s.site_token === siteToken || s.site_tag === siteToken
|
||||||
|
);
|
||||||
|
if (matched) return matched.site_tag;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to resolve site_tag from token', e);
|
||||||
|
}
|
||||||
|
return siteToken;
|
||||||
|
}
|
||||||
|
|
||||||
export async function purgePublicCache(platform: App.Platform | undefined): Promise<string | null> {
|
export async function purgePublicCache(platform: App.Platform | undefined): Promise<string | null> {
|
||||||
const env = platform?.env as AnalyticsEnv | undefined;
|
const env = platform?.env as AnalyticsEnv | undefined;
|
||||||
let error: string | null = null;
|
let error: string | null = null;
|
||||||
@@ -262,6 +292,7 @@ async function fetchAudience(
|
|||||||
siteTag: string,
|
siteTag: string,
|
||||||
now: Date
|
now: Date
|
||||||
): Promise<AnalyticsData['audience']> {
|
): Promise<AnalyticsData['audience']> {
|
||||||
|
const realSiteTag = await getSiteTagByToken(token, accountId, siteTag);
|
||||||
const thirtyDaysAgo = new Date(now.getTime() - 30 * 86400000).toISOString().split('T')[0];
|
const thirtyDaysAgo = new Date(now.getTime() - 30 * 86400000).toISOString().split('T')[0];
|
||||||
try {
|
try {
|
||||||
const data = await graphql<{
|
const data = await graphql<{
|
||||||
@@ -282,7 +313,7 @@ async function fetchAudience(
|
|||||||
accounts(filter: { accountTag: $account }) {
|
accounts(filter: { accountTag: $account }) {
|
||||||
rumPageloadEventsAdaptiveGroups(
|
rumPageloadEventsAdaptiveGroups(
|
||||||
limit: 30
|
limit: 30
|
||||||
filter: { siteToken: $site, datetime_geq: $date }
|
filter: { siteTag: $site, datetime_geq: $date }
|
||||||
orderBy: [date_ASC]
|
orderBy: [date_ASC]
|
||||||
) {
|
) {
|
||||||
count
|
count
|
||||||
@@ -299,7 +330,7 @@ async function fetchAudience(
|
|||||||
`,
|
`,
|
||||||
{
|
{
|
||||||
account: accountId,
|
account: accountId,
|
||||||
site: siteTag,
|
site: realSiteTag,
|
||||||
date: `${thirtyDaysAgo}T00:00:00Z`
|
date: `${thirtyDaysAgo}T00:00:00Z`
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user