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

@grahamcrackers/commerce-events-sdk

v2.0.0-beta.0

Published

SDK for working with events on an Adobe Commerce storefront

Downloads

6

Readme

Magento Storefront Events SDK

version downloads size build typescript contributing

Overview

This package serves as the foundation for eventing on an Adobe Commerce storefront. It provides access to a common data layer, and an event publishing and subscription service.

You can handle the events in a custom implementation, or use the Magento Storefront Event Collector package that listens for events and sends them to Adobe Commerce edges for processing.

Note: When an event is published through the SDK, all subscribers to that event get notified. Defining a custom listener shouldn't preclude you from also running the Magento Storefront Event Collector.

Our context schemas are designed to simplify forwarding to two edges:

  • Adobe Commerce Data Services (maintained by Adobe Engineering and used to power merchant performance dashboards)
  • Adobe Experience Platform (requires a subscription and additional merchant setup; data can be used by merchants inside the Adobe Experience Platform for detailed analytics, targeted merchandising, real time customer data profiles, and more)

Installation

This SDK can be used as a hosted script, or bundled in a JavaScript application. The script version is hosted on unpkg, and the bundled version is hosted on npm.

To load the SDK as a script, use the following snippet.

<script src="https://unpkg.com/@adobe/magento-storefront-events-sdk/dist/index.js"></script>

To install the script as a dependency, run this command.

npm install @adobe/magento-storefront-events-sdk

Quick Start

Once imported, you have access to the four main functions of the Events SDK.

Below is a code example of how to get started.

IMPORTANT: Relevant context data must be populated before publishing events that require it.

import mse from "@adobe/magento-storefront-events-sdk";
// handler - can go in a different module
function addToCartHandler(event) {
    // do something with the event
}
// subscribe to events
mse.subscribe.addToCart(addToCartHandler);

// set context data
const shoppingCartContext = {
    id: "1",
    items: [
        {
            id: "shoppingCart",
            product: {
                productId: 111111,
                sku: "ts001",
                pricing: {
                    regularPrice: 20.0,
                    currencyCode: "USD",
                },
            },
            quantity: 1,
        },
    ],
};
mse.context.setShoppingCart(shoppingCartContext);

// publish events
mse.publish.addToCart();

// unsubscribe from events
mse.unsubscribe.addToCart(addToCartHandler);

API Reference

The SDK API is broken down into four major parts: Context, Publish, Subscribe, Unsubscribe.

Context

These setters can be used to specify context in the mse:

setAEP

mse.context.setAEP(aepCtx);

Sets the AEP which can be used by event handlers to forward events to the Adobe Experience Platform. A client must have an AEP subscription and provide a valid IMS Org Id and Datastream Id.

setCategory

mse.context.setCategory(categoryCtx);

Sets the Category context.

setAccount

mse.context.setAccount(accountCtx);

Sets the Account context.

setCustomUrl

mse.context.setCustomUrl(customUrlCtx);

Sets the CustomUrl context.

setEventForwarding

mse.context.setEventForwarding(eventForwardingCtx);

Sets the EventForwarding context. Tells a handler if it should forward events to Adobe Commerce DataSolutions (commerce: true), Adobe Experience Platform (aep: true), or both.

setMagentoExtension @deprecated

mse.context.setMagentoExtension(magentoExtensionCtx);

Sets the MagentoExtension context. Includes Data Services extension version.

This field is deprecated. setDataServicesExtension should be used instead.

setDataServicesExtension

mse.context.setDataServicesExtension(dataServicesExtensionCtx);

Sets the DataServicesExtension context. Includes Data Services extension version.

setOrder

mse.context.setOrder(orderCtx);

Sets the Order context.

setPage

mse.context.setPage(pageCtx);

Sets the Page context.

setProduct

mse.context.setProduct(productCtx);

Sets the Product context.

setRecommendations

mse.context.setRecommendations(recommendationsCtx);

Sets the Recommendations context.

setRecommendationsExtension

mse.context.setRecommendationsExtension(recommendationsExtensionCtx);

Sets the RecommendationsExtension context. Includes Recommendations extension version.

setReferrerUrl

mse.context.setReferrerUrl(referrerUrlCtx);

Sets the ReferrerUrl context.

setSearchExtension

mse.context.setSearchExtension(searchExtensionCtx);

Sets the SearchExtension context. Includes Live Search extension version.

setSearchInput

mse.context.setSearchInput(searchInputCtx);

Sets the SearchInput context.

setSearchResults

mse.context.setSearchResults(searchResultsCtx);

Sets the SearchResults context.

setShopper

mse.context.setShopper(shopperCtx);

Sets the Shopper context.

setShoppingCart

mse.context.setShoppingCart(shoppingCartCtx);

Sets the ShoppingCart context.

setStorefrontInstance

mse.context.setStorefrontInstance(storefrontCtx);

Sets the StorefrontInstance context. This context is used when forwarding data to Adobe Commerce Data Services to identify the Adobe Commerce instance associated with the data.

setContext

mse.context.setContext(name, ctx);

Sets a custom Context.

These getters are available for accessing context data:

mse.context.getAEP();
mse.context.getCategory();
mse.context.getContext(name);
mse.context.getCustomUrl();
mse.context.getEventForwarding();
mse.context.getMagentoExtension();
mse.context.getDataServicesExtension();
mse.context.getOrder();
mse.context.getPage();
mse.context.getProduct();
mse.context.getRecommendations();
mse.context.getReferrerUrl();
mse.context.getProduct();
mse.context.getRecommendations();
mse.context.getRecommendationsExtension();
mse.context.getSearchExtension();
mse.context.getSearchInput();
mse.context.getSearchResults();
mse.context.getShopper();
mse.context.getShoppingCart();

Publish

These functions publish events which notify all subscribers:

// requires shoppingCart ctx to be set
mse.publish.addToCart();
// requires shoppingCart ctx to be set
mse.publish.abandonCart();
mse.publish.createAccount(ctx);
mse.publish.customUrl(ctx);
mse.publish.editAccount(ctx);
mse.publish.initiateCheckout(ctx);
// requires shoppingCart ctx and productCtx to be set
mse.publish.instantPurchase(ctx);
mse.publish.pageActivitySummary(ctx);
mse.publish.pageView(ctx);
// requires shoppingCart ctx and orderContext to be set
mse.publish.placeOrder(ctx);
// requires shoppingCart ctx and productCtx to be set
mse.publish.productPageView(ctx);
// requires recommendationsContext to be set
mse.publish.recsItemAddToCartClick(unitId, productId, ctx);
// requires recommendationsContext to be set
mse.publish.recsItemClick(unitId, productId, ctx);
mse.publish.recsRequestSent(ctx);
mse.publish.recsResponseReceived(ctx);
// requires recommendationsContext to be set
mse.publish.recsUnitRender(unitId, ctx);
// requires recommendationsContext to be set
mse.publish.recsUnitView(unitId, ctx);
mse.publish.referrerUrl(ctx);
mse.publish.removeFromCart(ctx);
// requires searchResultsContext to be set
mse.publish.searchCategoryClick(searchUnitId, name, ctx);
// requires searchResultsContext to be set
mse.publish.searchProductClick(searchUnitId, sku, ctx);
// requires searchInputContext to be set
mse.publish.searchRequestSent(searchUnitId, ctx);
// requires searchResultsContext to be set
mse.publish.searchResponseReceived(searchUnitId, ctx);
// requires searchResultsContext to be set
mse.publish.searchResultsView(searchUnitId, ctx);
// requires searchResultsContext to be set
mse.publish.searchSuggestionClick(searchUnitId, suggestion, ctx);
// requires shoppingCartContext to be set
mse.publish.shoppingCartView(ctx);
mse.publish.signIn(ctx);
mse.publish.signOut(ctx);
mse.publish.updateCart(ctx);

Subscribe

These functions subscribe to events:

mse.subscribe.addToCart(handler, options);
mse.subscribe.abandonCart(handler, options);
mse.subscribe.createAccount(handler, options);
mse.subscribe.customUrl(handler, options);
mse.subscribe.editAccount(handler, options);
mse.subscribe.dataLayerChange(handler, options);
mse.subscribe.dataLayerEvent(handler, options);
mse.subscribe.initiateCheckout(handler, options);
mse.subscribe.instantPurchase(handler, options);
mse.subscribe.pageActivitySummary(handler, options);
mse.subscribe.pageView(handler, options);
mse.subscribe.placeOrder(handler, options);
mse.subscribe.productPageView(handler, options);
mse.subscribe.recsItemAddToCartClick(handler, options);
mse.subscribe.recsItemClick(handler, options);
mse.subscribe.recsRequestSent(handler, options);
mse.subscribe.recsResponseReceived(handler, options);
mse.subscribe.recsUnitRender(handler, options);
mse.subscribe.recsUnitView(handler, options);
mse.subscribe.referrerUrl(handler, options);
mse.subscribe.removeFromCart(handler, options);
mse.subscribe.searchCategoryClick(handler, options);
mse.subscribe.searchProductClick(handler, options);
mse.subscribe.searchRequestSent(handler, options);
mse.subscribe.searchResponseReceived(handler, options);
mse.subscribe.searchResultsView(handler, options);
mse.subscribe.searchSuggestionClick(handler, options);
mse.subscribe.shoppingCartView(handler, options);
mse.subscribe.signIn(handler, options);
mse.subscribe.signOut(handler, options);
mse.subscribe.updateCart(handler, options);

Unsubscribe

These functions unsubscribe from events:

mse.unsubscribe.addToCart(handler);
mse.unsubscribe.abandonCart(handler);
mse.unsubscribe.createAccount(handler);
mse.unsubscribe.customUrl(handler);
mse.unsubscribe.editAccount(handler);
mse.unsubscribe.dataLayerChange(handler);
mse.unsubscribe.dataLayerEvent(handler);
mse.unsubscribe.initiateCheckout(handler);
mse.unsubscribe.instantPurchase(handler);
mse.unsubscribe.pageActivitySummary(handler);
mse.unsubscribe.pageView(handler);
mse.unsubscribe.placeOrder(handler);
mse.unsubscribe.productPageView(handler);
mse.unsubscribe.recsItemAddToCartClick(handler);
mse.unsubscribe.recsItemClick(handler);
mse.unsubscribe.recsRequestSent(handler);
mse.unsubscribe.recsResponseReceived(handler);
mse.unsubscribe.recsUnitRender(handler);
mse.unsubscribe.recsUnitView(handler);
mse.unsubscribe.referrerUrl(handler);
mse.unsubscribe.removeFromCart(handler);
mse.unsubscribe.searchCategoryClick(handler);
mse.unsubscribe.searchProductClick(handler);
mse.unsubscribe.searchRequestSent(handler);
mse.unsubscribe.searchResponseReceived(handler);
mse.unsubscribe.searchResultsView(handler);
mse.unsubscribe.searchSuggestionClick(handler);
mse.unsubscribe.shoppingCartView(handler);
mse.unsubscribe.signIn(handler);
mse.unsubscribe.signOut(handler);
mse.unsubscribe.updateCart(handler);

Support

If you have any questions or encounter any issues, please reach out on GitHub.