# Fix GTM and Intercom console errors

## What was done in code (optional robustness)

- **GTM** (`client/main.html`): The GTM snippet is wrapped in try/catch so a single script failure (e.g. ERR_ADDRESS_INVALID) does not break the rest of the page.
- **Router** (`imports/routes/router.js`): GTM (gtag), Facebook (fbq), and Intercom are called only if the script loaded (`typeof gtag === 'function'`, etc.). A try/catch wraps these so 403 or blocked scripts don't break the app. **Intercom widget** is loaded only after the app has mounted (after `FlowRouter.initialize()`), so if Intercom is blocked or returns 403, the rest of the app still runs and the chat simply won't show.
- **Users init** (`imports/api/Users/client/init/users.js`): Intercom `update` and `shutdown` are guarded with `typeof window.Intercom === 'function'`.

After deploy, the app will no longer throw when GTM or Intercom fail; the rest of the app keeps working.

---

## 1. net::ERR_ADDRESS_INVALID (GTM / scripts)

**What to do:**

1. **Always use the real public URL**  
   Open the site as **https://www.everyspace.co.za/** (with HTTPS and correct host). Avoid opening it by server IP or `http://0.0.0.0:3000` when testing production behaviour.

2. **Keep ROOT_URL correct in Docker**  
   Use `ROOT_URL=https://www.everyspace.co.za/` (with trailing slash) so any server-generated links or redirects are correct:
   ```bash
   --env ROOT_URL=https://www.everyspace.co.za/
   ```

3. **If it only happens in one environment**  
   Try another network (e.g. mobile hotspot), another browser, or incognito with extensions off. If it goes away, the cause is likely local (DNS, proxy, extension, VPN).

No further code change is required for GTM URLs; fixing how the site is opened and how ROOT_URL is set usually addresses ERR_ADDRESS_INVALID.

---

## 2. Intercom 403 (api-iam.intercom.io)

403 means Intercom's backend rejects the request. Common reasons: domain not allowed, or wrong/restricted app. App ID in code is `vs1gaavu`.

**What to do (in Intercom, not in code):**

1. Log in to **Intercom** (intercom.com).
2. Open **Settings → Installation → Web** (or "Where you've installed Intercom" / domain settings).
3. Under **allowed/supported domains**, ensure:
   - `https://www.everyspace.co.za`
   - and, if you use it, `https://everyspace.co.za`
   are both listed and saved.
4. Confirm the app ID in the code (`vs1gaavu`) is the same as the app in that workspace.

After the domain is allowlisted, the 403s for launcher_settings and ping should stop (you may need a hard refresh or clear cache).

---

## 3. Rebuild and redeploy

Rebuild the image and run the container with the updated code and `ROOT_URL`:

```bash
docker stop everyspace_d 2>/dev/null || true
docker rm everyspace_d 2>/dev/null || true
docker build -t everyspace:latest .
docker run --restart unless-stopped --network host --name everyspace_d -d \
  --env ROOT_URL=https://www.everyspace.co.za/ \
  ... (rest of your env vars)
  everyspace:latest
```

Summary: **ERR_ADDRESS_INVALID** — use the real URL and set ROOT_URL correctly. **Intercom 403** — add the domains in Intercom's allowed list and confirm app ID.
