npm.io
0.2.5 • Published 13h ago

@aftership/web-pixels

Licence
MIT
Version
0.2.5
Deps
3
Size
347 kB
Vulns
36
Weekly
0

What is AfterShip Pixel Web SDK

AfterShip Pixel is a powerful tool for marketing campaign optimization and analytics with a strong commitment to data science. The AfterShip Pixel Web SDK and API make it simple to integrate.

Getting started

Begin using AfterShip Pixel APIs with a few simple steps. Log in, get your API credentials, and start developing.

Get Pixel ID

Currently, Pixel ID is available only to whitelisted users. Contact us to obtain one.

Install

AfterShip provides two common integration methods in web development. Depending on how your store is deployed, you can choose one of them to quickly integrate AfterShip Pixel into your store.

Using npm or yarn or pnpm or bun (Recommend)
$ npm install @aftership/web-pixels --save

or

$ yarn add @aftership/web-pixels

Business Pixel integration skill

The Business Pixel Web and Node.js integration skill is maintained in AfterShip/skills and distributed there as part of the frontend-skills plugin of the aftership-skills marketplace. This repository no longer keeps a copy; use the skill from that repository when adding @aftership/business-pixels to a business frontend, Module Federation application, BFF, or Node.js service.

Business Pixel runtime diagnostics

Business Pixel Web and Node.js expose delivery-safe runtime metadata:

ASPixel.version;
ASPixel.getDiagnostics();
await ASPixel.flush();

getDiagnostics() reports initialization state and timing, duplicate Web SDK versions, and aggregate queue counts. It does not expose event payloads or identity values. Web events are snapshotted when they are enqueued, the Datacat/PostHog mirror is invoked once, and Pixel endpoint retries reuse the same event and $log_id with a bounded exponential retry policy.

Add the AfterShip Pixel snippet

Currently, AfterShip Pixel ID is only available to whitelisted users. Please contact us to obtain the AfterShip Pixel snippet

Report the First Event

Initialization

If you installed using Add the AfterShip Pixel snippet, you can skip this step as the snippet already calls the initialization API.

Before reporting the first event, we need to call ASPixel.init to complete the SDK initialization, allowing AfterShip Pixel to know the source of upcoming events.

If this step isn't correctly executed, subsequent events reported via Track won't reach the server.

ASPixel.init({
  pixelId:"<AfterShipPixelId>"
})
Report Events

The ASPixel.events contains a variety of semantic event methods that you can choose to bury events in the right scenarios. Each event method has an explicit typescript type.

SDK will send the request after calling ASPixel.events. Before calling this API, please make sure that your website is compliant with privacy regulations (e.g. GDPR, CCPA, etc.)

// Track search terms
ASPixel.events.clicked({
	linkUrl: "test.com",
	label: "test",
});

// Track a simpler version.
ASPixel.events.pageViewed();

Identify Users

After initializing ASPixel, you can call ASPixel.identify() to identify individual users. This method accepts customerId, and all subsequent event reports will include this customerId.

// Track search terms
// Add a customer ID
ASPixel.identify({
	customerId: 'test customerId',
});

After identifying the user, it is also possible to resetIdentity the user by calling ASPixel.resetIdentiy(). After calling this,all events will not carry the user id.

// Track a simpler version.
// Add a user ID
ASPixel.track('$page_viewed', {
	customer_id: "e4b0f8a6c9d64a2b8e2f0c67a8d4f9e2"
});

Make a test API request

Enabling this won't report events but will print logs in the console.

Enable Debug Mode

Method: Call window.ASPixel.debug(true) in the console.

Disable Debug Mode

Method: Call window.ASPixel.debug(false) in the console.

API Basic

  1. init
  • Description: To initialize the SDK, you need to provide a pixelId to identify the store. This process is asynchronous, so be sure to handle it accordingly.
  • Parameter:
await ASPixel.init({
  pixelId:"<AfterShipPixelId>"
})
  1. events
  • Description: For the event reporting method, provide the event name along with the corresponding parameters to report the event.
  • Parameter:
ASPixel.events.pageViewed()
  1. debug
  • Description: To debug data reporting, you can call the debug method under the window.ASPixel in the console to enable the debugging environment. Once enabled, events will not call the API; they will only output the event calls in the console.
  • Parameter:
// fisrt time is open
window.ASPixel.debug()
// second time is close
window.ASPixel.debug()

Keywords