> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beyondwords.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Advert Link

> Links to the current advert's click-through URL.

export const AdvertLinkDemo = () => {
  const MOCK_CONTENT = [{
    id: "demo-1",
    title: "How AI voices are changing publishing",
    imageUrl: "/images/player-ui/demo-cover.svg",
    sourceUrl: "https://beyondwords.io",
    sourceId: "demo",
    adsEnabled: true,
    duration: 124,
    audio: [{
      id: "audio-1",
      url: "/images/player-ui/demo-silent.mp3",
      contentType: "audio/mpeg",
      duration: 124
    }],
    video: [],
    summarization: {
      audio: [],
      video: []
    },
    segments: []
  }];
  const MOCK_ADVERTS = [{
    id: "advert-1",
    type: "native",
    placement: "pre-roll",
    vastUrl: null,
    clickThroughUrl: "https://sponsor.example.com",
    imageUrl: "/images/player-ui/demo-cover.svg",
    textColor: "#ffffff",
    backgroundColor: "#943bfc",
    iconColor: "#ffffff",
    audio: [{
      id: "audio-2",
      url: "/images/player-ui/demo-silent.mp3",
      contentType: "audio/mpeg",
      duration: 8
    }],
    video: []
  }];
  const getHostName = url => {
    try {
      return new URL(url).hostname.replace(/^www\./, "");
    } catch {
      return null;
    }
  };
  const newEvent = props => ({
    id: crypto.randomUUID(),
    createdAt: new Date().toISOString(),
    status: "pending",
    initiatedBy: "user",
    ...props
  });
  const [target, setTarget] = useState(null);
  const [player, setPlayer] = useState(null);
  const [adverts, setAdverts] = useState([]);
  const [advertIndex, setAdvertIndex] = useState(0);
  useEffect(() => {
    if (!target) return;
    let instance;
    const init = () => {
      instance = new window.BeyondWords.Player({
        target,
        content: MOCK_CONTENT,
        adverts: MOCK_ADVERTS,
        introsOutros: [],
        advertIndex: 0,
        widgetStyle: "none",
        showUserInterface: false,
        analyticsConsent: "none"
      });
      const sync = () => {
        setAdverts(instance.adverts);
        setAdvertIndex(instance.advertIndex);
      };
      instance.addEventListener("<any>", sync);
      sync();
      setPlayer(instance);
    };
    if (window.BeyondWords) init(); else window.addEventListener("BeyondWordsReady", init);
    return () => {
      window.removeEventListener("BeyondWordsReady", init);
      if (instance) instance.destroy();
    };
  }, [target]);
  const advert = adverts[advertIndex];
  return <div className="not-prose flex w-full items-center justify-center rounded-lg border p-10" style={{
    minHeight: "16rem",
    fontFamily: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif'
  }}>
      <div ref={setTarget} style={{
    display: "none"
  }} />
      {player && advert && <a target="_blank" rel="noreferrer" href={advert.clickThroughUrl} onClick={() => {
    player.onEvent(newEvent({
      type: "PressedAdvertLink",
      description: "The advert link was pressed to open the click-through URL in a new tab.",
      initiatedBy: "user"
    }));
  }}>
          {getHostName(advert.clickThroughUrl)}
        </a>}
    </div>;
};

<Tabs>
  <Tab title="Preview">
    <AdvertLinkDemo />
  </Tab>

  <Tab title="Code">
    ```jsx AdvertLink.jsx theme={null}
    "use client";

    import { newEvent } from "@/lib/newEvent";
    import { usePlayer, usePlayerSelector } from "@/registry/react/PlayerProvider";

    const getHostName = (url) => {
      try {
        return new URL(url).hostname.replace(/^www\./, "");
      } catch {
        return null;
      }
    };

    export function AdvertLink() {
      const player = usePlayer();
      const adverts = usePlayerSelector((p) => p.adverts);
      const advertIndex = usePlayerSelector((p) => p.advertIndex);
      const advert = adverts[advertIndex];
      if (!advert) return null;
      return (
        <a
          target="_blank"
          rel="noreferrer"
          href={advert.clickThroughUrl}
          onClick={() => {
            player.onEvent(
              newEvent({
                type: "PressedAdvertLink",
                description: "The advert link was pressed to open the click-through URL in a new tab.",
                initiatedBy: "user",
              }),
            );
          }}
        >
          {getHostName(advert.clickThroughUrl)}
        </a>
      );
    }
    ```
  </Tab>
</Tabs>

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npx @beyondwords/cli components add advert-link
  ```

  ```bash pnpm theme={null}
  pnpm dlx @beyondwords/cli components add advert-link
  ```

  ```bash yarn theme={null}
  yarn dlx @beyondwords/cli components add advert-link
  ```

  ```bash bun theme={null}
  bunx --bun @beyondwords/cli components add advert-link
  ```
</CodeGroup>

## Usage

```tsx theme={null}
import { PlayerProvider } from '@/registry/react/PlayerProvider';
import { AdvertLink } from '@/registry/react/AdvertLink';

export function Player() {
  return (
    <PlayerProvider args={{ projectId: 9504, contentId: "ba00ef51-03ac-4a85-8b98-e45c957e8ef8", showUserInterface: false }}>
      <AdvertLink />
    </PlayerProvider>
  );
}
```

Reads the current advert from the surrounding
[`PlayerProvider`](/player-ui/react/player-provider) and renders a link to its
`clickThroughUrl` (showing the hostname as the label). On click it emits a
`PressedAdvertLink` event (built with [`newEvent`](/player-ui/react/new-event)). It
renders nothing when there's no active advert, so it's safe to include
unconditionally.

## Reference

|                       |                                                                 |
| --------------------- | --------------------------------------------------------------- |
| **Props**             | None                                                            |
| **Player state read** | `adverts`, `advertIndex`                                        |
| **Events emitted**    | `PressedAdvertLink`                                             |
| **Requires**          | a [`PlayerProvider`](/player-ui/react/player-provider) ancestor |
