Skip to content
HyperKit

Custom analytics events

Analytics events are essential for tracking and understanding user behavior in your application. This guide will walk you through the process of creating and implementing custom analytics events using our analytics service.

One event can go to Firebase, to Hypercell Analytics and to Adjust, and can also be logged to the Unity Console while you develop. You choose the destinations per event, as described in Where your event goes below.

Here’s how to create your own custom events:

  1. Create event data object that extends IAnalyticsEvent.
    Example TestEvent:

    using Services.Analytics;
    namespace Samples.Analytics
    {
    public class TestEvent : IAnalyticsEvent
    {
    public string Payload;
    public TestEvent(string payload)
    {
    Payload = payload;
    }
    }
    }
  2. Implement IAnalyticsEventHandler with the specific analytics provider interface.

    Example TestEventHandler:

    using System;
    using Services.Analytics;
    using Services.Analytics.Handlers;
    using Services.Analytics.Models;
    using UnityEngine;
    namespace Samples.Analytics
    {
    /// <summary>
    /// Example of event handler for test event
    /// </summary>
    public class TestEventHandler : IAnalyticsEventHandler,
    IFirebaseEventHandler<TestEvent>,
    IDebugEventHandler<TestEvent>
    {
    /// <summary>
    /// Event type to handle event
    /// </summary>
    public Type EventType => typeof(TestEvent);
    /// <summary>
    /// Example firebase implementation of sending event
    /// </summary>
    /// <param name="e"> Instance of event </param>
    /// <param name="analyticsContext"> Analytics context </param>
    /// <param name="extender"> Firebase extender for adding extra parameters out of box </param>
    public void TrackFirebaseEvent(TestEvent e, AnalyticsContext analyticsContext, IAnalyticsEvent.IExtender extender)
    {
    FirebaseEventBuilder.Handle("test_event", extender)
    .AddParameter("payload", e.Payload)
    .WithGameContext(analyticsContext.GameContext)
    .LogEvent();
    }
    /// <summary>
    /// Used to debug events, usually just logs in console
    /// </summary>
    /// <param name="e"></param>
    /// <param name="analyticsContext"></param>
    public void TrackDebugEvent(TestEvent e, AnalyticsContext analyticsContext)
    {
    Debug.Log($"Test event with payload {e.Payload}");
    }
    }
    }
  3. Register your handler.

    Example code:

    // Preferrably do this right after HyperKit is initialized
    HyperKit.Analytics.RegisterEventHandler(new TestEventHandler());
  4. Track event occurrence when necessary.

    Example code:

    private void OnTestButtonClick()
    {
    HyperKit.Analytics.TrackEvent(new TestEvent("Test payload"));
    }

Your event class is only a marker. The destination is decided by the interfaces your handler implements, one interface per destination:

  • IFirebaseEventHandler<T>: Firebase Analytics.
  • IGamestotyEventHandler<T>: Hypercell Analytics. Inside it, call Gamestoty.SendEvent(eventName, level, stage, context), or the two argument overload Gamestoty.SendEvent(eventName, context), which reuses the level and stage the SDK already tracks.
  • IAdjustEventHandler<T>: Adjust.
  • IDebugEventHandler<T>: Unity Console.

A destination whose interface you do not implement receives nothing, and nothing is logged about it. The handler in step 2 implements Firebase and debug only, so that event never reaches Hypercell Analytics.

Two destinations also depend on the underlying SDK being ready:

  • Adjust tracking is compiled out in the Unity Editor, so an IAdjustEventHandler<T> has no effect there at all. Test it on a build.
  • Gamestoty discards events sent before it has finished initializing.

TrackDebugEvent runs only on a debug device: always in the Unity Editor, and on a real build only when the device is flagged as debug. Not seeing the log does not mean the event was not sent.

RegisterEventHandler throws if a handler is already registered for the same event type, so register each handler once.