Skip to Main Content
Player Logo
PlayerPlugins

Beacon Plugin

The beacon plugin enables users to send and/or collect beaconing information from assets in a normalized API. It exposes a common API for publishing beacons from an asset library, and will automatically attach itself to the current view, enabling additional meta-data to be added to each event.

Consuming Beacons

Beacon Format

By default, the beacon plugin returns beacons in the following format:
interface DefaultBeacon {
  /** The user action taken ('clicked', 'visited') **/
  action: string; 

  /** The type of UI element interacted with ('button', 'menu') **/
  element: string;

  /** Any additional data from a `metaData.beacon` property **/
  data: any;

  /** The id of the asset **/
  assetId: string;

  /** The id of the view **/
  viewId: string;
}

Usage

Add the beacon plugin to a player:
import { Player } from '@player-ui/player';
import { BeaconPlugin } from '@player-ui/beacon-plugin';

const player = new Player({
  plugins: [
    new BeaconPlugin({

      // Any plugins to the beacon-plugin
      plugins: [],

      // Callback to handle any beacon event
      callback: () => {}
    }),
  ],
});
Beacons can be published directly by the plugin, but in most cases, a platform specific adapter is recommended.
beaconPlugin.beacon({
  action: 'click',
  element: 'button',
  asset: asset // The entire Asset object, for use in the plugin pipeline
  // other metadata
});

Beacon Plugins

Similar to how Player accepts plugins, the beacon-plugin itself accepts a list of plugins. These are able to mutate and augment the beacon payload as it makes its way through the publishing pipeline.
There are 3 hooks that are currently exposed:
  • buildBeacon - Assembles a given beacon ffor publishing
  • cancelBeacon - Given a current beacon, determine if it should be published or not.
  • publishBeacon - Receive the final becaon. This is a substitute for the callback in the beacon plugin options.

Publishing Beacons

beacon expression

The beacon plugin adds support for a beacon expression that can be referenced within content. Each beacon referenced from this expression will be assumed to originate from a view (no local asset information will be attached)
{
  "asset": {
    "id": "my-action",
    "type": "action",
    "exp": "beacon('my-action', 'some-data')"
  }
}

Assets

useBeacon hook

The @player-ui/beacon-plugin-react package exports a hook that assets can leverage to publish beacons.
The useBeacon hook takes base options that apply broadly, and returns a function with those options as the base. You can then pass event specific information when calling beacon.
Example
import { useBeacon } from '@player-ui/beacon-plugin-react';

// inside of your asset
export const Component = (props) => {
  const beacon = useBeacon({ action: 'clicked', asset: props });

  return (
    <button
      onClick={() =>
        beacon({
          element: 'button',
          data: {
            custom: 'fields',
          },
        })
      }
    >
      Click Me
    </button>
  );
};