Skip to content

Provide way to access client initialization state #188

@wesbos

Description

@wesbos

Currently if the Counterscale.init() function is run twice, it throws and error.

Sometimes code runs twice - like in react strict mode. So if it's already initialized, it would be nice to do one of these:

  1. It just returns early. Maybe silently?
  2. Provide the a function that will return the client, or a boolean from the GLOBALS variable here: https://github.com/benvinegar/counterscale/blob/main/packages/tracker/src/index.ts#L7 so ew can conditionally run init()

Also while we're talking about the client code - is it possible to also return early if history doesn't exist? Helpful for not breaking server rendered code. Currently doing this:

let isInitialized = false;
const isClient = typeof window !== 'undefined';

export function analytics() {
  if (isClient && !isInitialized) {
    Counterscale.init({
      siteId: `wesbos-${import.meta.env.DEV ? '-dev' : ''}`,
      reporterUrl: 'https://nunyabiznass.wesbos.com/collect',
    });
    isInitialized = true;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions