microsoft / clarity

A behavioral analytics library that uses dom mutations and user interactions to generate aggregated insights.
https://clarity.microsoft.com
MIT License
2.09k stars 208 forks source link

How to detect when Clarity is loaded #488

Open danilofuchs opened 11 months ago

danilofuchs commented 11 months ago

Is there an event for something like window.addEventListener("clarity:start")?

It would be useful to make sure our calls to identify() happen after that

wuservices commented 11 months ago

I was wondering the same thing, for the same reason - to call clarity.identify. This is especially important if loading Clarity via UET, since you may know if UET loads, but not after UET loads Clarity.

wuservices commented 11 months ago

How about this?

https://github.com/microsoft/clarity/blob/a60d2e7389e305da017e91c61ea9eb5705324edf/packages/clarity-js/src/queue.ts#L7-L16 shows there's an array queue and it seems to be flushed. Not sure if this is officially supported (i.e. if it could break later), but it seems to work.

// Start queuing up calls if Clarity hasn't loaded
window.clarity = window.clarity || function() {
  (window.clarity.q = window.clarity.q || []).push(arguments)
}