Skip to content

Links

A link to a place in your app opens your native app on a phone that has it, and a web page anywhere else: from an email, a QR code on a table, a text message. Each app has its own links, and its test app its own again. Links in Flutter describes them from the phone’s side; this page is the web’s.

A link is your app’s path under its link base: https://app.purrcrm.com/l/<your app's link name>/<path>, or a host of your own whose DNS points at PurrCRM. /offers/42 in your app is then <link base>/offers/42. crm.publicApp.value?.linkBase says your app’s.

What you draw: nothing for a link itself. Your router shows the page a link names; your share button shows the link a user makes.

Your team sets the links up under Links › Setup, on the Link domain card. For the web, the field that matters is Web page for a phone without the app (optional): {path} in it is the link’s path, such as https://www.northwind.example{path}.

A link opened in a browser on a computer, or on a phone without your native app, reaches PurrCRM first. PurrCRM counts its click (an email’s, a saved link’s) and redirects the browser to the place’s web page: the declared place’s own web page if it has one (Declare your places), or else that web page with the path filled in. Point them at your web app, and your router opens the path as it would any address. The redirect leaves PurrCRM‘s own parameters (crm_c, crm_l) off the address, so your page gets the path and your own query, and a saved link’s data does not reach it that way. A link to an in-app message (/messages/show/<id>) has no web page, so a browser never lands on yours with one.

2. Hand the SDK the address the page loaded at

Section titled “2. Hand the SDK the address the page loaded at”

Once, as the page starts and before your own routing, hand the SDK location.href:

src/main.ts
await crm.start();
if (!(await crm.handleLink(location.href))) {
// Yours: your router shows the page, as it already does.
}

Hand it only the address the page loaded at (and a link of the app’s you come by in the page, such as one scanned or pasted), not your router’s later navigations: on your own site those are your app’s, and one of PurrCRM‘s pages among them would be opened twice.

handleLink(href) answers whether the SDK takes the user where it goes. What it does with what can arrive:

The addressWhat handleLink does
One of PurrCRM‘s pages on your own site (/messages…, /help…; the paths of CrmLinks), when your site serves those paths and a web page names themAnswers true, and opens it through your navigate hook (Initialise). Not a link_opened event: a reload or a bookmark opens the same address.
A page of your own carrying crm_c (an email’s click) or crm_l (a saved link), put there by a link written straight to your site rather than through PurrCRMAnswers false, and your router shows the page. The click was not counted (the redirect would have taken it off), so the SDK posts it; it asks PurrCRM for the saved link’s data, tells linkOpened, and records link_opened.
A link under your app’s link base, or, before the app’s public face has answered, under /l/ on PurrCRM‘s own host, that you hand it yourself (scanned or pasted in the page)Answers true, and takes the user where it goes, as on a phone: one of PurrCRM‘s pages through navigate, an in-app message its link opens (/messages/show/<id>) shown over the page (In-app messages), or a place in your app through followable and openInApp. Its email’s click is posted, and it is a link_opened event.
Anything else, such as your own pages without those parameters, or an address on another siteAnswers false, and does nothing.

Whatever it takes waits until a user is signed in and ready (setReady), as for a tapped push, and linkOpened hears of it first. link_opened carries from (email or link), for segments and goals. The answer is true even for a path that goes nowhere: the link was PurrCRM‘s.

Once handled, take crm_c and crm_l off the address, so that a reload does not hand them in again:

const url = new URL(location.href);
if (url.searchParams.has("crm_c") || url.searchParams.has("crm_l")) {
url.searchParams.delete("crm_c");
url.searchParams.delete("crm_l");
history.replaceState(history.state, "", url.href);
}

Links your team saves in Links (a campaign, a QR code) are counted by PurrCRM as a browser opens them, and lead to their place’s web page like any link. When handleLink is handed a saved link itself, it takes the short form (…/go/summer24) and the full form (…/offers/42?crm_l=summer24), asks PurrCRM where the short form goes and what data it carries, and records which saved link it was on link_opened (saved). A short form PurrCRM does not know (archived, expired or never saved) opens nothing; a full form still opens its own path, without data. On a page of your own, crm_l is asked about the same way.

A saved link may carry data for the app (a coupon, a referrer). linkOpened hears of every link the SDK takes before it is followed, and of a page of your own that carried crm_l or crm_c:

src/crm.ts
crm.host = {
...crm.host,
linkOpened: (link) => {
const coupon = link.data["coupon"];
if (coupon) wallet.offer(coupon);
},
};

A CrmLinkContext has path, from, saved, data, campaign and channel; the last four are empty for a link nobody saved.

A link in a reply, an article, a notice or a message’s button is a path in your app or an https address. crm.openLink(link) follows it by PurrCRM‘s rules: one of PurrCRM‘s own pages through navigate, a path through your followable and openInApp, an https address through openExternal (a new tab unless you say). It answers whether it went anywhere. Ask crm.linkTargetOf(link) first, without going, to draw a link that goes nowhere as plain words:

const target = crm.linkTargetOf(span.link); // { kind: "inApp", location } | { kind: "web", url } | null
if (target === null) drawPlainText(span.text);
else drawLink(span.text, () => crm.openLink(span.link));

Declare the places a link may go, so your team picks them in PurrCRM‘s link picker instead of typing paths:

src/crm.ts
const host: CrmHost = {
followable: (link) => (link.startsWith("/offers") || link === "/wallet" ? link : null),
openInApp: (location) => router.push(location),
links: [
{ name: "Offer page", path: "/offers/{offer_id}", screen: "offer", webUrl: "https://www.northwind.example/offers/{offer_id}" },
{ name: "All offers", path: "/offers?category={category}" },
{ name: "Wallet", path: "/wallet", screen: "wallet" },
],
};

A part in braces is filled in by whoever writes the link: a value, or an attribute such as {{first_name}}. webUrl is the place’s page on the web, which a browser goes to (Settings). screen is the screen name you give crm.screen there. Declaring a place opens nothing: followable still decides.

Each check-in sends a digest of the list, and PurrCRM asks for the list itself when it has not seen that digest: once a build. Your team finds it under Links › Destinations. The digest is SHA-256 by WebCrypto, which a browser gives only a secure context: on a plain-http page the places are not declared, and the setup check says so.

Let your users share a place in your app with a link of their own: a recipe, an offer, a profile. Your team turns on Let the app make links for its users to share on Links › Setup, then:

const link = await crm.createLink("/recipes/42", {
data: { shared_by: user.id },
channel: "whatsapp",
});
if (link !== null) {
await navigator.share?.({ url: link.shortUrl }); // or copy it to the clipboard
}

shortUrl is the one to share (<link base>/go/<slug>); url is its full form. Whoever opens it gets the path, data and all; its clicks and opens are counted under Links, under Made by: Users, sharing. createLink answers null when nobody is signed in, the app does not let its users make links, the user has made 50 today, or there is no connection.

With a referral programme on Links › Referrals, every user has a link of their own to invite others:

const link = await crm.referralLink();
if (link !== null) showInvite(`Join me: ${link.shortUrl}`);

It is the same link every time. A user new to the app who arrives by it is credited to who shared it (Referrals). referralLink answers null when nobody is signed in, the app has no programme, or there is no connection.

A website of yours can also show PurrCRM‘s banner that opens your native app on a phone, set up under Links › Website (On your website). It is a script of its own, not part of the SDK, and the two live side by side. A page with a Content Security Policy needs PurrCRM‘s address in script-src for it.

Call crm.screen("wallet") as each screen shows (the same name every time; never a path or an id). Your team can then show an in-app message only on some screens (Events).