Skip to content

Add the messenger to your website

The website messenger is PurrCRM‘s messenger, ready-made for a website. You add one script tag to your pages and it draws:

  • The launcher, the round button in a corner of the page, with the number of unread messages.
  • The messenger: its home, conversations with your team, and the user’s tickets. Replies arrive while the conversation is open, and the reader sees when a teammate is typing and when their message has been Seen. They can add pictures (and files, if you turn them on), pick an emoji, and open a picture full size.
  • Tickets: a Tickets tab with the user’s tickets, where they can also open a new one from your ticket forms.
  • The help center: search, collections and articles.
  • Your in-app messages: banners, posts, carousels and surveys, including the ones meant for some pages only.
  • For someone signed in: their Inbox of the notices you sent them, and Offers and news, where they choose which topics they hear about.

It looks the way your team styles the messenger on Settings › your app › Messenger, and it works on phones as well as on computers.

  1. Open Settings › your app › Developers › Install.

  2. In Website messenger, copy The messenger’s tag. It looks like this, with your own address and app key:

    <script src="https://crm.example.com/public/v1/messenger/web/app_XXXXXXXX.js" async></script>
  3. Paste it into every page of your website, just before the closing </body> tag. If your website has one layout or template that every page uses, paste it there once.

The Website messenger card, with the messenger's tag to copyThe Website messenger card, with the messenger's tag to copy
The Website messenger card, with the messenger's tag to copy

Each of your apps has its own tag. Use the live app’s tag on your real website, and the test app’s on a test copy of it.

PurrCRM only answers the pages of websites you name.

  1. On the same page, Settings › your app › Developers › Install, find Web origins.
  2. Add your website’s address: the https://, the domain, and the port if it has one, with no path. For example https://www.example.com.
  3. Add each address the website is opened on. https://example.com and https://www.example.com are two addresses.
  4. Save.

Open a page of your website. The launcher appears in its corner.

The messenger open on a website, from the launcher in its cornerThe messenger open on a website, from the launcher in its corner
The messenger open on a website, from the launcher in its corner

Your website’s readers are either signed in to your website, or not.

By default, someone who hasn’t signed in is asked to sign in before they can write. To let them write straight away:

  1. Open Settings › your app › Developers › Sign-in.
  2. In Website visitors, tick Let website visitors write before they sign in.
The Sign-in tab of DevelopersThe Sign-in tab of Developers
The Sign-in tab of Developers

They show in your inbox as Visitor with a short code. If they sign in later on the same browser, their conversations move to their own contact, with everything they wrote.

If you leave it off, the messenger shows Sign in instead of the composer. To make that button open your own sign-in page, set signIn (see below).

Tell the messenger who is signed in, so they see their own conversations on every device. Your website hands it the user’s id and a short token that your backend signs with the app’s identity secret. Sign your users in explains how your backend makes the token.

Put this before the messenger’s tag, on pages where someone is signed in:

<script>
window.crmMessengerSettings = {
user: {
id: "THE_USER_ID",
name: "Their name",
// Your backend signs this with the app's identity secret.
token: () => fetch("/api/crm-token").then((r) => (r.ok ? r.text() : null)),
},
};
</script>
  • id is the user’s id on your website. It must be the same id the token is signed for.
  • token can be the token itself, or a function that fetches a fresh one. A function is better: the messenger calls it again when the token runs out.

If someone signs in or out without the page reloading, tell the messenger:

CrmMessenger.signIn({ id: "THE_USER_ID", name: "Their name", token: () => fetch("/api/crm-token").then((r) => r.text()) });
CrmMessenger.signOut();

CrmMessenger is there once the messenger has loaded. To wait for it:

window.addEventListener("crm-messenger:ready", () => {
// CrmMessenger is ready.
});

Everything about the messenger’s look is on Settings › your app › Messenger: the colours, dark mode, the header, the home’s greeting and cards, your words, and the launcher. Style the messenger and messages walks through each part. Publish, and your website shows the new look within a few minutes.

To see your draft on a website before you publish, choose Website under Preview in, beside the cards. It is the real website messenger, at desktop or phone width. Nothing you type in it is sent.

To let readers send files as well as pictures, tick Contacts may send files in Options (Let contacts send files). The messenger’s attach button then becomes Add a file, and takes pictures and files. Readers can also drop them on the conversation, or paste them in the message box.

For a website, the launcher has two extra settings, in Launcher:

  • Hide it on phones (a narrow screen).
  • Hide on pages: type a page’s path, such as /checkout, and press Add. /account/* is every page under /account.

On a phone, the messenger fills the screen, and the launcher hides while it is open.

A banner, post, carousel or survey can wait for a page of your website. In the message’s When step, fill Show on pages:

  • /pricing is that page alone.
  • /blog/* is every page under /blog.
  • Empty shows it on any page.

The message waits until the reader opens one of those pages, including pages your website opens without reloading. The pages your website has already reported are suggested as you type.

You don’t have to use the launcher. To open the messenger from a link or button of your own:

<button type="button" onclick="CrmMessenger.open()">Chat with us</button>

CrmMessenger.open("help") opens the help center, and CrmMessenger.open("messages") or CrmMessenger.open("tickets") their lists. CrmMessenger.newConversation() starts a new conversation.

To hide the launcher on every page and use only your own button, put launcher: false in window.crmMessengerSettings. To show it again later, call CrmMessenger.showLauncher(true).

window.crmMessengerSettings, before the tag:

SettingWhat it does
userWho is signed in: id, name and token. Leave it out for nobody.
opensWhat the launcher opens: "home", "messages", "help" or "tickets". Your launcher’s setting on the Messenger page decides it otherwise.
launcherfalse hides the launcher.
signInA function to call when the reader presses Sign in, for example () => location.assign("/login").
customEventsThe names of your own events that CrmMessenger.track may send.
appVersionYour website’s version, shown in reports.
emojifalse leaves out the emoji button.
languageA language tag, such as "de", for dates, times and numbers, and for the direction of the page (right to left for Arabic or Hebrew, for example). See Your own words and languages.
wordsYour own words for the messenger, keyed as the English ones. See Your own words and languages.
soundtrue plays a short sound for a reply. See Sound and desktop notifications.
desktopNotificationstrue shows a notification for a reply while the tab is hidden. See Sound and desktop notifications.

CrmMessenger, after it loads:

CallWhat it does
signIn(user), signOut()Someone signed in or out without the page reloading.
open(space?), newConversation(), close(), toggle()Open or close the messenger.
showLauncher(shown)Show or hide the launcher.
onUnread(listener)Calls listener with the number of unread messages, now and when it changes. Answers a function that stops it.
track(name, props?)Sends one of your own events (listed in customEvents).
page()Tells the messenger the page changed. It notices on its own when your website changes page; call this only if it doesn’t.
endVisitor()Forgets the visitor on this browser, for example on a shared computer.
setLanguage(tag, words?)Changes the language, and your words, without reloading.
askToNotify()Asks the reader to allow desktop notifications. Call it from their click. Answers whether they allowed them.

The messenger’s own words (its buttons, its headings, its messages when something goes wrong) are in English. To say them in your own words, or in another language, give words: a table with only the words you want to change. Anything you leave out stays in English.

<script>
window.crmMessengerSettings = {
language: "de",
words: {
messages: "Nachrichten",
openMessengerUnread: "Nachrichten öffnen, {n} ungelesen",
articles: (n) => (n === 1 ? "1 Artikel" : `${n} Artikel`),
sentences: { offline: "Keine Verbindung." },
},
};
</script>
  • Each word has a name, such as messages. The English table, src/messenger/words.ts in the web SDK’s repository, lists every name.
  • Where the English word takes something, such as a count n or a teammate’s name, write it in braces: {n}, {name}.
  • A word that changes with a number, such as “1 article” or “3 articles”, is a function that takes the number.
  • Messages about problems go under sentences. The ticket form’s words go under ticketForm, and the inbox’s and preferences’ words under account.
  • A word the messenger can’t use (a placeholder it doesn’t have, a function that fails) is said in English instead. Nothing breaks.

language sets how dates, times and numbers are written, and which way the messenger reads: a right-to-left language draws it right to left. Without it, the messenger uses the browser’s language, and the page’s direction.

To change both later, for example when the reader picks another language on your website, call CrmMessenger.setLanguage("fr", wordsInFrench).

Your words in Settings › your app › Messenger (the title, the greeting, the cards) still win, and what your team writes is sent in each person’s language as usual (Languages).

Both are off unless you turn them on:

<script>
window.crmMessengerSettings = { sound: true, desktopNotifications: true };
</script>
  • Sound: a short sound when a reply arrives while the messenger is closed or the tab is in the background. Browsers keep a page silent until the reader has clicked or typed on it, so the first reply after the page loads may be silent. It never plays for the reader’s own messages, or for a reader who has asked their computer to reduce motion.
  • Desktop notifications: a notification with the teammate’s name and the start of their reply, while the tab is in the background. Clicking it brings the tab forward and opens the conversation.

A browser only shows notifications once the reader allows them, and only asks when the reader clicks something. So ask from a button of your own:

<button type="button" onclick="CrmMessenger.askToNotify()">Tell me when you reply</button>

If the reader says no, or the browser has no notifications, nothing is shown. Notifications are not shown on Android phones.

If your website has a Content Security Policy

Section titled “If your website has a Content Security Policy”

If your website sends a Content-Security-Policy header, add PurrCRM‘s address (the start of the tag’s src, such as https://crm.example.com) to:

  • script-src, so the tag can load,
  • connect-src, so it can reach PurrCRM,
  • img-src, so pictures and avatars show. Add blob: too, so a picture the reader adds shows before it is sent.

Some parts of the messenger load only when they are first needed, such as the emoji picker, the ticket form and the sound. They come from the same address as the tag, so script-src already covers them. If the emoji picker can’t load, the composer works without it.

The messenger needs no unsafe-inline or unsafe-eval.

  • No launcher. Check that the tag is on the page and that your website’s address is in Web origins, exactly as the browser shows it. Also check Show the launcher in Launcher, and Hide on pages.
  • “Sign in to message us”. Nobody is signed in and website visitors are off. Turn them on (step 3) or sign the reader in.
  • A signed-in user is asked to sign in again. The token was refused. Test one on Settings › your app › Developers › Sign-in › Test a token, and check that id is the user the token is signed for.
  • The look didn’t change. Publish it on the Messenger page, then reload your website after a minute.
  • No Add a file button. Tick Contacts may send files in Options on the Messenger page, and publish.
  • No sound or notification for a reply. The reader must click or type on the page before a sound can play, and allow notifications before one shows. Neither comes while the messenger is open in a tab the reader is looking at.