# Widget Troubleshooting Guide

If your website widget is not appearing on your landing page, follow these troubleshooting steps:

## Quick Test

Visit the widget test page to verify your widget is working:
- **Test URL**: `https://www.halavoice.store/widget-test`
- Enter your widget token to test your specific widget

## Common Issues and Solutions

### 1. Widget Not Showing Up

**Check Browser Console**
- Open browser DevTools (F12)
- Look for errors prefixed with `[VoiceWidget]`
- Common errors:
  - `Failed to load config`: Check your widget token
  - `Failed to fetch`: CORS or network issue
  - `Widget not found`: Invalid or inactive widget

**Verify Embed Code**
```html
<script>
  (function(w,d,s,o,f,js){
    w[o]=w[o]||function(){(w[o].q=w[o].q||[]).push(arguments)};
    js=d.createElement(s);js.id=o;js.src=f;js.async=1;
    (d.head||d.body).appendChild(js);
  }(window,document,'script','vw','https://www.halavoice.store/widget/embed.js'));
  vw('init', 'YOUR_WIDGET_TOKEN_HERE');
</script>
```

**Common Embed Code Mistakes**
- Missing closing `</script>` tag
- Incorrect widget token
- Script placed inside another `<script>` tag
- Using `src` attribute with inline script code

### 2. Widget Disabled

If the widget button appears but is disabled:

**Check Widget Status**
1. Go to https://www.halavoice.store/app/tools/widgets
2. Click on your widget
3. Check the "Status" dropdown - should be "Active"

**Check Business Hours**
If business hours are enabled:
1. Go to Widget Settings → Business Hours
2. Verify current time is within business hours
3. Or disable business hours for 24/7 availability

**Check Credits**
- Ensure your account has sufficient credits
- Check credit balance in Settings → Billing

### 3. CORS Errors

If you see CORS errors in the console:

**Allowed Domains**
1. Go to your widget settings
2. Under "Allowed Domains", add your website domain
3. Format: `example.com` (without https://)
4. Wildcards: `*.example.com`

**Leave empty for any domain** (not recommended for production)

### 4. CSS Conflicts

If the widget is loading but not visible:

**Check z-index**
The widget uses `z-index: 999999`. If other elements have higher z-index, the widget may be hidden behind them.

**Check position**
The widget is `position: fixed; bottom: 24px; right: 24px;`. Ensure no CSS overrides this.

**Test in isolation**
```html
<!DOCTYPE html>
<html>
<head>
    <title>Widget Test</title>
</head>
<body>
    <h1>Widget Test</h1>
    <p>Widget should appear in bottom-right corner.</p>
    
    <script>
      (function(w,d,s,o,f,js){
        w[o]=w[o]||function(){(w[o].q=w[o].q||[]).push(arguments)};
        js=d.createElement(s);js.id=o;js.src=f;js.async=1;
        (d.head||d.body).appendChild(js);
      }(window,document,'script','vw','https://www.halavoice.store/widget/embed.js'));
      vw('init', 'YOUR_WIDGET_TOKEN');
    </script>
</body>
</html>
```

### 5. Network Issues

**Check Firewall/CDN**
- Ensure your firewall allows requests to `halavoice.store`
- If using CDN, ensure it's not blocking the widget script

**Check CSP Headers**
If you use Content Security Policy:
```
script-src 'self' 'unsafe-inline' https://halavoice.store;
connect-src 'self' https://halavoice.store;
img-src 'self' https://halavoice.store;
```

## Debug Logging

The widget now includes debug logging. Open the browser console (F12) and look for:

```
[VoiceWidget] Initializing widget with token: wgt_xxx
[VoiceWidget] Loading config for token: wgt_xxx
[VoiceWidget] Base URL: https://halavoice.store
[VoiceWidget] Config response status: 200
[VoiceWidget] Config loaded: { ... }
[VoiceWidget] Loading branding from: https://halavoice.store/api/branding
[VoiceWidget] Branding loaded: { ... }
[VoiceWidget] Creating widget...
[VoiceWidget] Widget container appended to body
[VoiceWidget] Widget creation complete
```

If any of these logs are missing or show errors, it will help identify the issue.

## Testing Checklist

- [ ] Widget token is correct
- [ ] Widget status is "Active"
- [ ] Business hours allow current time (if enabled)
- [ ] Account has sufficient credits
- [ ] Website domain is in allowed domains (if configured)
- [ ] Browser console shows no errors
- [ ] Widget appears on test page
- [ ] Network requests to halavoice.store succeed

## Getting Help

If you're still experiencing issues:

1. **Check Widget Status Dashboard**
   - Go to https://www.halavoice.store/app/tools/widgets
   - Click "Analytics" tab
   - Check widget status and error logs

2. **Regenerate Embed Token**
   - Go to widget settings
   - Click "Regenerate Token"
   - Copy new embed code

3. **Contact Support**
   - Include your widget ID
   - Share browser console errors
   - Screenshot of network requests

## Widget Configuration Best Practices

1. **Use HTTPS** for your website (required for microphone access)
2. **Add allowed domains** to prevent unauthorized embedding
3. **Set appropriate business hours** if not 24/7
4. **Monitor credit usage** in the Analytics dashboard
5. **Test regularly** using the widget test page

## Embed Code Placement

**Recommended**: Place the widget code before the closing `</body>` tag:

```html
<!DOCTYPE html>
<html>
<head>
    <title>Your Page</title>
</head>
<body>
    <!-- Your content here -->
    
    <!-- Widget code at the end -->
    <script>
      (function(w,d,s,o,f,js){
        w[o]=w[o]||function(){(w[o].q=w[o].q||[]).push(arguments)};
        js=d.createElement(s);js.id=o;js.src=f;js.async=1;
        (d.head||d.body).appendChild(js);
      }(window,document,'script','vw','https://www.halavoice.store/widget/embed.js'));
      vw('init', 'YOUR_WIDGET_TOKEN');
    </script>
</body>
</html>
```

**Alternative**: Place in `<head>` if you prefer earlier loading:

```html
<head>
    <title>Your Page</title>
    <script>
      (function(w,d,s,o,f,js){
        w[o]=w[o]||function(){(w[o].q=w[o].q||[]).push(arguments)};
        js=d.createElement(s);js.id=o;js.src=f;js.async=1;
        (d.head||d.body).appendChild(js);
      }(window,document,'script','vw','https://www.halavoice.store/widget/embed.js'));
      vw('init', 'YOUR_WIDGET_TOKEN');
    </script>
</head>
```

Both methods work correctly. The `async` attribute ensures the page loads without blocking.
