npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-autoinstrument

v2.1.3

Published

Automatically add `data-gs` attributes to elements to make them uniquely identifiable in the DOM

Downloads

25

Readme

Babel Plugin Auto-Instrument

This plugin will automatically generate data-gs and data-gs-c attributes for JSX elements based off of their path and position in the JSX tree.

For example, the path /app/components/file.js and JSX <Content><Main/></Content> would generate the following data-gs attribute for <Main>:

<Main data-gs="app__components__file__Content__Main />

The data-gs-c attribute (short for "data-gainsight-content") is derived from an elements children, content or various attribute (e.g. title or name.) An example:

<Content>
    <Header>
      A Title
    </Header>
</Content>

in the file /app/pages/News.js would generate the following attributes for <Header>:

<Header data-gs="app__pages__News__Content__Header" data-gs-c="a-title">

Motivation

We want to use the analytics solution Gainsight to track usage of our application. In Gainsight, features such as "Create new Query" are mapped to elements in the DOM, like the "Create Query" button.

These elements need to be uniquely identifiable. To minimize maintenance effort, the unique attributes are generated at build time, instead of manually adding id attributes (or other unique characteristics.) This also allows mapping new features without requiring changes from the front-end team and a new release.

Gainsight uses CSS selectors to identify elements in the DOM, for example

[data-gs*="News"][data-gs-c*="title"]

would match the <Header data-gs="app_pages_News" data-gs-c="a-title">. The selectors use substring matching and are chosen to be as robust as possible, e.g. adding a wrapping <div> around the <Header> would not break the selector, even though the data-gs attribute would now contain app__pages__News__Content__div__Header.

Should the selector break with a new release (e.g. renaming News.js to Blog.js), Gainsights backfill feature can be used to fill in the gap after the release. Gainsight tracks all interactions with the app, and stores the target elements attributes and the parent elements from the DOM. A new selector (e.g. [data-gs*=Blog"][data-gs-c*="title"]) can retroactively be matched against this history of interactions and mapped to a certain feature.

The data-gs-c attribute is necessary for e.g. <label> elements and other that contain short but usually identifying content, as it's not possible to match against content with CSS selectors (unlike with e.g. XPath.)

Caveats

In some cases, data-gs needs to be explicitly forwarded to JSXElements that render DOM nodes. For example, if a shared component doesn't forward data-gs, then only the attributes generate for the shared component will end up in the DOM, not the attributes where it was used:

Shared component /app/components/Button.jsx:

export function Button({ title, style, children, onClick }) {
  return (
    <a onClick={onClick} title={title} className={style === 'important' ? 'red' : 'green'}>
        <span className='expand-padding'>
            <span className={style === 'important' ? 'big' : 'small'}>
                {children}
            </span>
        </span>
    </a>
  )
}

Usage /app/pages/DatabaseEditor.jsx:

<Form>
    <Button style='important'>
        Delete Database
    </Button>
</Form>

In DatabaseEditor.jsx, would get the following data-gs props:

data-gs={gs('app', 'pages', 'DatabaseEditor', 'Form', 'Button')}
data-gs-c={gsC('delete-database')}

This would help identify this particular button via the delete-database string. However, as the shared component does not forward data-gs or data-gs-c that's passed to it, the actual DOM node will end up like this:

<a class='red' data-gs='app__components__Button'>
    <span class='expand-padding' data-gs='app__components__Button__span'>
        <span class="big' data-gs='app__components__Button__span__span' data-gs-c="delete-database">
            Delete Database
        </span>
    </span>
</a>

The padding on <a>s child will expand the clickable area in <a>, however the useful data-gs-c attribute ends up on the second <span>. Gainsight would now only count clicks on "Delete Database" if they actually hit the letters, and not if they hit the margin/padding.

Depending on the design, the margin/padding can be a significant part of the button, and Gainsight would miss clicks on that area, which will skew our statistics.

Therefore, the shared component needs to be modified to forward data-gs and data-gs-c to the <a> tag:

export function Button({ title, style, children, onClick, 'data-gs': dataGs, 'data-gs-c': dataGsC }) {
  return (
    <a
        onClick={onClick}
        title={title}
        className={style === 'important' ? 'red' : 'green'}
        data-gs={dataGs}
        data-gs-c={dataGsC}
    >
        <span className='expand-padding'>
            <span className={style === 'important' ? 'big' : 'small'}>
                {children}
            </span>
        </span>
    </a>
  )
}

The output will now be

<a class='red' data-gs='app__pages__DatabaseEditor__Form__Button' data-gs-c="delete-database">
    <span class='expand-padding' data-gs='app__components__Button__span'>
        <span class="big' data-gs='app__components__Button__span__span' data-gs-c="delete-database">
            Delete Database
        </span>
    </span>
</a>

and the following CSS selector would track all clicks on the Button, even those that hit margin/padding:

[data-gs*=DatabaseEditor][data-gs-c*="delete-database"]