Sample: Analytics
PlayUR has a simple yet powerful analytics system, to allow you to record in-game events (Actions), with optional meta-data. The final part of the Simple Example gives a good guide on how to implement analytics actions.
Basic Usage
The simplest way to record analytics is to record a single Analytics Action. This only requires a on-demand call to record the action, and will automatically record the action name, and the timestamp of when it occurred. The following sample records an action on a button click:
public class AnalyticsBasic : PlayURBehaviour
{
/// The analytics action to trigger when the user clicks
public Action clickAction;
//to be linked up to a button via Unity Event System
public void OnButtonClick()
{
PlayURPlugin.instance.RecordAction(clickAction);
}
}
Advanced Usage (Analytics Columns Meta-data)
You might want to store some additional information about a given action (i.e. meta-data). To do this, you need to create an InProgressAction
object using BeginRecordAction
, and call the AddColumn
function to add meta data. You can attach as many columns to an action as you like. You do not need to add information for all defined columns, these will just be blank in the analytics table by default.
In this sample, we add meta-data to record the relative time since level load as metadata:
using PlayUR;
/// <summary>
/// Example code showing how to record <see cref="Action"/>s with and without extra meta data.
/// </summary>
public class AnalyticsCustom : PlayURBehaviour
{
/// <summary>
/// The action to trigger when a collision happens
/// Note, no default value set as <see cref="Action"/> enum values may become invalid.
/// </summary>
public Action bounceAction;
/// <summary>
/// The anayltics column to store metadata (time) in.
/// Note, no default value set as <see cref="Action"/> enum values may become invalid.
/// </summary>
public AnalyticsColumn column;
public void OnCollisionEnter(Collision collision)
{
//set up the action to record but don't send it yet
var a = PlayURPlugin.instance.BeginRecordAction(bounceAction);
//add meta data to the action (this can happen immediately, or at any time as long as you have the reference to the action
a.AddColumn(column, Time.time);
//when ready, actually queue it up to record
a.Record();
}
}
Extra-Advanced Usage: Updatable Actions
PlayUR provides functionality for where an Action's meta-data might be updated, appended, or incremented frequently. Rather than uploading multiple action instances, it is recommended to use one of the following classes, which will periodically record their updates to the server:
PlayURPlugin.UpdatableAction<T>
In this sample, the last known position of the game object is consistently stored in the positionColumn
. Only one Action is stored, and it contains a single value of the last transform.position
of the object in the Analytics Column. This type of action can store data of any type that can be converted into a `String``.
public Action moveAction;
public AnalyticsColumn positionColumn;
PlayURPlugin.UpdatableAction<Vector3> action;
void Start()
{
action = new PlayURPlugin.UpdatableAction<Vector3>(clickAction);
}
void Update()
{
action.Set(positionColumn, transform.position);
}
PlayURPlugin.AppendableAction
In this sample, a list of positions is recorded every frame. Only one Action is stored, and it contains a list of values of the transform.position
of the object in the Analytics Column. This action type can only store String
s.
public Action movementAction;
public AnalyticsColumn positionsColumn;
PlayURPlugin.AppendableAction action;
void Start()
{
action = new PlayURPlugin.AppendableAction<Vector3>(movementAction, positionsColumn);
}
void Update()
{
action.Append(transform.position);
}
PlayURPlugin.CountAction
In this sample, a number counter keeps a track of the number of frames (i.e. the number is incremented each frame). Only one Action is stored, and it contains the final count for the int
value in the Analytics Column. This action type can only store int
s.
public Action framesAction;
public AnalyticsColumn frameCountColumn;
PlayURPlugin.CountAction action;
void Start()
{
action = new PlayURPlugin.CountAction<Vector3>(framesAction, frameCountColumn);
}
void Update()
{
action.Increment();
}
Unity Sample Requirements
You can import this sample from the Unity Package Manager. For the code to run, you will need a game set up in Unity (see Getting Started), and the following configured for the game on the PlayUR Dashboard:
- At least one Analytics Action (you will then select the action on the component for any of the scripts)
- At least one Analytics Column (you will then select the column on the component for any of the scripts)