Snowplow JavaScript Tracker

website·Indexed 29 days ago

https://snowplow.github.io/snowplow-javascript-tracker

Documentation for Snowplow tracking libraries, including @snowplow/node-tracker and @snowplow/react-native-tracker. Covers API references for PayloadBuilder, Emitter, EventStore, and various event interfaces such as PageViewEvent and EcommerceTransactionItemEvent.

Tokens
92.2K
Snippets
923
Records
1.1K
Agent score
70%

What's inside Snowplow JavaScript Tracker

  1. Overview of react-native-tracker Interfaces

    The react-native-tracker package provides several interfaces to manage event tracking, configuration, and data persistence in React Native applications. Key interfaces include:

    • Configuration: CoreConfiguration (core library settings), AppLifecycleConfiguration (app lifecycle tracking), DeepLinkConfiguration (deep link tracking), SessionConfiguration (session tracking), and EventStoreConfiguration (event store settings).
    • Event Handling: PayloadBuilder (mutable object for tracker payloads), EventPayloadAndContext (returns a built event and context), and PageViewEvent (represents page views or virtual page views in SPAs).
    • Infrastructure: Emitter (manages the event queue and batching to the collector), EventStoreIterator (iterates over stored events), and CorePlugin (defines core plugins).
    • Context & Data: PlatformContextRetriever (overrides platform context properties), DeviceTimestamp (representation of a device timestamp), and RuleSet (matches Iglu schema URIs via accept/reject properties).
  2. Use TrackerCore to manage persistent payload pairs and track events

    The TrackerCore interface provides the fundamental mechanisms for adding persistent data to all events and triggering the final payload delivery in the Snowplow React Native tracker.
    interface TrackerCore {
      // Set a persistent key-value pair to be added to every payload
      addPayloadPair: (key: string, value: unknown) => void;
    
      // Finalizes the payload and sends it. 
      // Adds context and payloadPairs name-value pairs to the payload.
      track: (pb: PayloadBuilder, context?: Array<SelfDescribingJson> | null, timestamp?: Timestamp | null) => Payload;
    }
  3. Use the LinkClickEvent interface to track link clicks

    The LinkClickEvent interface is used to represent an event triggered when a user clicks a link on a webpage, typically an anchor (<a>) tag. It captures metadata about the clicked element and the destination URL.
    interface LinkClickEvent {
      targetUrl: string;
      elementClasses?: string[];
      elementContent?: string;
      elementId?: string;
      elementTarget?: string;
    }
  4. Use the SessionState interface for session tracking

    The SessionState interface represents the current session state tracked in events within the @snowplow/react-native-tracker. It contains identifiers for the session and user, as well as metadata about the session's sequence and storage.
    export interface SessionState {
      sessionId: string;             // An identifier for the session
      sessionIndex: number;         // The index of the current session for this user
      userId: string;               // An identifier for the user of the session
      storageMechanism: string;     // The mechanism used to store session information on the device
      eventIndex?: number;           // (Optional) Index of the current event in the session
      firstEventId?: string;        // (Optional) Identifier of the first event for this session
      firstEventTimestamp?: string;  // (Optional) Date-time timestamp of the first event in the session
      previousSessionId?: string;   // (Optional) The previous session identifier for this user
    }
  5. Understand the RequestFailure type for @snowplow/node-tracker

    The RequestFailure type defines the structure of the data available to the onRequestFailure callback when a request fails in the Snowplow Node tracker. It provides details about the failed events, the HTTP status code, an error message, and whether the tracker intends to retry the request.
    type RequestFailure = {
        events: EventBatch;
        status?: number;
        message?: string;
        willRetry: boolean;
    };
  6. Use the ContextEvent interface in Snowplow callbacks

    The ContextEvent interface is used as the argument for ContextGenerator and ContextFilter callbacks. It provides metadata about the event currently being processed, allowing developers to conditionally add or filter context based on the event's payload, schema, or type.
    interface ContextEvent {
      event: Payload;      // The event payload
      eventSchema: string; // The event schema where one is available, or empty string
      eventType: string;   // The event type
    }
  7. Handle request failures using the RequestFailure type

    The RequestFailure type defines the structure of the data passed to the onRequestFailure callback in the @snowplow/react-native-tracker. It provides details about the failed event batch, the HTTP status code, the error message, and whether the tracker will attempt to retry the request.
    type RequestFailure = {
        events: EventBatch;
        status?: number;
        message?: string;
        willRetry: boolean;
    };