# Website embed

> Add a Rewards button to any website, including Shopify and WordPress, with one script tag.

Source: https://docs.onari.xyz/docs/website-embed

No app? The Rewards Hub also works on websites. One script tag adds a **Rewards** button in your brand colour; signed-in customers open their own Rewards Hub in a panel (a full-screen sheet on phones). Visitors who aren't signed in see nothing.

It works on any site: plain HTML, React or Next.js, Shopify themes and WordPress. It doesn't set cookies on your site or change your styles, and it works in browsers that block third-party cookies.

## 1. Create an identity secret

In the dashboard, go to **Developers → Website & stores** and click **Create identity secret**. There's one for sandbox and one for live.

The secret proves which customer is signed in, so nobody can open someone else's rewards by changing an id in the page. Keep it on your server or in your theme code, never in browser JavaScript.

## 2. Sign the customer's id

The signature is the hex HMAC-SHA256 of the player id, keyed with your identity secret. Use the same player id you send in events.

<Tabs items={["Node.js", "PHP", "Python", "onari SDK"]}>
<Tab value="Node.js">

```ts
import { createHmac } from "node:crypto";

const signature = createHmac("sha256", process.env.ONARI_IDENTITY_SECRET).update(String(customer.id)).digest("hex");
```

</Tab>
<Tab value="PHP">

```php
$signature = hash_hmac('sha256', (string) $customer->id, getenv('ONARI_IDENTITY_SECRET'));
```

</Tab>
<Tab value="Python">

```python
import hmac, hashlib, os

signature = hmac.new(os.environ["ONARI_IDENTITY_SECRET"].encode(), str(customer.id).encode(), hashlib.sha256).hexdigest()
```

</Tab>
<Tab value="onari SDK">

```ts
import { signPlayer } from "onari";

const signature = await signPlayer(process.env.ONARI_IDENTITY_SECRET, customer.id);
```

</Tab>
</Tabs>

## 3. Add the script for signed-in customers

Render this only when a customer is signed in, just before `</body>`:

```html
<script src="https://hub.onari.xyz/embed.js" async
  data-workspace="your-workspace"
  data-player="CUSTOMER_ID"
  data-signature="SIGNATURE"></script>
```

| Attribute | |
|---|---|
| `data-workspace` | Your workspace's Hub name, shown in the snippet in the dashboard. |
| `data-player` | Your id for the signed-in customer. |
| `data-signature` | The signature from step 2. |
| `data-environment` | `sandbox` while testing. Leave it out for live. |
| `data-label` | Button text. Default `Rewards`. |
| `data-position` | `right` (default) or `left`. |
| `data-launcher` | `false` to hide the button, for example when you only use the inline Hub. |

### Shopify

Shopify can sign the customer in your theme, with no server code. In Shopify admin open **Online Store → Themes → ⋯ → Edit code → layout/theme.liquid** and paste this just before `</body>`:

```liquid
{% if customer %}
  <script src="https://hub.onari.xyz/embed.js" async
    data-workspace="your-workspace"
    data-player="{{ customer.id }}"
    data-signature="{{ customer.id | hmac_sha256: 'YOUR_IDENTITY_SECRET' }}"></script>
{% endif %}
```

Theme code is only visible to your staff, so the secret is safe there. To count orders without code, add the [Shopify connection](/docs/shopify): it uses the same customer id.

### WordPress

Add this to your theme's `functions.php` (or a code snippets plugin):

```php
add_action('wp_footer', function () {
  if (!is_user_logged_in()) return;
  $id = (string) get_current_user_id();
  $sig = hash_hmac('sha256', $id, 'YOUR_IDENTITY_SECRET');
  printf('<script src="https://hub.onari.xyz/embed.js" async data-workspace="your-workspace" data-player="%s" data-signature="%s"></script>', esc_attr($id), esc_attr($sig));
});
```

Send the same WordPress user id as `player_id` in your events, so progress and rewards match.

## Show the Hub inside a page

Put this where the Hub should appear, with the script tag above on the same page:

```html
<div data-onari-hub data-height="640"></div>
```

Add `data-path="/me"` to open a section (`/me`, `/trivia` or `/campaigns/<id>`), and `data-launcher="false"` on the script tag if you don't also want the floating button.

## Single-page apps

If customers sign in without a page load, call these from your own code:

```js
// After sign-in, with the signature from your server:
OnariHub.identify(customer.id, signature);

// On sign-out:
OnariHub.reset();

// Open or close the panel from your own button:
OnariHub.open();
OnariHub.close();
```

You can also set `window.onariSettings = { workspace, player, signature, environment }` before the script loads instead of `data-` attributes.

## How it works

- The script asks Onari whether the signature is right before showing the button. If it isn't, there's no button and the browser console says why.
- The panel opens the Hub with a session that works for an hour; the script opens a fresh one after 50 minutes. The session travels in the Hub's own address instead of a cookie, so Safari and Firefox (which block third-party cookies) work too.
- Only embed pages can be shown inside other sites; the rest of the Hub refuses to be framed.
- Escape closes the panel, and focus returns to where it was.
- A signature never expires, so treat it like the customer's session: only render it for the signed-in customer. Rolling the identity secret (**⋯ → Roll identity secret**) invalidates every signature at once.
