Skip to content

Conversation

@bokelley
Copy link
Contributor

@bokelley bokelley commented Nov 3, 2025

Summary

Fixes the complete end-to-end flow for custom targeting configuration on products, ensuring targeting configured in the UI is properly applied to GAM line items.

Problem

The targeting selector widget was working (loading keys and values), but the custom targeting wasn't actually being applied to GAM line items because:

  1. Product creation/edit ignored the form field: The targeting_template form field from the widget was never parsed - it was hardcoded to {}
  2. Schema mismatch: Widget stores data in targeting_template.key_value_pairs, but GAM adapter reads from implementation_config.custom_targeting_keys

Solution

1. Parse targeting_template from form data (both add and edit)

Before:

"targeting_template": {},  # ❌ Hardcoded empty dict

After:

targeting_template_json = form_data.get("targeting_template", "{}")
targeting_template = json.loads(targeting_template_json) if targeting_template_json else {}

2. Copy key-value pairs to GAM implementation config

# If targeting template has key_value_pairs, copy to implementation_config for GAM
if targeting_template.get("key_value_pairs"):
    if "custom_targeting_keys" not in base_config:
        base_config["custom_targeting_keys"] = {}
    # Merge key-value pairs into implementation_config for GAM adapter
    base_config["custom_targeting_keys"].update(targeting_template["key_value_pairs"])

3. Persist both fields

product.targeting_template = targeting_template
product.implementation_config = base_config
attributes.flag_modified(product, "targeting_template")
attributes.flag_modified(product, "implementation_config")

Data Flow

User selects targeting in UI widget
  ↓
Widget stores in targeting_template.key_value_pairs
  ↓
Backend parses form data (THIS PR)
  ↓
Copies to implementation_config.custom_targeting_keys
  ↓
GAM adapter reads custom_targeting_keys
  ↓
Applied to GAM line item targeting

Test plan

  • Load product add page
  • Select custom targeting keys and values
  • Submit form and verify targeting_template is saved
  • Verify implementation_config.custom_targeting_keys is populated
  • Edit existing product with targeting
  • Verify changes are persisted
  • Create media buy with product
  • Verify GAM line item has custom targeting applied
  • All tests pass (861 unit, 32 integration, 28 integration_v2)

Related PRs

This PR completes the targeting integration by connecting the UI to the GAM adapter.

🤖 Generated with Claude Code

…items

The targeting selector widget on product pages was storing custom
targeting key-value pairs in targeting_template.key_value_pairs, but
the product creation/edit code was not reading this form field.
Additionally, the GAM adapter looks for custom targeting in
implementation_config.custom_targeting_keys, creating a disconnect.

Fixed by:
1. Parsing targeting_template JSON from form data (previously hardcoded to {})
2. Copying key_value_pairs to implementation_config.custom_targeting_keys
3. Flagging both fields as modified for SQLAlchemy to persist changes
4. Applied to both add_product() and edit_product() endpoints

This ensures custom targeting configured via the UI is properly
applied to GAM line items when media buys are created.

Fixes: Custom targeting not being applied to GAM line items

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@bokelley bokelley merged commit a1132ae into main Nov 3, 2025
10 checks passed
danf-newton pushed a commit to Newton-Research-Inc/salesagent that referenced this pull request Nov 24, 2025
…items (adcontextprotocol#686)

The targeting selector widget on product pages was storing custom
targeting key-value pairs in targeting_template.key_value_pairs, but
the product creation/edit code was not reading this form field.
Additionally, the GAM adapter looks for custom targeting in
implementation_config.custom_targeting_keys, creating a disconnect.

Fixed by:
1. Parsing targeting_template JSON from form data (previously hardcoded to {})
2. Copying key_value_pairs to implementation_config.custom_targeting_keys
3. Flagging both fields as modified for SQLAlchemy to persist changes
4. Applied to both add_product() and edit_product() endpoints

This ensures custom targeting configured via the UI is properly
applied to GAM line items when media buys are created.

Fixes: Custom targeting not being applied to GAM line items

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants