Skip to content

Commit a254c1e

Browse files
Improve loading of the chameleon script
1 parent d82c7b7 commit a254c1e

File tree

4 files changed

+55
-21
lines changed

4 files changed

+55
-21
lines changed
Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
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+
}
210

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+
}
424

525
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+
});
1957
},
2058
};

packages/app/src/app/overmind/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
IDerive,
55
IOnInitialize,
66
IOperator,
7-
IReaction,
87
IState,
98
} from 'overmind';
109
import { createHook } from 'overmind-react';
@@ -63,9 +62,6 @@ export interface OnInitialize extends IOnInitialize<Config> {}
6362
export interface Action<Input = void, Output = void>
6463
extends IAction<Config, Input, Output> {}
6564

66-
export interface Reaction<Input = void, Output = void>
67-
extends IReaction<Config> {}
68-
6965
export interface AsyncAction<Input = void, Output = void>
7066
extends IAction<Config, Input, Promise<Output>> {}
7167

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ export const contentMounted: Action = ({ state, effects }) => {
136136

137137
return null;
138138
});
139+
140+
effects.chameleon.loadTour(state.user && state.user.id);
139141
};
140142

141143
export const resizingStarted: Action = ({ state }) => {

packages/app/src/app/overmind/onInitialize.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ export const onInitialize: OnInitialize = (
66
) => {
77
const provideJwtToken = () => state.jwt || effects.jwt.get();
88

9-
effects.chameleon.initialize(overmindInstance.reaction);
10-
119
effects.fsSync.initialize({
1210
onModulesByPathChange(cb: (modulesByPath: any) => void) {
1311
overmindInstance.reaction(

0 commit comments

Comments
 (0)