# Adding Colophon to a website

This page is written for a coding agent that has been asked to add Colophon to a publisher's
website. It is also a fair summary for a human developer. The longer guide is at
https://colophondigital.com/developers.

Colophon provides two things that sit inside an article page:

- **Afterword**: the comment section.
- **CrowdSurge**: polls, quizzes, predictions and one-tap reactions.

Both are added with one script and one HTML element each. There is no package to install and
no build step.

## What you need from the publisher

- Their **site key**. It starts with `pk_`, comes from the Install page of their Colophon
  console, and is public: it is safe to commit and to ship in HTML. If you were not given one,
  stop and ask for it. Do not invent one.
- The site's domain must be on the key's list of allowed origins (console, Settings). If the
  units answer `origin_not_allowed`, tell the publisher which origin to add.

## 1. Load the script once

Put this in the template that every article page shares, in the head or before the closing
body tag. It loads asynchronously and renders nothing on pages that have no Colophon elements.

```html
<script async src="https://api.colophondigital.com/embed/colophon.js" data-site="YOUR_SITE_KEY"></script>
```

Load it from this address. Do not download it, bundle it or serve a copy: it is updated in
place.

## 2. Add the comment section (Afterword)

In the article template, straight after the article body:

```html
<div data-colophon="afterword" data-article-id="ARTICLE_ID"></div>
```

- `data-article-id` is required. Use the CMS's permanent id for the article (a database id
  or UUID). Never use the URL, slug or headline: comments would be lost when an editor
  changes them.
- Optional: `data-article-title`, `data-article-url`, `data-article-section`,
  `data-article-tags` (comma-separated), `data-article-image`. When they are missing,
  Colophon reads the page's canonical link and Open Graph tags, so most sites need none.
- A live comment count for a headline or a link anywhere on the site:
  `<span data-colophon="comment-count" data-article-id="ARTICLE_ID"></span>`

## 3. Add a poll (CrowdSurge)

Where the poll should appear, usually between the article body and the comments:

```html
<div data-colophon="crowdsurge" data-placement="article"></div>
```

- With `data-placement`, the publisher chooses in the console which poll fills that slot,
  and can change it without another code change. Use a short name such as `article` or
  `sidebar` and tell the publisher which names you used.
- To show one specific poll, use `data-poll="POLL_ID"` instead. The id comes from the console.
- An element with no live poll renders nothing and takes no space.

## Other units

```html
<div data-colophon="frontlist" data-limit="5"></div>          <!-- most discussed articles -->
<div data-colophon="pull-quote" data-comment="COMMENT_ID"></div> <!-- one comment as a quotation -->
```

## Rules

- **Do not style the elements** and do not wrap them in an iframe. Each unit renders in a
  shadow root and takes its typeface, text colour, accent colour and corner radius from the
  page around it, in light or dark. To pin a value, set a custom property on any ancestor:
  `--colophon-accent`, `--colophon-on-accent`, `--colophon-link`, `--colophon-radius`,
  `--colophon-font`, `--colophon-heading-font`.
- **Give the element the full width of the article column.** Do not put it inside a
  fixed-height or `overflow: hidden` container.
- **Single-page apps:** elements added after load are found automatically. After a client-side
  route change that reuses the same element, call `window.colophon.refresh()`.
- **Content-Security-Policy:** allow `https://api.colophondigital.com` in `script-src`, `connect-src` and
  `img-src`. No inline script or `unsafe-eval` is needed.
- **Consent:** the units carry labelled advertising. If the site has a consent tool, call
  `window.colophon.setConsent({ ads: false })` when a reader declines advertising, and
  `{ ads: true }` when they accept. The script also honours Global Privacy Control.
- **The publisher's own log-in (optional):** to sign readers in with the site's existing
  accounts, the server signs a short-lived HS256 JWT with the SSO secret from the console
  (claims `sub`, `name`, optional `email`, `exp` within ten minutes) and the page calls
  `window.colophon.login(token)`. Never put the SSO secret in client code. Skip this unless
  you were asked for it.

## Check your work

1. Open one article page in a browser. The comment section should appear where you put the
   element, in the site's own typeface.
2. The page fires `colophon:ready` on `document` once the script has started:
   `document.addEventListener('colophon:ready', () => console.log('Colophon is running'))`.
3. Nothing on the page should shift or scroll sideways at a phone width.
4. If nothing appears, read the browser console and the network tab for a request to
   `https://api.colophondigital.com`. `unknown_site` means the key is wrong; `origin_not_allowed` means the
   domain is not on the key's allowed list; a blocked request usually means the
   Content-Security-Policy.

## When you have finished

Show the publisher the diff, name every placement you used, and give one article URL to
check. Do not deploy, and do not change the site's consent or privacy settings, unless you
were asked to.
