← back to Ga Allsites
gate1_create_fleet_property.py
50 lines
import os, pathlib, json
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=str(pathlib.Path.home()/".config"/"ga-analytics-agent"/"service-account.json")
from google.analytics.admin import AnalyticsAdminServiceClient
from google.analytics.admin_v1alpha import types as t
c=AnalyticsAdminServiceClient()
ACCT="accounts/371997142" # Steve Abrams APPS (where all DW properties live)
# Idempotency guard: never create a duplicate.
existing=None
for a in c.list_account_summaries():
for ps in a.property_summaries:
if ps.display_name.strip().lower()=="dw fleet microsites":
existing=ps.property
if existing:
# fetch its measurement id
mid=""
for ds in c.list_data_streams(parent=existing):
w=getattr(ds,"web_stream_data",None)
if w and w.measurement_id: mid=w.measurement_id
print(f"ALREADY EXISTS: {existing} MEASUREMENT_ID: {mid} -- not creating a duplicate.")
raise SystemExit(0)
# 1) property
prop=t.Property(display_name="DW Fleet Microsites", time_zone="America/Los_Angeles",
currency_code="USD", industry_category=t.IndustryCategory.SHOPPING, parent=ACCT)
p=c.create_property(property=prop); pid=p.name.split("/")[-1]
print(f"PROPERTY_CREATED: {p.name} display='{p.display_name}'")
# 2) web data stream -> measurement id
ds=t.DataStream(display_name="DW Fleet Microsites Web",
type_=t.DataStream.DataStreamType.WEB_DATA_STREAM,
web_stream_data=t.DataStream.WebStreamData(default_uri="https://designerwallcoverings.com"))
s=c.create_data_stream(parent=p.name, data_stream=ds); mid=s.web_stream_data.measurement_id
print(f"DATA_STREAM_CREATED: {s.name}")
print(f"MEASUREMENT_ID: {mid}")
# 3) event-scoped 'vendor' custom dimension
dim=t.CustomDimension(parameter_name="vendor", display_name="Vendor",
description="Brand vendor slug (e.g. kravet, carnegie, thibaut)",
scope=t.CustomDimension.DimensionScope.EVENT)
cd=c.create_custom_dimension(parent=p.name, custom_dimension=dim)
print(f"CUSTOM_DIMENSION_CREATED: param='{cd.parameter_name}' scope={cd.scope.name}")
print("\n=== GATE 1 COMPLETE ===")
print(json.dumps({"property":p.name,"property_id":pid,"measurement_id":mid,"account":ACCT}, indent=2))
# persist for GATE 2
out=pathlib.Path.home()/ "Projects"/"ga-allsites"/"gate1_result.json"
out.write_text(json.dumps({"property":p.name,"property_id":pid,"measurement_id":mid}, indent=2))
print(f"\nsaved -> {out}")