Skip to content

Conversation

@bokelley
Copy link
Contributor

@bokelley bokelley commented Nov 3, 2025

Summary

Fixes JavaScript errors in the targeting browser and product pages when loading targeting keys and values.

Problem 1: TypeError in targeting browser

The loadTargetingData() function was replacing the entire targetingData object with the API response:

targetingData = data;  // ❌ Replaces entire object

The API response only includes customKeys, audiences, labels, and last_sync - it doesn't include customValues. This meant that when a user clicked on a targeting key, the code tried to access targetingData.customValues[keyId], which was undefined, causing:

TypeError: undefined is not an object (evaluating 'targetingData.customValues[keyId] = data.values || []')

Problem 2: 401 errors on product pages

The targeting selector widget on product add/edit pages was making API calls without the {{ script_name }} prefix, causing 401 unauthorized errors in subdomain-based tenant routing. The dropdown would show "-- Select a key --" but wouldn't load any targeting keys.

Solutions

Fix 1: Preserve customValues cache (targeting browser)

Changed to merge the API data into existing targetingData properties instead of replacing the whole object:

// ✅ Merge properties to preserve customValues cache
targetingData.customKeys = data.customKeys || [];
targetingData.audiences = data.audiences || [];
targetingData.labels = data.labels || [];
// Don't overwrite customValues - populated on-demand by loadCustomValues()

This preserves the customValues property, which acts as a cache and gets populated on-demand when users click on targeting keys.

Fix 2: Add script_name prefix (product pages)

Added {{ script_name }} prefix to API endpoints in targeting_selector_simple.html:

// Before: /api/tenant/${tenantId}/targeting/all
// After:  {{ script_name }}/api/tenant/${tenantId}/targeting/all

This matches the pattern used in targeting_browser.html and fixes the 401 routing errors.

Test plan

  • Load targeting browser page
  • Verify targeting keys are displayed
  • Click on a targeting key
  • Verify values load successfully without TypeError
  • Click on multiple keys to verify caching works
  • Load product add/edit page
  • Verify targeting keys dropdown loads properly
  • Select a key and verify values load
  • All tests pass locally (861 unit, 32 integration, 28 integration_v2)

🤖 Generated with Claude Code

bokelley and others added 2 commits November 3, 2025 09:46
The loadTargetingData function was replacing the entire targetingData
object with API response data, which didn't include the customValues
property. This caused a TypeError when clicking on targeting keys:

  TypeError: undefined is not an object (evaluating
  'targetingData.customValues[keyId] = data.values || []')

Fixed by merging API data into targetingData properties instead of
replacing the whole object, preserving the customValues cache that
gets populated on-demand by loadCustomValues().

Fixes: TypeError when clicking targeting keys to load values

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

Co-Authored-By: Claude <noreply@anthropic.com>
The targeting selector widget on product pages was making API calls
without the script_name prefix, causing 401 errors in subdomain-based
tenant routing. The dropdown showed "-- Select a key --" but wouldn't
load any targeting keys.

Fixed by adding {{ script_name }} prefix to both API endpoints:
- /api/tenant/${tenantId}/targeting/all (for loading keys)
- /api/tenant/${tenantId}/targeting/values/${keyId} (for loading values)

This matches the pattern used in targeting_browser.html.

Fixes: Targeting keys dropdown not loading on product add/edit pages

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

Co-Authored-By: Claude <noreply@anthropic.com>
@bokelley bokelley changed the title fix: preserve customValues cache when loading targeting data fix: targeting keys errors in browser and product pages Nov 3, 2025
@bokelley bokelley merged commit 7fc3603 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
…tocol#685)

* fix: preserve customValues cache when loading targeting data

The loadTargetingData function was replacing the entire targetingData
object with API response data, which didn't include the customValues
property. This caused a TypeError when clicking on targeting keys:

  TypeError: undefined is not an object (evaluating
  'targetingData.customValues[keyId] = data.values || []')

Fixed by merging API data into targetingData properties instead of
replacing the whole object, preserving the customValues cache that
gets populated on-demand by loadCustomValues().

Fixes: TypeError when clicking targeting keys to load values

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

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: add script_name prefix to targeting API URLs in product page

The targeting selector widget on product pages was making API calls
without the script_name prefix, causing 401 errors in subdomain-based
tenant routing. The dropdown showed "-- Select a key --" but wouldn't
load any targeting keys.

Fixed by adding {{ script_name }} prefix to both API endpoints:
- /api/tenant/${tenantId}/targeting/all (for loading keys)
- /api/tenant/${tenantId}/targeting/values/${keyId} (for loading values)

This matches the pattern used in targeting_browser.html.

Fixes: Targeting keys dropdown not loading on product add/edit pages

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

Co-Authored-By: Claude <noreply@anthropic.com>

---------

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