microsoft / clarity

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

`clarity-js` docs #606

Open cloud-walker opened 1 month ago

cloud-walker commented 1 month ago

Hi all, I'm trying to use clarity-js instead of the official IIFE version, but I cannot find anywhere the docs on how to use it.

I'm missing something?

tugbadeveloptica commented 4 days ago

Hi all, I'm trying to use clarity-js instead of the official IIFE version, but I cannot find anywhere the docs on how to use it.

I'm missing something?

Hello, I want to use the repo in my project. Have you found a solution?

cloud-walker commented 4 days ago

Hello after various trial and error, chatting with GPT I've baked something like that (is a React project):

import {clarity} from 'clarity-js'
import {useEffect} from 'react'

import {envVars} from '~/envVars'

export function Clarity() {
  useEffect(() => {
    if (!envVars.VITE_CLARITY_PROJECT_ID) {
      return
    }

    const handleVisibilityChange = () => {
      if (document.hidden || document.visibilityState === 'hidden') {
        clarity.pause()
      } else {
        clarity.resume()
      }
    }

    clarity.consent() // we actually dont need to wait for user consent here as we are on OMP
    clarity.identify('unknown')
    clarity.start({
      projectId: envVars.VITE_CLARITY_PROJECT_ID,
      upload: 'https://m.clarity.ms/collect',
      content: true,
      track: true,
    })

    document.addEventListener('visibilitychange', handleVisibilityChange)

    return () => {
      document.removeEventListener('visibilitychange', handleVisibilityChange)
      clarity.stop()
    }
  }, [])

  return null
}
tugbadeveloptica commented 3 days ago

Hello after various trial and error, chatting with GPT I've baked something like that (is a React project):

import {clarity} from 'clarity-js'
import {useEffect} from 'react'

import {envVars} from '~/envVars'

export function Clarity() {
  useEffect(() => {
    if (!envVars.VITE_CLARITY_PROJECT_ID) {
      return
    }

    const handleVisibilityChange = () => {
      if (document.hidden || document.visibilityState === 'hidden') {
        clarity.pause()
      } else {
        clarity.resume()
      }
    }

    clarity.consent() // we actually dont need to wait for user consent here as we are on OMP
    clarity.identify('unknown')
    clarity.start({
      projectId: envVars.VITE_CLARITY_PROJECT_ID,
      upload: 'https://m.clarity.ms/collect',
      content: true,
      track: true,
    })

    document.addEventListener('visibilitychange', handleVisibilityChange)

    return () => {
      document.removeEventListener('visibilitychange', handleVisibilityChange)
      clarity.stop()
    }
  }, [])

  return null
}

thankk you veryy much , i will definitelyy try it