Custom API

Introduction

We may not yet support your platform for getting data into Fanplayr 360, so we offer a powerful API to work directly with our system to add data.

You can use the Custom API to send data from a server, or a mobile app. The options a limitless.

The JavaScript API uses exactly the same calls under the hood.

Sending Messages

Once you have created a Custom Integration before starting, to get a WRITE_KEY and ENDPOINT which you can then use to send data to Fanplayr 360.

Calling the API is as simple as making an HTTPS request with the following details:

  • URL: https://<ENDPOINT>/v1/collect

  • Method: POST

  • Headers

    • content-type: application/json

    • x-api-key: <WRITE_KEY>

  • Body: A JSON string containing an array of Messages.

Example Request

curl 'https://<ENDPOINT>/v1/collect' \
  -H 'x-api-key: <WRITE_KEY>' \
  -H 'content-type: application/json' \
  --data-raw '[<JSON_MESSAGE>, <JSON_MESSAGE>]'

The API can only handle messages of ~250kb, so we calling the API with no more than 20 Messages in one call.

Message Structure

Each message you send to Fanplayr 360 needs to contain a basic set of data.

Basic Message

interface Message {
  messageId: string
  writeKey: string
  timestamp: string
  type: 'track' | 'identify' | 'userDataOps', 'consent' | 'page'
  userId: string
  sessionId: string
  pageId: string
  context?: Context,
  properties?: Properties,
  identities?: Identities,
  operations?: Operations
}
  • messageId - Every message you send must include a unique identifier. We suggest using a UUID, nanoID or ulid.

  • writeKey - The WRITE_KEY that is sent through in the x-api-key must also be supplied here.

  • timestamp - The time the event happened in ISO-8601 format and UTC time zone ie "2023-01-23T15:34:48Z".

  • type - The type of the message.

  • userId - A unique identifier for this user. If the same userId is supplied in a different session the messages will be treated as if they were from the same user. This is separate from managing identify using identify calls.

  • sessionId - A unique identifier for a session. See Handling Sessions for more details.

  • pageId - A unique identifier for the current page / screen view. This is used to tie events together. See Tracking Page Views.

The basic message structure may or may not include context, properties, identities and operations depending on the type being sent.

  • context - can be used in any message type

  • properties - should only be used in track or page type

  • identities - should only be used in identify type

  • operations - should only be used in userDataOps and consent type

Context

Context is used to track information about the page, or screen, the user is on, and their IP address for location, and userAgent to track browser or device type.

interface Context {
  page: {
    url: string
    referrer: string
    type: string
    title: string
  },
  ipAddress: string
  userAgent: string
}
  • page

    • url - the URL of the page, if tracking on a website.

    • referrer - the URL of the referring page, if tracking on a website.

    • type - a string representing the page. Any value can be used here, but some values will add functionality - see JavaScript API / Track Pages. Can also be used to represent "screens" in a mobile app.

    • title - the page title of the website or app screen.

  • ipAddress - the IP address of the users device, or the server / device that this message was sent from. It is used to generate extra attributes such as $country .

  • userAgent - the User Agent string from a browser, or defined for a device type such as a mobile phone. It is used to generate extra attributes such as $device .

Properties

Properties are any key / value pair, or Semantic Object, which you want to track along with the event. This should only be used with track or page type Messages.

interface Properties {
  [key:string]: string | number | boolean | SemanticObject
}

The key for a property can be any string value. It is a suggestion to not start the value with a "$" as this may override generated attributes. It may also be good practice to use either camel-case keys such as "myKeyName", and stay away from non-ASCII characters as some systems you export data to may otherwise have problems.

Although you can send in string , number or boolean types all data is stored as strings in Fanplayr 360.

Identities

Identities are key / value pairs that let the system know how to identify and merge users and sessions. This should only be provided for the message type identify.

type Identities = {
  [key:string]: string
}

Operations

Operations should only be used with the userDataOps and consent type messages. The properties supported are different for each.

userDataOps

interface Operation {
  type: 'set' | 'setOnce' | 'unset' | 'increment'
  key: string
  value: string | number | boolean
  expiresOn?: number,
}

type Operations = Array<Operation>
interface Operation {
  type: 'set' 'unset'
  key: string
  value: string
  purpose?: Array<{
    type: string
    topics:? Array<string>
  }>
}

type Operations = Array<Operation>

What next?

Read on to understand how to use each message type, along with examples.

Last updated