# ViKPI for Unity

Session replay, touch heatmaps, funnels, retention, performance and error tracking for Unity games.

Supports Unity 2021.3 and newer on Android, iOS, WebGL, Windows, macOS and Linux. No third-party
dependencies.

## Install

Pick one:

- **.unitypackage**: download [vikpi-unity.unitypackage](https://vikpi.com/downloads/vikpi-unity.unitypackage)
  and open it in Unity, or use *Assets → Import Package → Custom Package*.
- **Package Manager (git)**: *Window → Package Manager → + → Add package from git URL*:

  ```
  https://github.com/<your-org>/vikpi.git?path=sdk/unity/VikpiSdkProject/Assets/ViKPI
  ```

## Set up

1. Create a project at [vikpi.com](https://vikpi.com) and copy its key (`vk_pub_…`).
2. In Unity, open **Project Settings → ViKPI** (or *Tools → ViKPI → Settings*) and paste the key.
   Self-hosting? Set **Endpoint** to your server's origin as well.
3. Under **Privacy**, enter your game's **Privacy Policy URL** (https). It is required: builds fail
   without it, because Google Play and the App Store reject apps that don't disclose analytics and
   session recording. See [what to declare on the stores](https://vikpi.com/legal/sdk).
4. Build and run. On first launch players see a short consent prompt; once they tap **Allow**,
   sessions appear in the dashboard a few seconds later.

Play Mode in the Editor sends nothing unless you turn on **Run In Editor**, so your own testing
doesn't mix with players' data.

## What is collected automatically

| Data | Details |
| --- | --- |
| Sessions | Start, end, duration, device model, OS, app version, screen size, locale |
| Video | Low-frame-rate replay of the screen; sample rate, fps and quality are set in the dashboard |
| Touches | Down, move and up, normalized to the screen; the mouse on desktop |
| Scenes | `$scene` whenever the active scene changes |
| Performance | Each second: FPS, 95th percentile frame time, allocated memory, battery |
| Errors | Exceptions, `Debug.LogError` and failed asserts, with stack traces, from any thread |

## Custom events

```csharp
using System.Collections.Generic;
using ViKPI;

Vikpi.Track("level_complete", new Dictionary<string, object>
{
    { "level", 3 },
    { "stars", 2 },
    { "used_booster", true },
});
```

Names may use letters, digits, spaces and `_ . : -`, up to 64 characters. Up to 32 properties per
event; values are strings, numbers or booleans. Use the same events to build funnels in the dashboard.

## Players

```csharp
Vikpi.SetUserId(account.Id); // persists across launches; pass null on sign-out
```

## Privacy and app store compliance

Nothing is collected, and no id is created, before the player agrees.

| Setting | What it does |
| --- | --- |
| **Privacy Policy URL** | Required (https). Shown in the consent prompt; `Vikpi.OpenPrivacyPolicy()` opens it. Builds fail without it. |
| **Consent Mode** | *Built-in prompt* (default): a short dialog on first launch, Allow and Don't allow equally prominent; texts are editable. *Manual*: your dialog or consent platform calls `Vikpi.SetConsent(bool)`. *Not required*: collects without asking (build warning). |
| **App Targets Children** | For Google Play Families, Apple Kids category, COPPA: the SDK collects nothing at all. |
| **Mask Text Inputs** | On by default: every `InputField` and `TMP_InputField` is blacked out in videos on the device. |

Add a privacy section to your settings menu:

```csharp
Vikpi.ShowConsentPrompt();                 // let players change their choice
Vikpi.OpenPrivacyPolicy();
Vikpi.RequestDataDeletion(ok => { ... }); // erase everything recorded from this install
```

- Refusing or withdrawing consent stops the SDK at once and erases data not yet sent.
- Add the **ViKPI / Recording Mask** component to anything else that shows personal data (profiles,
  chat, payment screens). It is painted over **on the device**, before encoding.
- `Vikpi.PauseRecording()` / `Vikpi.ResumeRecording()` stop video around whole screens.
- The device id is a random install id: no advertising id (GAID/IDFA), IDFV or Android ID, so no App
  Tracking Transparency prompt is needed.
- **iOS privacy manifest**: `Plugins/iOS/PrivacyInfo.xcprivacy` declares the collected data types and
  the required-reason APIs (UserDefaults CA92.1, system boot time 35F9.1, file timestamps C617.1).
  After an iOS build the SDK checks it reached the Xcode project and warns if you need to merge it.
- Google Play Data safety and App Store privacy answers: <https://vikpi.com/legal/sdk>.

## How it behaves on the device

- **Cheap on the main thread.** Frames are copied and downscaled on the GPU and read back
  asynchronously; JPEG encoding runs on a background thread. Where async readback isn't available
  (WebGL), recording drops to 4 fps.
- **Offline first.** Every request is written to disk before it is sent and retried with backoff,
  so nothing is lost to a tunnel or a killed app. Storage is capped (50 MB by default); video is
  dropped before analytics when the cap is reached.
- **Wi-Fi only video** can be switched on per project in the dashboard; analytics still upload on
  cellular.
- **Sessions** end when the app quits or stays in the background longer than the session timeout
  (5 minutes by default). A session killed in the background is closed on the next launch.

## API

| Member | Purpose |
| --- | --- |
| `Vikpi.Init(apiKey = null, endpoint = null)` | Start manually when Auto Start is off |
| `Vikpi.Track(name, properties = null)` | Record a custom event |
| `Vikpi.SetUserId(id)` | Tie sessions to your player id |
| `Vikpi.PauseRecording()` / `ResumeRecording()` | Stop or resume video |
| `Vikpi.Flush()` | Upload buffered data now |
| `Vikpi.Shutdown()` | End the session and stop the SDK |
| `Vikpi.SetConsent(granted)`, `ConsentStatus` | Record or read the player's choice |
| `Vikpi.ShowConsentPrompt()` | Show the built-in consent prompt again |
| `Vikpi.OpenPrivacyPolicy()` | Open the privacy policy URL |
| `Vikpi.RequestDataDeletion(onComplete)` | Erase the player's data on the server and locally |
| `Vikpi.SessionId`, `IsRunning`, `IsRecording` | Current state |

## Troubleshooting

Turn on **Debug Logging** in the settings for a line per upload and retry in the console.

- *Build failed: "Set your game's privacy policy URL"*: fill in Project Settings → ViKPI → Privacy.
- Nothing recorded: the player hasn't tapped **Allow** yet (`Vikpi.ConsentStatus`), or **App Targets
  Children** is on.
- *"No valid API key"*: paste the key under Project Settings → ViKPI.
- *"The server rejected the API key"*: the key was revoked or belongs to another server.
- No video: check the project's sample rate in the dashboard, and the monthly recording quota.
