Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ To start tracking, you need to know your `projectToken`. To initialize the track
var infinario = Infinario.Infinario.GetInstance();
infinario.Initialize("projectToken");

//or if you want to track app version as well
infinario.Initilize("projectToken", "1.0.0");
//or if you want to track app version as well, third parameter is instance
infinario.Initilize("projectToken", "1.0.0", "https://api.infinario.com");
```

Now you can track events by calling the ```Track``` method:
Expand Down Expand Up @@ -110,7 +110,36 @@ infinario.Track("my_player_action", <long_your_tsp>);
infinario.Track("my_player_action", <properties> , <long_your_tsp>);
```


### Get Segment for player
To obtain player's segment information form segmentations you can use method GetCurrentSegment.
You have to specify projectSecret (this is different than projectToken - you can find it in project overview).
The second parameter is segmentationId (obtained from last part of url, when creating or viewing segments).
The last one is your callback method with 3 parameters:
- `boolean` type specifing if retrieving of segment was successfull
- `InfinarioSegment` type with the our desired information about players segment
- `string` information about errors that occured
```
infinario.GetCurrentSegment(projectSecret, segmentationId, OnSegmentReceiveCallback);

private void OnSegmentReceiveCallback(bool success, InfinarioSegment infinarioSegment, string error)
{
if (success)
{
Debug.Log(infinarioSegment);
// Do stuff according to segment type
}
else
{
Debug.LogError(error);
}
}
```
InfinarioSegment class object returned in OnSegmentReceiveCallback contains public methods:
- `GetName()` returning string name of the segment where player belongs
- `GetSegmentationName()` returning string name of segmentation
- `GetSegmentIndex()` returns integer identifying the order of current segment in segmentation (starting with 0)


### Offline Behavior

Once instantized, the SDK collects and sends all tracked events continuously to the Infinario servers.
Expand All @@ -119,4 +148,4 @@ However, if your application goes offline, the SDK guarantees you to re-send the

## Final Remarks
- Make sure you create at most one instance of ```Infinario``` during your application lifetime.
- If you wish to override some of the capabilities (e.g. session management), please note that we will not be able to give you any guarantees.
- If you wish to override some of the capabilities (e.g. session management), please note that we will not be able to give you any guarantees.
14 changes: 9 additions & 5 deletions source/Assets/Scripts/InfinarioSDK/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ internal class Constants
* SDK
*/
public static string SDK = "Unity SDK";
public static string VERSION = "2.0.2";
public static string VERSION = "2.0.3";

/**
* Tracking ids, events and properties
*/
public static string ID_REGISTERED = "registered";
public static string ID_COOKIE = "cookie";

public static string EVENT_SESSION_START = "session_start";
public static string ID_USER = "user_id";

public static string EVENT_SESSION_START = "session_start";
public static string EVENT_SESSION_END = "session_end";
public static string EVENT_IDENTIFICATION = "identification";
public static string EVENT_VIRTUAL_PAYMENT = "virtual_payment";
Expand Down Expand Up @@ -42,10 +43,13 @@ internal class Constants
public static long BULK_MAX = 60 * 20;

public static double SESSION_TIMEOUT = 60;

public static string DEFAULT_TARGET = "https://api.infinario.com";

public static string DEFAULT_SECRET = "X-Infinario-Secret";
public static string DEFAULT_TARGET = "https://api.infinario.com";
public static string BULK_URL = "/bulk";

public static string GET_SEGMENT_URL = "/analytics/segmentation-for";

public static string ENDPOINT_UPDATE = "crm/customers";
public static string ENDPOINT_TRACK = "crm/events";
}
Expand Down
8 changes: 3 additions & 5 deletions source/Assets/Scripts/InfinarioSDK/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ public static Dictionary<string, object> GetProperties()
private static string GetPlatform()
{
var platform = Application.platform;

var dict = new Dictionary<RuntimePlatform, string> {
{RuntimePlatform.Android, "Android"},
{RuntimePlatform.IPhonePlayer, "iOS"},
{RuntimePlatform.LinuxPlayer, "Linux"},
{RuntimePlatform.WSAPlayerARM, "Windows Phone"},
{RuntimePlatform.WSAPlayerX64, "Windows Store"},
{RuntimePlatform.WSAPlayerX86, "Windows Store"},
{RuntimePlatform.OSXDashboardPlayer, "Mac OS X"},
{RuntimePlatform.OSXEditor, "Mac OS X"},
{RuntimePlatform.OSXPlayer, "Mac OS X"},
{RuntimePlatform.OSXWebPlayer, "Mac OS X"},
{RuntimePlatform.OSXEditor, "Mac OS X"},
{RuntimePlatform.WindowsEditor, "Unity Editor"},
{RuntimePlatform.WindowsPlayer, "Windows Standalone"},
{RuntimePlatform.WP8Player, "Windows Phone"},
{RuntimePlatform.WindowsWebPlayer, "Web Player"}
{RuntimePlatform.WebGLPlayer, "Web Player"}
};
return (dict.ContainsKey (platform) ? dict [platform] : "Other");
}
Expand Down
10 changes: 8 additions & 2 deletions source/Assets/Scripts/InfinarioSDK/Infinario.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Infinario.Interface;
Expand Down Expand Up @@ -32,8 +33,8 @@ public void Initialize(string projectToken)
{
Initialize (projectToken, null, null);
}
public void Initialize(string projectToken, string appVersion)

public void Initialize(string projectToken, string appVersion)
{
Initialize (projectToken, appVersion, null);
}
Expand Down Expand Up @@ -125,5 +126,10 @@ public void SessionStatus(bool paused, Dictionary<string, object> properties)
}
}

public void GetCurrentSegment(string projectSecret, string segmentationId, Action<bool, InfinarioSegment, string> onSegmentReceiveCallback)
{
implementation.GetCurrentSegment(projectSecret, segmentationId, onSegmentReceiveCallback);
}

}
}
4 changes: 3 additions & 1 deletion source/Assets/Scripts/InfinarioSDK/InfinarioInterface.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Infinario.Interface
{
Expand All @@ -11,5 +12,6 @@ abstract class IInfinario
public abstract void TrackSessionStart (Dictionary<string, object> properties);
public abstract void TrackSessionEnd (Dictionary<string, object> properties);
public abstract void TrackVirtualPayment (string currency, long amount, string itemName, string itemType);
public abstract void GetCurrentSegment (string projectSecret, string segmentaionId, Action<bool, InfinarioSegment, string> onSegmentReceiveCallback);
}
}
39 changes: 39 additions & 0 deletions source/Assets/Scripts/InfinarioSDK/InfinarioSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace Infinario
{
class InfinarioSegment
{
private string _name;
private string _analysis_name;
private int _index;

public InfinarioSegment(string name, string analysisName, int index){
this._name = name;
this._analysis_name = analysisName;
this._index = index;
}

public string GetName(){
return this._name;
}

public string GetSegmentationName()
{
return this._analysis_name;
}

public int? GetSegmentIndex()
{
return this._index;
}

public override string ToString()
{
return "Segment: " + _name + ", Segment Index: " + _index + ", Segmentation name: " + _analysis_name;
}
}
}
69 changes: 59 additions & 10 deletions source/Assets/Scripts/InfinarioSDK/InfinarioUnitySDK.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -16,6 +17,7 @@ class Unity : IInfinario
private string appVersion = null;
private Dictionary<string, object> deviceProperties = null;
Dictionary<string, object> customerIds = null;
private Sender.Sender sender;

private static object initializeFinalizeLock = new object();
private static object sessionLock = new object();
Expand All @@ -33,23 +35,63 @@ public override void Initialize(string projectToken, string appVersion, string t

commandQueue = new PersistentBulkCommandQueue ("events", Constants.BULK_LIMIT);

new Sender.Sender ((target != null) ? target : Constants.DEFAULT_TARGET, commandQueue);
sender = new Sender.Sender ((target != null) ? target : Constants.DEFAULT_TARGET, commandQueue);
}

public override void Identify(Dictionary<string, object> customer, Dictionary<string, object> properties)
{
if (customer.ContainsKey (Constants.ID_REGISTERED) && customer[Constants.ID_REGISTERED] != null)
{
customerIds[Constants.ID_REGISTERED] = customer[Constants.ID_REGISTERED];

if (!customer[Constants.ID_REGISTERED].Equals(storage.GetRegisteredId()))
if ((customer.ContainsKey (Constants.ID_REGISTERED) && customer[Constants.ID_REGISTERED] != null) ||
(customer.ContainsKey(Constants.ID_USER) && customer[Constants.ID_USER] != null))
{
if (customer.ContainsKey(Constants.ID_REGISTERED))
{
customerIds[Constants.ID_REGISTERED] = customer[Constants.ID_REGISTERED];
}
else
{
if (customerIds.ContainsKey(Constants.ID_REGISTERED))
{
customerIds.Remove(Constants.ID_REGISTERED);
}
}
if (customer.ContainsKey(Constants.ID_USER))
{
storage.SaveRegisteredId(customer[Constants.ID_REGISTERED].ToString());
Dictionary<string, object> mergedProperties = MergeAutomaticProperties(customer);
customerIds[Constants.ID_USER] = customer[Constants.ID_USER];
}
else
{
if (customerIds.ContainsKey(Constants.ID_USER))
{
customerIds.Remove(Constants.ID_USER);
}
}

if ((customer.ContainsKey(Constants.ID_REGISTERED) && !customer[Constants.ID_REGISTERED].Equals(storage.GetRegisteredId())) ||
(customer.ContainsKey(Constants.ID_USER) && !customer[Constants.ID_USER].Equals(storage.GetUserId())))
{
if (customer.ContainsKey(Constants.ID_REGISTERED))
{
storage.SaveRegisteredId(customer[Constants.ID_REGISTERED].ToString());
}
else
{
storage.SaveRegisteredId(null);
}
if (customer.ContainsKey(Constants.ID_USER))
{
storage.SaveUserId(customer[Constants.ID_USER].ToString());
}
else
{
storage.SaveUserId(null);
}
Dictionary<string, object> mergedProperties = MergeAutomaticProperties(customer);
Track (Constants.EVENT_IDENTIFICATION, mergedProperties, double.NaN);

if (properties != null)
if (properties != null)
{
Update(properties);
}
}
}
}
Expand Down Expand Up @@ -128,7 +170,12 @@ public override void TrackVirtualPayment(string currency, long amount, string it
this.Track(Constants.EVENT_VIRTUAL_PAYMENT, properties, double.NaN);
}

private Dictionary<string, object> MergeAutomaticProperties(Dictionary<string, object> properties)
public override void GetCurrentSegment(string projectSecret, string segmentaionId, Action<bool, InfinarioSegment, string> onSegmentReceiveCallback)
{
sender.GetCurrentSegment(customerIds, projectSecret, segmentaionId, onSegmentReceiveCallback);
}

private Dictionary<string, object> MergeAutomaticProperties(Dictionary<string, object> properties)
{
lock (initializeFinalizeLock)
{
Expand All @@ -151,5 +198,7 @@ private void ScheduleCommand(Command command)
lst.Add (command.Execute());
commandQueue.MultiEnqueue(lst);
}


}
}
30 changes: 26 additions & 4 deletions source/Assets/Scripts/InfinarioSDK/PersistentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Infinario.MiniJSON;
using System;
using System.Linq;
using System.Globalization;

namespace Infinario.Storage
{
Expand Down Expand Up @@ -57,11 +58,28 @@ public string GetRegisteredId()
}
}

public void SaveSessionStart(double timestamp, Dictionary<string, object> properties)
public void SaveUserId(string userId)
{
lock (lockAccess)
{
PlayerPrefs.SetString(Constants.ID_USER, userId);
}
}

public string GetUserId()
{
lock (lockAccess)
{
var userId = PlayerPrefs.GetString(Constants.ID_USER);
return (String.IsNullOrEmpty(userId) ? null : userId);
}
}

public void SaveSessionStart(double timestamp, Dictionary<string, object> properties)
{
lock (lockAccess)
{
PlayerPrefs.SetString (Constants.PROPERTY_SESSION_START_TIMESTAMP, timestamp.ToString());
PlayerPrefs.SetString (Constants.PROPERTY_SESSION_START_TIMESTAMP, timestamp.ToString("R", CultureInfo.InvariantCulture));
PlayerPrefs.SetString (Constants.PROPERTY_SESSION_START_PROPERTIES, Json.Serialize(properties));
}
}
Expand All @@ -87,7 +105,7 @@ public void SaveSessionEnd(double timestamp, Dictionary<string, object> properti
{
lock (lockAccess)
{
PlayerPrefs.SetString (Constants.PROPERTY_SESSION_END_TIMESTAMP, timestamp.ToString());
PlayerPrefs.SetString (Constants.PROPERTY_SESSION_END_TIMESTAMP, timestamp.ToString("R", CultureInfo.InvariantCulture));
PlayerPrefs.SetString (Constants.PROPERTY_SESSION_END_PROPERTIES, Json.Serialize(properties));
}
}
Expand Down Expand Up @@ -129,7 +147,11 @@ public Dictionary<string, object> GetIds()
var registered = GetRegisteredId ();
if (!string.IsNullOrEmpty (registered))
ids.Add(Constants.ID_REGISTERED, registered);
return ids;

var userid = GetUserId();
if (!string.IsNullOrEmpty(userid))
ids.Add(Constants.ID_USER, userid);
return ids;
}
}
}
Loading