clarity icon indicating copy to clipboard operation
clarity copied to clipboard

Is it possible to install and use clarity-js instead of embedding script?

Open mikan3rd opened this issue 3 years ago • 10 comments

Clarity manual says to set it up by embedding a script. I have already confirmed that it works correctly when script is embedded. https://docs.microsoft.com/en-us/clarity/clarity-setup

However, I think it would be smarter if I could npm install and use clarity-js . After doing npm install clarity-js, I started clarity as follows, but the analysis did not POST. I couldn't see any errors either.

import { clarity } from 'clarity-js';

clarity.start({ projectId: ClarityProjectId });

Is it possible to use clarity-js instead of embedding script? What is the purpose of clarity-js if it is not available?

It would be helpful if you could specify how to use it in the README.

mikan3rd avatar Aug 18 '21 03:08 mikan3rd

@mikan3rd, have you figured this out to work at your end?

karandatwani92 avatar Sep 25 '22 05:09 karandatwani92

After a couple of days trying to use Clarity-JS in a React Project i was finally able to make it work, however the lack of documentation made it very hard.

Since i wanted to track also user info, i implemented as soon as user authenticate in the system:


useEffect(() => {
        clarity.consent()
        if (user?.email) {
            const { id, email, name } = user

            // The only way a found to pass specific info to clarity so i can filter info was using 
            // cookies
            setCookie('user_id', id, 1)
            setCookie('user_email', email, 1)
            setCookie('user_name', name, 1)
            clarity.set('user_id', id)
            clarity.set('user_email', email)
            clarity.set('user_name', name)

            clarity.start({
                projectId: 'YOUR_PROJECT_ID',
                upload: 'https://m.clarity.ms/collect',
                track: true,
                content: true,
                cookies: ['user_id', 'user_email', 'user_name']
            })
        }

        return () => {
            clarity.stop()
        }
    }, [user])

Also give it some time, some times it can take half an hour until i see some recordings on clarity website

henriquemod avatar Sep 25 '22 12:09 henriquemod

Thank you @henriquemod for the code snippet, But using clarity-js instead of embedding the script, the clarity live extension is failing , is there any config change required to allow the plugin .

srikiranvelpuri avatar Jan 23 '23 15:01 srikiranvelpuri

Would be nice for the team to actually add some documentation here. Has anyone come across any docs yet?

almcaffee avatar Apr 28 '23 14:04 almcaffee

any update on this? what's the best approach to replace the embedded script

giulianok avatar May 06 '23 13:05 giulianok

Install:

npm i clarity-js

Import:

import { clarity } from 'clarity-js';

Use:

clarity.start({
    projectId: "xxxxxx",
    upload: 'https://m.clarity.ms/collect',
    track: true,
    content: true,
});

karandatwani92 avatar May 07 '23 06:05 karandatwani92

import { clarity } from 'clarity-js';

This results in error of Module '"clarity-js"' has no exported member 'clarity' since the 'clarity-js' package exports the 'clarity' object as a whole, not its properties as named exports.

however once you try to import it with:

import * as clarity from 'clarity-js';

You get errors on trying to use start():

Property 'start' does not exist on type 'typeof import("clarity-js")'.

tmchow avatar Jun 18 '23 04:06 tmchow

No amount of mucking with this could I get this to cleanly work. I've raised this issue here: https://github.com/microsoft/clarity/issues/408

tmchow avatar Jun 18 '23 19:06 tmchow

honestly speaking, it is from Microsoft, i think it will be great to have documentation of how to use clarity-js right? if it is meant to be used by others. Kamsahamida! Xie Xie! Thank You!

hocktoh89 avatar Oct 17 '23 04:10 hocktoh89

window is not defined at eval (c:/Users/sit310.SIT/Desktop/CloneSurveyor_Admin/node_modules/clarity-js/build/clarity.module.js:76:9) at async instantiateModule (file:///C:/Users/sit310.SIT/Desktop/CloneSurveyor_Admin/node_modules/vite/dist/no

HemangDholu avatar Jan 30 '24 10:01 HemangDholu