Open the script in Visual Studio. Copy and paste this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sandstorm.Analytics.Client;
public class Analytics : MonoBehaviour
{
void Start()
{
// Replace <<ApiKey Goes Here>> with the API Key you copied in Step 2.
// Replace test@test.com with the Id you want to assign to the user or
// let Sandstorm SDK to generate a random Id.
var client = SandstormClientBuilder.Build().WithApiKey("<<ApiKey Goes Here>>").WithIdentity("test@test.com").Get();
client.Track(SandstormEvent.New("Start Session").With("Message", "This event comes from Unity"));
Debug.Log("Start Session");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var client = SandstormClientBuilder.GetClient();
// Assign test2@test.com id to this user.
// From now on, events assigned to identity test2@test.com
// will be assigned to this same user.
// This cannot be undone.
client.Alias("test2@test.com");
client.Track(SandstormEvent.New("Key Pressed").With("Key", "Space"));
Debug.Log("Key Pressed");
}
}
private void OnDisable()
{
var client = SandstormClientBuilder.GetClient();
client.Track(SandstormEvent.New("End Session"));
}
}
Step 4
In your Unity project, attach the script to any component.
Try it!
Play the scene.
In Sandstorm app, go to Settings -> Live
Your events should appear in the live view.
More options
Go to API Specifications to see all operations available.