@axceera/playstep-referrals 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +46 -2
- package/dist/cdn/referrals.js +1 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +1 -0
- package/package.json +37 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PlayStep
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
6
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
8
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
11
|
+
Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
14
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
16
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
17
|
+
|
|
18
|
+
Icons: Lucide (https://lucide.dev), ISC License. Copyright (c) for portions of Lucide are held by Cole Bemis 2013-2022
|
|
19
|
+
as part of Feather (MIT). All other copyright (c) for Lucide are held by Lucide Contributors 2022.
|
package/README.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @axceera/playstep-referrals
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Where your users came from, for [PlayStep Referrals](https://playstep.app/docs/sdk/referrals): partner links, promo
|
|
4
|
+
codes, UTM tags and referring sites. An add-on to [`@axceera/playstep`](https://www.npmjs.com/package/@axceera/playstep),
|
|
5
|
+
shipped on its own so apps that don't use Referrals download none of it. About 2 KB gzipped, no dependencies.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @axceera/playstep @axceera/playstep-referrals
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { playstep } from '@axceera/playstep'
|
|
13
|
+
import { referrals } from '@axceera/playstep-referrals'
|
|
14
|
+
|
|
15
|
+
playstep.init({ appKey: 'ps_pub_prod_your_key' })
|
|
16
|
+
const ref = referrals(playstep, { appKey: 'ps_pub_prod_your_key' })
|
|
17
|
+
|
|
18
|
+
playstep.identify(user.id, { email: user.email }) // the add-on sends where they came from, once
|
|
19
|
+
const mine = await ref.myLink() // { link, code } with a friend program, or null
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
It wraps the core's `identify` and `reset` in place, and hashes user ids exactly as the core does: the raw id (and
|
|
23
|
+
email) never leave the browser. On a marketing site without the core, call `referrals(null, { appKey })` or use the
|
|
24
|
+
script tag:
|
|
25
|
+
|
|
26
|
+
```html
|
|
27
|
+
<script async src="https://cdn.playstep.app/referrals/v1/referrals.js" data-app-key="ps_pub_prod_your_key"></script>
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Options: `appDomains` (your app's other domains, which links carry the ref to), `consent: 'required'` (store nothing
|
|
31
|
+
until `ref.consent(true)`), `api`. Full docs: [playstep.app/docs/sdk/referrals](https://playstep.app/docs/sdk/referrals).
|
|
32
|
+
|
|
33
|
+
## In this repo
|
|
34
|
+
|
|
35
|
+
- `src/capture.ts`: what a page view carries (`ps_ref`, `ref`, UTM tags, the referring host) and the `_ps_ref`
|
|
36
|
+
first-party cookie on the root domain (first touch kept, last moved on).
|
|
37
|
+
- `src/index.ts`: `referrals()` and `attach()`, the identify and reset wrappers, sending once per user and touches
|
|
38
|
+
(`POST /v1/referrals/attribution`; with no touches too, so the user counts as direct in Sources), `myLink()` (`GET /v1/referrals/me`). The hash is `@playstep/spec/sha256`, the
|
|
39
|
+
core's own code.
|
|
40
|
+
- `src/cdn.ts` and `scripts/cdn.ts`: the script-tag build and its CDN release (`referrals/<version>/` and the `v1`
|
|
41
|
+
channel, with a manifest of SRI hashes); `scripts/upload-cdn.ts` puts it in R2 (`pnpm release cdn`, RUNBOOK.md › 4).
|
|
42
|
+
- Budgets in `.size-limit.json` (`pnpm size` after `pnpm build`); they only go down.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pnpm build && pnpm size
|
|
46
|
+
pnpm test
|
|
47
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(){let e=[];for(let t=2;e.length<64;t++){let r=!0;for(let e=2;e*e<=t;e++)t%e===0&&(r=!1);r&&e.push(Math.cbrt(t)%1*2**32|0)}let t=(e,t)=>e>>>t|e<<32-t;function r(r){let n=(new TextEncoder).encode(r),o=n.length,l=new Uint32Array(16*(o+8>>6)+16);for(let e=0;e<o;e++)l[e>>2]|=n[e]<<24-e%4*8;l[o>>2]|=128<<24-o%4*8,l[l.length-1]=8*o,l[l.length-2]=Math.floor(o/536870912);let i=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],a=new Uint32Array(64);for(let r=0;r<l.length;r+=16){for(let e=0;e<64;e++)if(e<16)a[e]=l[r+e];else{let r=a[e-15],n=a[e-2];a[e]=a[e-16]+(t(r,7)^t(r,18)^r>>>3)+a[e-7]+(t(n,17)^t(n,19)^n>>>10)}let[n,o,s,c,p,f,u,d]=i;for(let r=0;r<64;r++){let l=d+(t(p,6)^t(p,11)^t(p,25))+(p&f^~p&u)+e[r]+a[r]|0,i=(t(n,2)^t(n,13)^t(n,22))+(n&o^n&s^o&s)|0;d=u,u=f,f=p,p=c+l|0,c=s,s=o,o=n,n=l+i|0}[n,o,s,c,p,f,u,d].forEach((e,t)=>i[t]=i[t]+e|0)}return i.map(e=>(e>>>0).toString(16).padStart(8,"0")).join("")}let n=(e,t)=>e?.trim().slice(0,t)||void 0;function o(e=location,t=document.referrer,r=Date.now()){let o=new URLSearchParams(e.search),l={at:r},i={},a=!1;for(let e of["source","medium","campaign","term","content"]){let t=n(o.get(`utm_${e}`),100);t&&(a=!!(i[e]=t))}a&&(l.utm=i),(l.click=n(o.get("ps_ref"),200))&&(a=!0),(l.ref=n(o.get("ref"),64))&&(a=!0);let s=/^https?:\/\/([^/:?#]+)/i.exec(t)?.[1]?.toLowerCase(),c=e.hostname.split(".").slice(-2).join(".");return s&&s!==c&&!s.endsWith(`.${c}`)&&(a=!!(l.referrer=s.slice(0,255))),a?l:void 0}function l(e,t){document.cookie=`_ps_ref=${e?encodeURIComponent(JSON.stringify(e)):""};path=/;samesite=lax;max-age=${e?7776e3:0}${t?`;domain=${t}`:""}${"https:"===location.protocol?";secure":""}`}let i="__playstepReferrals",a="playstep:ref:sent";function s(e,t){let r=e;if(r[i])return r[i];let{identify:n,reset:o}=r;return r.identify=(e,o)=>{let l=n.call(r,e,o);return t.identify(e,o),l},r.reset=()=>{let e=o.call(r);return t.reset(),e},r[i]=t}let c=document.currentScript?.dataset??{};if(c.appKey&&!window.playstepReferrals){let e=function(e,t){let n,i,c=(t.api??"https://cdn.playstep.app").replace(/\/$/,""),p=function(){let e=location.hostname.split(".");if(e.length<2||/^[\d.]+$/.test(location.hostname))return"";for(let t=e.length-2;t>=0;t--){let r=`;domain=${e.slice(t).join(".")};path=/`;if(document.cookie=`_ps_t=1${r}`,document.cookie.includes("_ps_t=1"))return document.cookie=`_ps_t=${r};max-age=0`,e.slice(t).join(".")}return""}(),f="required"!==t.consent,u=((e,t)=>t?{first:e.first??t,last:t}:e)(function(){try{return JSON.parse(decodeURIComponent(/(?:^|; )_ps_ref=([^;]+)/.exec(document.cookie)?.[1]??"{}"))}catch{return{}}}(),o());f&&l(u,p);let d=t.appDomains?.map(e=>e.toLowerCase());d?.length&&document.addEventListener("click",e=>{let t=e.target?.closest?.("a[href]"),r=u.last;if(!t||!r||!d.includes(t.hostname)||t.hostname===location.hostname)return;let n=new URL(t.href);r.click&&n.searchParams.set("ps_ref",r.click),r.ref&&n.searchParams.set("ref",r.ref),t.href=n.href},!0);let m=()=>{if(!n||!f)return;let e="";try{e=localStorage.getItem(a)??""}catch{}let o=r(n+JSON.stringify(u)).slice(0,16);e.includes(o)||fetch(`${c}/v1/referrals/attribution`,{method:"POST",keepalive:!0,headers:{"content-type":"text/plain"},body:JSON.stringify({key:t.appKey,user:n,...u,email:i})}).then(t=>t.ok&&localStorage.setItem(a,`${o},${e}`.slice(0,340))).catch(()=>{})},h={get touches(){return u},consent(e){f=e,e||(u={}),l(e?u:null,p),m()},identify(e,o){if(!e)return;n=((e,t)=>`u_${r(`${e}:${t}`).slice(0,40)}`)(t.appKey,`${e}`);let l=o?.email;i="string"==typeof l&&l.trim()?r(l.trim().toLowerCase()):void 0,m()},reset(){n=i=void 0},myLink:()=>n?fetch(`${c}/v1/referrals/me?key=${t.appKey}&user=${n}`).then(e=>e.ok?e.json():null).catch(()=>null):Promise.resolve(null)};return e?s(e,h):h}(window.playstep??null,{appKey:c.appKey,api:c.api,appDomains:c.appDomains?.split(/[\s,]+/).filter(Boolean),consent:"required"===c.consent?"required":"granted"});window.playstepReferrals=e,window.playstep||addEventListener("load",()=>window.playstep&&s(window.playstep,e))}}();
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//#region src/capture.d.ts
|
|
2
|
+
/** One visit's clues (the API's attribution touch, spec touchSchema). */
|
|
3
|
+
interface Touch {
|
|
4
|
+
click?: string;
|
|
5
|
+
ref?: string;
|
|
6
|
+
code?: string;
|
|
7
|
+
utm?: {
|
|
8
|
+
source?: string;
|
|
9
|
+
medium?: string;
|
|
10
|
+
campaign?: string;
|
|
11
|
+
term?: string;
|
|
12
|
+
content?: string;
|
|
13
|
+
};
|
|
14
|
+
referrer?: string;
|
|
15
|
+
at: number;
|
|
16
|
+
}
|
|
17
|
+
interface Touches {
|
|
18
|
+
first?: Touch;
|
|
19
|
+
last?: Touch;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/index.d.ts
|
|
23
|
+
/** The part of the PlayStep SDK the add-on uses: any object with these two methods. */
|
|
24
|
+
export interface PlayStepLike {
|
|
25
|
+
identify(userId: string, traits?: Record<string, unknown>): unknown;
|
|
26
|
+
reset(): unknown;
|
|
27
|
+
}
|
|
28
|
+
export interface ReferralsOptions {
|
|
29
|
+
/** The environment's publishable key (ps_pub_…), the same one PlayStep is set up with. */
|
|
30
|
+
appKey: string;
|
|
31
|
+
/** PlayStep's API, for local development or staging. */
|
|
32
|
+
api?: string;
|
|
33
|
+
/** Your app's other domains (app.acme.io when this is acme.com): links to them carry the visitor's ref along. */
|
|
34
|
+
appDomains?: string[];
|
|
35
|
+
/** 'required': remember nothing until consent(true). Default 'granted'. */
|
|
36
|
+
consent?: 'granted' | 'required';
|
|
37
|
+
}
|
|
38
|
+
export interface FriendLink {
|
|
39
|
+
/** The user's own link, such as https://go.playstep.app/f-3k9x2m7q1a. */
|
|
40
|
+
link: string;
|
|
41
|
+
/** Their code, for people who type it in. */
|
|
42
|
+
code: string;
|
|
43
|
+
}
|
|
44
|
+
export interface Referrals {
|
|
45
|
+
/** Where this visitor came from: the first touch and the latest. */
|
|
46
|
+
readonly touches: Touches;
|
|
47
|
+
/** Records consent (or its withdrawal, which forgets the touches). */
|
|
48
|
+
consent(granted: boolean): void;
|
|
49
|
+
/** What the wrapped identify() calls: sends the touches for this user once. */
|
|
50
|
+
identify(userId: string, traits?: Record<string, unknown>): void;
|
|
51
|
+
/** What the wrapped reset() calls: forgets the signed-in user. */
|
|
52
|
+
reset(): void;
|
|
53
|
+
/** The signed-in user's own friend link and code, when the app has a friend program; null otherwise. */
|
|
54
|
+
myLink(): Promise<FriendLink | null>;
|
|
55
|
+
}
|
|
56
|
+
/** The key the SDKs file a signed-in user's events under: `u_` + 40 hex of sha256(publishableKey:userId). */
|
|
57
|
+
export declare const userKey: (appKey: string, userId: string) => string;
|
|
58
|
+
export declare function referrals(playstep: PlayStepLike | null, options: ReferralsOptions): Referrals;
|
|
59
|
+
/**
|
|
60
|
+
* Wraps the core's identify() and reset() in place, once: every caller (React's provider included) is covered. A
|
|
61
|
+
* PlayStep already wrapped keeps its first add-on, which is returned.
|
|
62
|
+
*/
|
|
63
|
+
export declare function attach(playstep: PlayStepLike, self: Referrals): Referrals;
|
|
64
|
+
//#endregion
|
|
65
|
+
export type { Touch, Touches };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=[];for(let t=2;e.length<64;t++){let n=!0;for(let e=2;e*e<=t;e++)t%e===0&&(n=!1);n&&e.push(Math.cbrt(t)%1*2**32|0)}const t=(e,t)=>e>>>t|e<<32-t;function n(n){let r=new TextEncoder().encode(n),i=r.length,a=new Uint32Array((i+8>>6)*16+16);for(let e=0;e<i;e++)a[e>>2]|=r[e]<<24-e%4*8;a[i>>2]|=128<<24-i%4*8,a[a.length-1]=i*8,a[a.length-2]=Math.floor(i/536870912);let o=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],s=new Uint32Array(64);for(let n=0;n<a.length;n+=16){for(let e=0;e<64;e++)if(e<16)s[e]=a[n+e];else{let n=s[e-15],r=s[e-2];s[e]=s[e-16]+(t(n,7)^t(n,18)^n>>>3)+s[e-7]+(t(r,17)^t(r,19)^r>>>10)}let[r,i,c,l,u,d,f,p]=o;for(let n=0;n<64;n++){let a=p+(t(u,6)^t(u,11)^t(u,25))+(u&d^~u&f)+e[n]+s[n]|0,o=(t(r,2)^t(r,13)^t(r,22))+(r&i^r&c^i&c)|0;p=f,f=d,d=u,u=l+a|0,l=c,c=i,i=r,r=a+o|0}[r,i,c,l,u,d,f,p].forEach((e,t)=>o[t]=o[t]+e|0)}return o.map(e=>(e>>>0).toString(16).padStart(8,`0`)).join(``)}const r=(e,t)=>e?.trim().slice(0,t)||void 0;function i(){let e=location.hostname.split(`.`);if(e.length<2||/^[\d.]+$/.test(location.hostname))return``;for(let t=e.length-2;t>=0;t--){let n=`;domain=${e.slice(t).join(`.`)};path=/`;if(document.cookie=`_ps_t=1${n}`,document.cookie.includes(`_ps_t=1`))return document.cookie=`_ps_t=${n};max-age=0`,e.slice(t).join(`.`)}return``}function a(e=location,t=document.referrer,n=Date.now()){let i=new URLSearchParams(e.search),a={at:n},o={},s=!1;for(let e of[`source`,`medium`,`campaign`,`term`,`content`]){let t=r(i.get(`utm_${e}`),100);t&&(s=!!(o[e]=t))}s&&(a.utm=o),(a.click=r(i.get(`ps_ref`),200))&&(s=!0),(a.ref=r(i.get(`ref`),64))&&(s=!0);let c=/^https?:\/\/([^/:?#]+)/i.exec(t)?.[1]?.toLowerCase(),l=e.hostname.split(`.`).slice(-2).join(`.`);return c&&c!==l&&!c.endsWith(`.${l}`)&&(s=!!(a.referrer=c.slice(0,255))),s?a:void 0}function o(){try{return JSON.parse(decodeURIComponent(/(?:^|; )_ps_ref=([^;]+)/.exec(document.cookie)?.[1]??`{}`))}catch{return{}}}function s(e,t){document.cookie=`_ps_ref=${e?encodeURIComponent(JSON.stringify(e)):``};path=/;samesite=lax;max-age=${e?7776e3:0}${t?`;domain=${t}`:``}${location.protocol===`https:`?`;secure`:``}`}const c=(e,t)=>t?{first:e.first??t,last:t}:e,l=`__playstepReferrals`,u=`playstep:ref:sent`,d=(e,t)=>`u_${n(`${e}:${t}`).slice(0,40)}`;function f(e,t){let r=(t.api??`https://cdn.playstep.app`).replace(/\/$/,``),l=i(),f=t.consent!==`required`,m=c(o(),a()),h,g;f&&s(m,l);let _=t.appDomains?.map(e=>e.toLowerCase());_?.length&&document.addEventListener(`click`,e=>{let t=e.target?.closest?.(`a[href]`),n=m.last;if(!t||!n||!_.includes(t.hostname)||t.hostname===location.hostname)return;let r=new URL(t.href);n.click&&r.searchParams.set(`ps_ref`,n.click),n.ref&&r.searchParams.set(`ref`,n.ref),t.href=r.href},!0);let v=()=>{if(!h||!f)return;let e=``;try{e=localStorage.getItem(u)??``}catch{}let i=n(h+JSON.stringify(m)).slice(0,16);e.includes(i)||fetch(`${r}/v1/referrals/attribution`,{method:`POST`,keepalive:!0,headers:{"content-type":`text/plain`},body:JSON.stringify({key:t.appKey,user:h,...m,email:g})}).then(t=>t.ok&&localStorage.setItem(u,`${i},${e}`.slice(0,340))).catch(()=>{})},y={get touches(){return m},consent(e){f=e,e||(m={}),s(e?m:null,l),v()},identify(e,r){if(!e)return;h=d(t.appKey,`${e}`);let i=r?.email;g=typeof i==`string`&&i.trim()?n(i.trim().toLowerCase()):void 0,v()},reset(){h=g=void 0},myLink:()=>h?fetch(`${r}/v1/referrals/me?key=${t.appKey}&user=${h}`).then(e=>e.ok?e.json():null).catch(()=>null):Promise.resolve(null)};return e?p(e,y):y}function p(e,t){let n=e;if(n[l])return n[l];let{identify:r,reset:i}=n;return n.identify=(e,i)=>{let a=r.call(n,e,i);return t.identify(e,i),a},n.reset=()=>{let e=i.call(n);return t.reset(),e},n[l]=t}export{p as attach,f as referrals,d as userKey};
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axceera/playstep-referrals",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PlayStep Referrals: knows where your users came from (referral and affiliate links, promo codes, UTM tags) and tells PlayStep when they sign in. An add-on to @axceera/playstep; nothing is added to the core.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"homepage": "https://playstep.app/docs/sdk/referrals",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/*.js",
|
|
20
|
+
"dist/*.d.ts",
|
|
21
|
+
"dist/cdn"
|
|
22
|
+
],
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@size-limit/file": "^14.2.0",
|
|
25
|
+
"happy-dom": "^20.14.6",
|
|
26
|
+
"size-limit": "^14.2.0",
|
|
27
|
+
"terser": "^5.51.2",
|
|
28
|
+
"tsdown": "^0.23.0",
|
|
29
|
+
"typescript": "~6.0.3",
|
|
30
|
+
"vitest": "^4.1.11",
|
|
31
|
+
"@playstep/spec": "0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsdown && node scripts/cdn.ts",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"size": "size-limit",
|
|
37
|
+
"typecheck": "tsc --noEmit",
|
|
38
|
+
"lint": "eslint ."
|
|
39
|
+
}
|
|
6
40
|
}
|