|
1 | | -import { Reaction } from '..'; |
| 1 | +declare global { |
| 2 | + interface Window { |
| 3 | + chmln: any; |
| 4 | + chmln_resolvers: { |
| 5 | + resolve: any; |
| 6 | + reject: any; |
| 7 | + }; |
| 8 | + } |
| 9 | +} |
2 | 10 |
|
3 | | -let _hasLoadedScript = false; |
| 11 | +let _script; |
| 12 | + |
| 13 | +function loadScript() { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + window.chmln_resolvers = { |
| 16 | + resolve, |
| 17 | + reject, |
| 18 | + }; |
| 19 | + const script = document.createElement('script'); |
| 20 | + script.innerHTML = `/* Chameleon - better user onboarding */!function(t,n,o){var a="chmln",e="adminPreview",c="setup identify alias track clear set show on off custom help _data".split(" ");if(n[a]||(n[a]={}),n[a][e]&&(n[a][e]=!1),!n[a].root){n[a].accountToken=o,n[a].location=n.location.href.toString(),n[a].now=new Date;for(var s=0;s<c.length;s++)!function(){var t=n[a][c[s]+"_a"]=[];n[a][c[s]]=function(){t.push(arguments)}}();var i=t.createElement("script");i.src="https://fast.trychameleon.com/messo/"+o+"/messo.min.js",i.async=!0,i.onload = window.chmln_resolvers.resolve,i.onerror = window.chmln_resolvers.reject,t.head.appendChild(i)}}(document,window,"SD8v1wAhTGvsMfAUSklVZC5ucKfiTB8uw73OJ9QOxIdiGn-1Ia9sN-BDlqTksEtFejdYPw");`; |
| 21 | + document.body.appendChild(script); |
| 22 | + }); |
| 23 | +} |
4 | 24 |
|
5 | 25 | export default { |
6 | | - initialize(reaction: Reaction) { |
7 | | - reaction( |
8 | | - state => state.user && state.user.id, |
9 | | - userId => { |
10 | | - if (!_hasLoadedScript && userId) { |
11 | | - const script = document.createElement('script'); |
12 | | - script.innerHTML = `/* Chameleon - better user onboarding */!function(t,n,o){var a="chmln",e="adminPreview",c="setup identify alias track clear set show on off custom help _data".split(" ");if(n[a]||(n[a]={}),n[a][e]&&(n[a][e]=!1),!n[a].root){n[a].accountToken=o,n[a].location=n.location.href.toString(),n[a].now=new Date;for(var s=0;s<c.length;s++)!function(){var t=n[a][c[s]+"_a"]=[];n[a][c[s]]=function(){t.push(arguments)}}();var i=t.createElement("script");i.src="https://fast.trychameleon.com/messo/"+o+"/messo.min.js",i.async=!0,t.head.appendChild(i)}}(document,window,"SD8v1wAhTGvsMfAUSklVZC5ucKfiTB8uw73OJ9QOxIdiGn-1Ia9sN-BDlqTksEtFejdYPw"); |
13 | | - chmln.identify(${userId}, {});`; |
14 | | - document.body.appendChild(script); |
15 | | - _hasLoadedScript = true; |
16 | | - } |
17 | | - } |
18 | | - ); |
| 26 | + /* |
| 27 | + This method might be called at any point in time, with a user or not. We want to |
| 28 | + make sure that we load the script on first call and then make sure it finishes loading |
| 29 | + if any new calls are made. |
| 30 | + */ |
| 31 | + loadTour(userId: string) { |
| 32 | + if (!_script) { |
| 33 | + _script = loadScript(); |
| 34 | + } |
| 35 | + |
| 36 | + if (userId) { |
| 37 | + return _script.then(() => { |
| 38 | + window.chmln.identify(userId, {}); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + let uid; |
| 43 | + try { |
| 44 | + uid = document.cookie.match(/\bvisitor-uid=([a-z0-9-]+)(;|$)/)[1]; |
| 45 | + } catch (e) { |
| 46 | + document.cookie = |
| 47 | + 'visitor-uid=' + |
| 48 | + (uid = Math.random() |
| 49 | + .toString(36) |
| 50 | + .substring(2)) + |
| 51 | + '; expires=Tue, Oct 13 2037 04:24:07 UTC; path=/;'; |
| 52 | + } |
| 53 | + |
| 54 | + return _script.then(() => { |
| 55 | + window.chmln.identify(uid, { visitor: true }); |
| 56 | + }); |
19 | 57 | }, |
20 | 58 | }; |
0 commit comments