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

as24-optimizely-fragment

v1.1.16

Published

AS24 OptimizelyX integration helper fragment

Downloads

16

Readme

Shared OptimizelyX Fragment for AS24 Pages

AS24<->OptimizelyX integration helper fragment

What do we use OptimizelyX for?

We use Optimizely for conducting multivariant tests (A/B tests).

What does this fragment do?

This fragment loads the OptimizelyX JS (client side) API in an optimized way: asynchronously but still very fast.

Our main Project "AS24 CXP" is hardcoded in the currently only fragment. Other projects are welcome to join.

Optimizely Full Stack is out of scope of this project.

How to use OptimizelyX?

For detailed API reference please check out the official Optimizely docs

Installing type-definitions

This package kindly provides TypeScript declarations for OptimizelyX (WIP) and the custom-element integration. You can benefit from these by installing

yarn add -D as24-optimizely-fragment

and importing the declarations somewhere in your app

import 'as24-optimizely-fragment';

The definitions will make the window object aware of the different optimizely methods and response types, plus patch the JSX namespace to support the as24-optimizely-fragment custom element. They will also make sure you check the existance of window.optimizely and window.optimizelyReady since they might not be present in the page depending on script order or fragment inclusion.

⚠️ Caution

There is no guarantee that window.optimizely is defined (e.g. fragment is not included, ad blocker blocks optimizely, etc.) or library hasn't loaded yet. Therefore, either

a) make sure optimizely is initialized (this happens automatically if you use the optimizelyReady promise) b) or include this line in your codebase before interacting with the Optimizely API (push-functionality only).

window.optimizely = window.optimizely || [];

TypeScript users: to prevent runtime errors, you'll need to check for window.optimizely existance and methods besides push .

The variation map a.k.a. the test bucket in which the user has landed

{
    '6661191859': {
        //experiment ID
        id: '6670551924', // variation ID
        name: 'Variation #1',
        index: '1' // variation index
    }
}

Getting and using the variationMap

const onOptimizelyReady = () => {
    const experimentID = '6661191859';

    const state = window.optimizely.get('state');
    const variationMap = state.getVariationMap();
    const experiment = variationMap[experimentID];
    const defaultVariationIndex = 0;

    // showVariation is defined by the developer
    // This function shows the variant which the users was bucketed to or the default if user is not in the experiment.
    showVariation(experiment ? experiment.index : defaultVariationIndex);
};

window.optimizely = window.optimizely || [];

if (!window.optimizely.initialized) {
    window.optimizely.push({
        type: 'addListener',
        filter: {
            type: 'lifecycle',
            name: 'initialized'
        },
        handler: onOptimizelyReady
    });
} else {
    // Optimizely has already been initialized. Our code is only executed after Optimizely initialization.
    onOptimizelyReady();
}

Tracking events

window.optimizely = window.optimizely || [];
window.optimizely.push({
    type: 'event',
    eventName: 'watchedVideo',
    tags: {
        // Additional metadata for targeting (optional)
        title: 'Funny Cats',
        duration: 30
    }
});

Tracking events with a custom element

<as24-optimizely
    action="push"
    payload='
    {
        "type": "event",
        "eventName": "watchedVideo",
        "tags": {
            "title": "Funny Cats",
            "duration": 30
        }
    }
'
></as24-optimizely>

Add the fragment to your page

<!-- #include virtual="/fragment/optimizely/as24-cxp/" -->

Convenience helpers

This fragment defines a promise in the head of the document which resolves after the Optimizely snippet has been loaded and executed.

The promise resolves with an object which already has some useful objects, e.g. variationMap. This could also be extended.

const { optimizely, state, variationMap } = await window.optimizelyReady;

Campaign decided helper

Getting the correct status for an experiment can be cumbersome. In particular when the traffic allocation for the experiment is less than 100% it's possible that the user is assigned a variation but that variation is in holdback. This means that the user is actually not part of the experiment audience. In order to do so we need to hook into the campaignDecided callback doc.

In order to simplify this, window.optimizelyHelper is available. Using optimizelyHelper.get(experimentId) returns a promise that always resolves. If the user is not part of the experiment it will resolve with a { error: string } object, otherwise it will resolve with a CampaignEventData object.

Publishing new version

The types are exported to the public npm registry for easy consumption. You can simply run from your local machine

yarn publish --patch --message 'YOUR COMMIT MESSAGE HERE'

the first time yarn will ask for a username: autoscout24 and email #[email protected]. Ask in slack's engagement or acquisition channels for the password.

(where you can use --patch, --minor or --major depending on the version bump you want)

Only the files listed in the "files" section of package.json are included