# WBC campaign tagging: selected source functions # Excerpt only; no network or Shopify write code. # Sample outputs on this portfolio were evaluated using these functions. from urllib.parse import parse_qs, urlparse SHOPIFY_TAG_MAX = 40 def parse_utms(landing_site): """Extract query parameters used for order attribution.""" if not landing_site: return {} url = landing_site if landing_site.startswith("http") else f"https://x.com{landing_site}" qs = parse_qs(urlparse(url).query, keep_blank_values=True) return {key: values[0] for key, values in qs.items()} def is_clean(value): """Reject empty values or un-substituted Meta macros like {{ad.name}}.""" return bool(value) and "{{" not in value and "}}" not in value def clip(tag): """Shopify enforces 40 chars per tag — truncate to fit.""" return tag[:SHOPIFY_TAG_MAX] def derive_tags(utms, referring_site): """Build the tag list this order should have based on its UTMs.""" tags = set() src = utms.get("utm_source", "").strip() med = utms.get("utm_medium", "").strip() camp = utms.get("utm_campaign", "").strip() content = utms.get("utm_content", "").strip() if is_clean(camp): tags.add(clip(f"campaign-{camp}")) if is_clean(content): tags.add(clip(f"ad-{content}")) if is_clean(src) and is_clean(med): tags.add(clip(f"source-{src}-{med}")) elif is_clean(src) and src == "ig": tags.add("source-ig-organic") if is_clean(utms.get("gclid", "").strip()) and not src: tags.add("source-google-cpc") # Catch IG referrer when UTMs are missing (organic IG link-in-bio) if ( not src and not utms.get("fbclid") and referring_site and "instagram.com" in referring_site.lower() ): tags.add("source-ig-organic") return tags