chamaeleonidae / chmln

Easily add Chameleon to your product
Other
18 stars 1 forks source link

chmln returns strange error (react/next.js) #4

Open adammo94 opened 1 year ago

adammo94 commented 1 year ago

So I have this react (next.js actually) implementation:

useEffect(() => {
    if (!window?.chmln && !!user) {
      chmln.init(CHAMELEON_API_KEY, { fastUrl: 'https://fast.chameleon.io/' });
      chmln.identify(user.id, {
        name: `${user.firstName} ${user.lastName}`,
      });
    }

    if (window?.chmln && !user) {
      chmln.clear();
    }
  }, [user]);

however let's say I just open chrome dev tools and when I log in and type in window.chmln or just chmln it returns something like this: (t){"progeny-definition"!==t&&(e.instanceMethods.init.apply(this,arguments),d.init.apply(this,arguments))} If I don't use chmln.identify method it (window.chmln inside dev tools' console) works just fine. What am I doing wrong?

bnorton commented 1 year ago

@adammo94 are you encountering a specific error with the above code or just that window.chmln (after calling chmln.identify) looks a little "odd"? The (t){"... is expected since the window.chmln plain object is replaced after the full chameleon JS is loaded into the page.

I'd say your implementation looks good to me -- do you need to anything "more" than what you have here?

adammo94 commented 1 year ago

Yes, here https://help.chameleon.io/en/articles/1228858-how-does-chameleon-work-with-single-page-applications it's said:

There might be some cases when you might want to clear the Chameleon installation but there's no page refresh taking place.
A common case, for example, is when there's no hard refresh when users log out of your SPA.

Which is exactly my case. I want to use window.chmln.clear, however since window.chmln changed into this (t){"... thing I cannot access it and it just gives me loads of errors in console when I try .clear method.

kovcic commented 1 year ago

Hello, I have the same issue with chmln.clear not being present, but that's expected according to the export:

Screenshot 2022-11-14 at 10 03 47

So question is what is wrong, the documentation (stating .clear should be used with SPA) or the export missing it?

damir-minkashev commented 1 year ago

@kovcic Right. chmln lib doesn t provide other functions window.chmln.clear works

bnorton commented 1 year ago

@DamirMinkashev @kovcic I see what you're referencing -- if you store and use the reference to "this library", the object you store will not (yet) have the other JavaScript api functions (such as clear, on, etc.). The immediate workaround is to re-reference window.chmln after the call to init.