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

statsig-code-injection

v0.0.2

Published

Code injection library helps you run custom code in your landing page based on a Statsig experiment

Downloads

5

Readme

code-injection

Installation

Add this helper script to the <head> of your webpage

<script src="http://localhost/code-injection/src/index.js?apikey=[CLIENT-API-KEY]&configkey=[DYNAMIC-CONFIG-KEY]"></script>

Statsig Setup

Dynamic Config

  • Contains list of web tests with directions on where (url) and when (triggers) to activate the tests.
  • The Config Key should be used in the installation script as shown in the installation instructions above.
  • The top level entry should be experiments, which is an Array of Experiment Objects.
{
  experiments: [
    {
      key: 'simple_test',
      url: '/products/.*$|test(-static)?.html$',
      triggers: {
        interval_condition: {
          js: '(function() { return window.dataLayer.products; })();',
          timeout: 4000,
        },
        element_exists: {
          selector: 'img.pdp',
          continuous: true,
        },
        page_lifecycle: 'dom_ready',
      },
    },
    {
      key: 'payment_plan_page_test',
      url: '/payment-plan',
      triggers: {
        element_exists: 'section[data-component-name="collapsableplans"]',
      },
    },
  ]
}

Experiment Object definitions

| parameter | type | description | required | |-----------|--------|----------------------|----------| | key | string | An experiment_key in statsig. Any experiment keys defined here will be attempted to activate following the conventions defined below. | yes | | url | string | A Regex expression that determines where an experiment should activate. This will be checked once when the tool is initialize. | yes | | triggers | object | An object containing a set of trigger conditions | no | | triggers.interval_condition | object | This condition is for checking a condition on the webpage using custom javascript to determine if a test should activate | no | | triggers.interval_condition.js | string | This should be a javascript expression that returns boolean true or false | no | | triggers.interval_condition.timeout | integer | This value determines how long to check for the condition. This dictates when to abort evaluating the js condition within a setInterval loop. | no | | triggers.element_exists | object or string | This condition is for activating a test based on an element being pressent on a webpage. Note: You can provide a selector string as the value, which will do a one-time check for the element and then activate and run the experiment code. | no | | triggers.element_exists.selector | string | The element selector to look for. | yes | | triggers.element_exists.continuous | bool | Enabling this will continuously run the experiment code. When disabled, the experiment code will run only once | yes | | triggers.pageload_phase | string (dom_ready, window_onload) | This will ensure that experiment and it's code doesn't run until the specified phase during pageload. dom_ready is will be when all nodes are written to DOM and it's ready to be modified. window_onload is later in the lifecycle, once all external assets have also been loaded. | no |

Experiment Parameters

Each experiment included in the Dynamic Config above must contain either a code parameter or codeConfig + codeKey parametes.

  • Use code to define variation javascript that will be executed when the test is activated and the user is assigned to a Group.

  • Use the codeConfig + codeKey approach to reference a block of javascript defined within a Dynamic Config. This approach allows code to be reused across multiple experiments. The codeConfig parameter should indicate the Dynamic Config key that contains the code, the codeKey indicates the Object key within that Dynamic config that contains the javascript that should be executed when the test is activated and the user is assigned to a Group.