# Analytics

**URL:** https://heroiclabs.com/docs/hiro/unity/analytics/
**Summary:** Analytics are a way to track the performance of your game.
**Keywords:** analytics, hiro
**Categories:** hiro, unity, analytics

---


# Analytics

Hiro provides a way to easily incorporate analytics and track the performance of your game. You can create custom analytics systems to send events to third-party services, and multiplex these analytics events to one or more analytics systems. 

Hiro comes with built-in analytics systems for Firebase, Adjust, AppsFlyer, and [Satori](../../../satori/).

## Initializing the analytics system

The analytics system can be used to push events to multiple analytics systems which must be passed in as a dependency via the constructor, as well as an `ILogger` instance. These analytics systems must implement the `IAnalyticsSystem` interface. Hiro comes pre-packaged with systems for Satori, Firebase, Adjust and AppsFlyer.

```csharp
IList<IAnalyticsSystem> supportedAnalyticsSystems;

// Initialize individual analytics systems here e.g. 
// var satoriSystem = new SatoriSystem();
// var firebaseSystem = new FirebaseSystem();
// supportedAnalyticsSystems.Add(satoriSystem);
// supportedAnalyticsSystems.Add(firebaseSystem);

var analyticsSystem = new AnalyticsSystem(supportedAnalyticsSystems, logger);
systems.Add(analyticsSystem);
```

## Satori

The Satori analytics system pushes events into the [Satori Live Ops Server](https://www.heroiclabs.com/satori) and must be initialized with the connection credentials for the server as well as relies on the `IUserSystem`.

```csharp
var scheme = "https";
var host = "<hostAddress>";
var port = 7450;
var apiKey = "<apiKey>";
var enabled = true;

var satoriSystem = new SatoriSystem(logger, scheme, host, port, apiKey, userSystem, enabled);
supportedAnalyticsSystems.Add(satoriSystem);
```

## Firebase

The Firebase analytics system pushes events into a configured Firebase instance. You must provide a [Firebase configuration JSON file](https://support.google.com/firebase/answer/7015592?hl=en).

```csharp
var timeBetweenRemoteConfigFetches = TimeSpan.FromHours(12);
var firebaseSystem = new FirebaseSystem(logger, timeBetweenRemoteConfigFetches);
supportedAnalyticsSystems.Add(firebaseSystem);
```

## Adjust

The Adjust analytics system pushes events into a configured Adjust instance.

```csharp
AdjustEnvironment environment;
var adjustSystem = new AdjustSystem(userSystem, logger, "<appToken>", environment)
supportedAnalyticsSystems.Add(adjustSystem);
```

## AppsFlyer

The AppsFlyer analytics system pushes events into a configured AppsFlyer instance.

```csharp
var appsflyerSystem = new AppsflyerSystem();
appsflyerSystem.Setup("<developmentKey>", "<appId>");
supportedAnalyticsSystems.Add(appsflyerSystem);
```

## Event taxonomy

Hiro also provides a default taxonomy of events to standardize what should be sent by most games.

| Event | Description |
| --- | --- |
| `Install` | When players install the game for the first time. |
| `AppLaunch` | When the players log in to the game. This event can be triggered when the game is opened, switched to foreground, or when a new session on the server is established. |
| `AppClosed` | When the players close the game. |
| `SignUp` | When players use a social account method or sign-up by a custom (such as e-mail) method provided within the game. |
| `RewardedVideoShown` | When a Rewarded Video ad is displayed to the players. |
| `BannerShown` | When a Banner is successfully displayed to the players. |
| `InAppPurchase` | When a player successfully completes and in-app purchase. |
| `PurchaseInitiated` | When the purchase was initiated by the user. |
| `PurchaseAccepted`| When the user completes the payment from the store provider. This event should be sent when the purchase is approved by the store but not yet validated on the server.|
| `PurchaseCanceled` | When the user cancels a purchase. |
| `PurchaseFailed` | When the purchase failed for the user. A cause included as part of a purchase failure is if the receipt failed validation.
| `LevelStart` | When a player starts a new game or level. |
| `LevelComplete` | When a player successfully completes a level. |
| `LevelFail` | When a player fails to complete a level. |
| `LevelRestart` | When a player manually restarts a level. The `LevelStart` event is always sent along with this event. |
| `LevelAbandon` | When a player manually quits a level or the game. |
| `PowerUpUsed` | When a player uses a power-up. |
| `TutorialBegin` | When players begin the FTUE. Games can have multiple tutorials, to differentiate different tutorials and `id` parameter should be used. |
| `TutorialStepCompleted` | When players complete a step in a tutorial. |
| `TutorialEnd` | When players complete the FTUE. |
| `InterstitialShown`| When an interstitial Ad is displayed to a user. |
| `ScreenViewed` | When a player views a particular screen. |
| `SdkInitialization` | When one of the SDKs used within the game has been initialized successfully or failed to initialize. |
| `SetUser` | Set the user across all analytics systems. |
| `SetUserProperty` | Set analytics user property to describe a player. This helps to analyze behaviors of various player segments by applying these properties as filters to your reports. |
| `GameplayDuration` | When the gameplay duration is reached to one of the following levels (ie. 5, 10, 15, 30, 45, 60 minutes). The duration should be calculated as the player's total time consumed in the game. Session can vary game to game but the general definition is each time a new session id is generated between the server and client. |

These events can be logged directly by calling the associated method on the `AnalyticsSystem`. For example:

```csharp
analyticsSystem.Install();
analyticsSystem.GameStart("<gameId>", "<gameType>");
```

You can also specify exactly which analytics providers you wish to submit the event to. If you omit this then the event will be sent to all.

```csharp
analyticsSystem.TutorialBegin("<tutorialId>", new List<IAnalyticsSystem> { satoriSystem });
```

### Custom events

You can also send custom events to the analytics systems. The following is an example of a custom event:

```csharp
var properties = new Dictionary<string, object>
{
    { "deviceId", SystemInfo.deviceUniqueIdentifier },
    { "deviceType", SystemInfo.deviceType },
    { "deviceOS", SystemInfo.operatingSystem },
    { "deviceModel", SystemInfo.deviceModel }
};

analyticsSystem.LogEvent("deviceInfo", properties);
```
