feat(seo-worker): collect cached read-only Clarity aggregates
This commit is contained in:
1 parent
13dc74807a
commit
db3979d212
10 files changed
+455
-4
No files matched your search
@@ -9,6 +9,7 @@ from pathlib import Path
|
||||
import sqlite3
|
||||
from urllib.parse import quote, urlsplit
|
||||
import requests
|
||||
from clarity_collector import ClarityCollector, CLARITY_ENDPOINT, load_token
|
||||
|
||||
GSC_CREDS_PATH='/opt/data/credentials/gsc-service-account.json'
|
||||
GA4_PROPERTY_ID='552453291'
|
||||
@@ -114,13 +115,13 @@ class Backend:
|
||||
def decision(finding,sources):
|
||||
return {'action':'INSUFFICIENT_EVIDENCE','reason':'No approved comparative decision policy; unavailable signals cannot justify scaling or pruning. Correlation is not keyword-caused sales.','mandatoryDirective':finding.get('ceoFeedback'),'feedbackRevision':finding.get('feedbackVersion',finding.get('feedbackRevision',finding.get('feedbackSubmittedAt')))}
|
||||
|
||||
def collect_finding(finding,collector,dates,store):
|
||||
def collect_finding(finding,collector,dates,store,clarity=None):
|
||||
blog=finding.get('blog') or {}; url=finding.get('articleUrl') or blog.get('canonicalUrl')
|
||||
if not url and blog.get('slug'): url='https://canina.ir/blog/'+quote(blog['slug'],safe='')
|
||||
if not url or urlsplit(url).hostname!='canina.ir' or urlsplit(url).scheme!='https':
|
||||
store.alert('BACKEND','missing_or_invalid_article_url',finding['id']); return None
|
||||
sources={'gsc':collector.gsc_page(url,dates),'gscQueries':collector.gsc(url,dates),'ga4':collector.ga4(url,dates),'clarity':unavailable('reporting_credentials_not_configured'),'commerce':unavailable('article_to_order_linkage_not_verified')}
|
||||
payload={'findingId':finding['id'],'articleUrl':url,'periodStart':dates[0],'periodEnd':dates[1],'feedbackRevision':finding.get('feedbackVersion',finding.get('feedbackRevision',finding.get('feedbackSubmittedAt'))),'sources':sources,'decision':decision(finding,sources),'observedAt':now(),'periodAlignment':'same date labels; GSC Pacific and GA4 property timezone may differ; not session-level alignment'}
|
||||
sources={'gsc':collector.gsc_page(url,dates),'gscQueries':collector.gsc(url,dates),'ga4':collector.ga4(url,dates),'clarity':clarity.collect(url) if clarity else unavailable('collector_not_configured'),'commerce':unavailable('article_to_order_linkage_not_verified')}
|
||||
payload={'findingId':finding['id'],'articleUrl':url,'periodStart':dates[0],'periodEnd':dates[1],'feedbackRevision':finding.get('feedbackVersion',finding.get('feedbackRevision',finding.get('feedbackSubmittedAt'))),'sources':sources,'decision':decision(finding,sources),'observedAt':now(),'periodAlignment':'same date labels; GSC Pacific and GA4 property timezone may differ; not session-level alignment; Clarity has its own independent rolling UTC window'}
|
||||
for source,data in sources.items():
|
||||
if data['status']=='unavailable': store.alert(source.upper(),data['error'],finding['id'])
|
||||
store.snapshot(payload); return payload
|
||||
@@ -135,6 +136,8 @@ def snapshot_body(payload,key):
|
||||
if available:
|
||||
for k,v in mapping.items(): body[v]=metrics[k]
|
||||
else: metadata[source]['error']=data.get('error','no_reported_metrics')
|
||||
clarity=sources.get('clarity') or unavailable('missing_source')
|
||||
metadata['clarity']=dict(clarity,source='clarity',fetchedAt=clarity.get('observedAt',now()))
|
||||
metadata['worker']={'feedbackVersion':payload.get('feedbackRevision'),'mandatoryDirective':payload.get('decision',{}).get('mandatoryDirective'),'periodAlignment':payload.get('periodAlignment'),'sources':sources}
|
||||
body['sourceMetadata']=metadata
|
||||
return body
|
||||
@@ -183,8 +186,9 @@ def main():
|
||||
except Exception as exc:
|
||||
store.alert('BACKEND',type(exc).__name__,'global'); Path(args.receipt).write_text(json.dumps({'status':'blocked','error':type(exc).__name__,'observedAt':now()})); return 1
|
||||
else: p.error('--backend or --probe-url required')
|
||||
clarity=ClarityCollector(store,requests.Session(),load_token(),'canina.ir')
|
||||
for finding in findings:
|
||||
if finding.get('blog') or finding.get('articleUrl'): results.append(collect_finding(finding,collector,dates,store))
|
||||
if finding.get('blog') or finding.get('articleUrl'): results.append(collect_finding(finding,collector,dates,store,clarity))
|
||||
Path(args.receipt).write_text(json.dumps({'observedAt':now(),'period':dates,'findingCount':len(findings),'collectedCount':sum(x is not None for x in results),'results':results,'remoteWrites':False},ensure_ascii=False,indent=2))
|
||||
return 0
|
||||
if __name__=='__main__': raise SystemExit(main())
|
||||
Reference in new issue
Block a user