← back to Ga Allsites
create_dw_ga4.py
48 lines
#!/usr/bin/env python3
"""Create the 2 pending DW GA4 properties in the DesignerWallcoverings.com
account (15714274) — where the analytics SA already has Editor. Steve-run.
Writes the new G-IDs back into the analytics rollout pending file.
Usage: python3 ~/Projects/ga-allsites/create_dw_ga4.py
"""
import json
import pathlib
from google.analytics.admin import AnalyticsAdminServiceClient, DataStream, Property
KEY = pathlib.Path.home() / ".config" / "ga-analytics-agent" / "service-account.json"
PENDING = pathlib.Path.home() / ".claude" / "skills" / "analytics" / "rollout" / "pending_ga.json"
ACCOUNT = "accounts/15714274" # DesignerWallcoverings.com — SA has Editor here
TARGETS = [
("Commercial Wallcovering", "commercialwallcovering.com"),
("Hollywood Wallcovering", "hollywoodwallcovering.com"),
]
client = AnalyticsAdminServiceClient.from_service_account_file(str(KEY))
created = {}
for display, domain in TARGETS:
try:
prop = client.create_property(property=Property(
parent=ACCOUNT, display_name=display,
time_zone="America/Los_Angeles", currency_code="USD",
industry_category="ONLINE_COMMUNITIES"))
stream = client.create_data_stream(parent=prop.name, data_stream=DataStream(
type_=DataStream.DataStreamType.WEB_DATA_STREAM,
display_name=f"{display} Web",
web_stream_data=DataStream.WebStreamData(default_uri=f"https://{domain}")))
mid = stream.web_stream_data.measurement_id
created[domain] = mid
print(f"CREATED {domain:30} {mid} ({prop.name})")
except Exception as e: # noqa: BLE001
print(f"ERROR {domain:30} {type(e).__name__}: {str(e)[:140]}")
# write the measurement ids back into pending_ga.json
if created and PENDING.exists():
data = json.loads(PENDING.read_text())
for s in data.get("sites", []):
if s.get("domain") in created and not s.get("measurement_id"):
s["measurement_id"] = created[s["domain"]]
PENDING.write_text(json.dumps(data, indent=2))
print(f"\nWrote {len(created)} G-ID(s) back to {PENDING}")
print("\nNext: inject gtag via the staged runbook, then rebuild ga-allsites cache.")