0.0.2 • Published 1 year ago

statsig-code-injection v0.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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

parametertypedescriptionrequired
keystringAn experiment_key in statsig. Any experiment keys defined here will be attempted to activate following the conventions defined below.yes
urlstringA Regex expression that determines where an experiment should activate. This will be checked once when the tool is initialize.yes
triggersobjectAn object containing a set of trigger conditionsno
triggers.interval_conditionobjectThis condition is for checking a condition on the webpage using custom javascript to determine if a test should activateno
triggers.interval_condition.jsstringThis should be a javascript expression that returns boolean true or falseno
triggers.interval_condition.timeoutintegerThis 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_existsobject or stringThis 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.selectorstringThe element selector to look for.yes
triggers.element_exists.continuousboolEnabling this will continuously run the experiment code. When disabled, the experiment code will run only onceyes
triggers.pageload_phasestring (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.