@sqid-dev/playground 0.1.0 → 0.1.1
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/package.json +1 -1
- package/public/recipes.js +31 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqid-dev/playground",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A real-looking website (Driftwood, a desk company) with a Sqid panel docked on the side: watch a visitor get identified, try the scenarios and recipes, see the record that goes out, then go live on your own site.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
package/public/recipes.js
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
const cap = (s) => String(s || '').replace(/\b[a-z]/g, c => c.toUpperCase());
|
|
8
8
|
const first = (v) => cap(v?.firstName || (v?.name || '').split(' ')[0] || '') || null;
|
|
9
9
|
const company = (v) => v?.company ? cap(v.company) : null;
|
|
10
|
-
const sizeOf = (v) => v?.company_size || v?.job_company_size || null;
|
|
11
10
|
|
|
12
11
|
const RECIPES = {
|
|
13
12
|
dom : {
|
|
@@ -34,31 +33,37 @@ engine.on('visitor.unknown', () => {
|
|
|
34
33
|
},
|
|
35
34
|
pricing : {
|
|
36
35
|
title : 'Pricing personalisation', where : '/pricing', docs : 'https://docs.sqid.dev/recipes/pricing-personalization',
|
|
37
|
-
code :
|
|
36
|
+
code : `// Sqid tells you who the visitor is. Which plan fits them is your data:
|
|
37
|
+
// route the profile through your backend and let it answer.
|
|
38
|
+
const engine = new squid.engine({ sp: 'https://api.example.com' });
|
|
38
39
|
|
|
39
40
|
engine.on('visitor.identified', (e) => {
|
|
40
|
-
const v = e.detail;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
document.querySelector('#pricing-title').textContent = \`Team pricing for \${v.company}\`;
|
|
44
|
-
document.querySelector('#plan-team').classList.toggle('featured', team);
|
|
41
|
+
const v = e.detail; // Sqid's traits + your fields
|
|
42
|
+
document.querySelector('#pricing-title').textContent = \`Pricing for \${v.company}\`;
|
|
43
|
+
document.querySelector(\`#plan-\${v.accountType.toLowerCase()}\`).classList.add('featured');
|
|
45
44
|
});`,
|
|
46
45
|
apply(v, on){
|
|
47
46
|
if(page() !== 'pricing'){ return; }
|
|
47
|
+
this._on = on;
|
|
48
48
|
let t = document.getElementById('pricing-title'), s = document.getElementById('pricing-sub'), pill = document.getElementById('pricing-pill');
|
|
49
|
-
let
|
|
50
|
-
let
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
let plans = { starter : document.getElementById('plan-starter'), team : document.getElementById('plan-team'), enterprise : document.getElementById('plan-enterprise') };
|
|
50
|
+
let badge = document.getElementById('plan-team-badge');
|
|
51
|
+
const reset = () => { t.textContent = t.dataset.default; s.textContent = s.dataset.default; pill.classList.add('hidden'); badge.classList.add('hidden'); Object.values(plans).forEach(p => p.classList.remove('featured')); };
|
|
52
|
+
const show = (d) => {
|
|
53
|
+
if(!this._on){ return; }
|
|
54
|
+
let plan = String(d.accountType || '').toLowerCase();
|
|
55
|
+
t.textContent = `Pricing for ${company(d)}`;
|
|
56
|
+
s.textContent = `Your account is on ${d.accountType}. (That came from the mock "your database", not from Sqid: Sqid supplied who you are.)`;
|
|
55
57
|
pill.classList.remove('hidden');
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
58
|
+
Object.entries(plans).forEach(([k, el]) => el.classList.toggle('featured', k === plan));
|
|
59
|
+
badge.classList.toggle('hidden', plan !== 'team');
|
|
60
|
+
};
|
|
61
|
+
if(!(v && on)){ reset(); return; }
|
|
62
|
+
if(this._last){ show(this._last); }
|
|
63
|
+
if(this._engine || !window.squid?.engine){ return; }
|
|
64
|
+
// The same service-provider pattern the docs recipe uses, against the playground's mock backend.
|
|
65
|
+
this._engine = new window.squid.engine({ sp : location.origin + '/_sqid/sp' });
|
|
66
|
+
this._engine.on('visitor.identified', e => { this._last = e.detail; show(e.detail); });
|
|
62
67
|
}
|
|
63
68
|
},
|
|
64
69
|
chat : {
|
|
@@ -128,15 +133,16 @@ document.addEventListener('mouseleave', (e) => {
|
|
|
128
133
|
|
|
129
134
|
engine.on('visitor.identified', (e) => {
|
|
130
135
|
const v = e.detail;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
window.analytics?.identify(
|
|
134
|
-
window.posthog?.identify(
|
|
136
|
+
const uid = v.id || engine.visitor().meta.user_id; // your id if you set one, else Sqid's
|
|
137
|
+
// Segment / Mixpanel / Amplitude / PostHog: one identify call each
|
|
138
|
+
window.analytics?.identify(uid, { email: v.email, company: v.company, title: v.company_role });
|
|
139
|
+
window.posthog?.identify(uid, { email: v.email, company: v.company });
|
|
135
140
|
});`,
|
|
136
141
|
apply(v, on){
|
|
137
142
|
if(!(v && on)){ return; }
|
|
138
|
-
|
|
139
|
-
|
|
143
|
+
let uid = v.id || document.cookie.match(/squid_vid=([^;]+)/)?.[1] || null;
|
|
144
|
+
console.log('[analytics] identify', uid, { email : v.email, company : v.company, title : v.company_role });
|
|
145
|
+
document.dispatchEvent(new CustomEvent('sqid:recipe', { detail : { recipe : 'analytics', text : `identify(${uid}) sent to the analytics stubs (see the browser console)` } }));
|
|
140
146
|
}
|
|
141
147
|
},
|
|
142
148
|
sp : {
|