-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegmentTracking.cs
119 lines (100 loc) · 3.28 KB
/
SegmentTracking.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class SegmentTracking : MonoBehaviour
{
#if DEVELOPMENT_BUILD
private string _segmentApiKey = "DEV ENV SEGMENT WRITE KEY GOES HERE";
#else
private string _segmentApiKey = "PROD ENV SEGMENT WRITE KEY GOES HERE";
#endif
public static SegmentTracking Instance;
private static string _authorizationHeaderValue;
private static string _trackUrl = "https://api.segment.io/v1/track";
// private static string _trackUrl = "http://localhost:8080/"; // use netcat to inspect the payload being sent
public int FrameFlushSchedule = 30;
private Queue<Item> _queue = new Queue<Item>(20);
public class Item
{
public Item(string name, Hashtable properties)
{
Name = name;
Properties = properties;
}
public string Name { get; }
public Hashtable Properties { get; }
}
void Awake()
{
Debug.Log("Awake");
Instance = this;
byte[] segmentWriteKeyAsBytes = Encoding.ASCII.GetBytes(_segmentApiKey + ":");
Debug.Log(_segmentApiKey);
Debug.Log(segmentWriteKeyAsBytes);
_authorizationHeaderValue = "Basic " + System.Convert.ToBase64String(segmentWriteKeyAsBytes);
}
void Update()
{
if (Time.frameCount % FrameFlushSchedule == 0)
{
Flush();
}
}
public void Track(Item item)
{
Debug.Log("Tracking.Track " + item.Name);
_queue.Enqueue(item);
}
public void Track(string name, Hashtable properties)
{
Debug.Log("Tracking.Track " + name);
var item = new Item(name, properties);
_queue.Enqueue(item);
}
public void Flush()
{
while (_queue.Count > 0)
{
var item = _queue.Dequeue();
StartCoroutine(SendTrack(item));
}
}
private class RequestPostData
{
public string anonymousId;
public string _event;
public RequestPostData(string name)
{
anonymousId = "anonymousId";
_event = name;
}
}
IEnumerator SendTrack(Item item)
{
// TODO: postData should be constructed from the Item's name and properties
string postData = "{\"anonymousId\":\"anonymousId123\",\"event\":\"event\"}";
byte[] postDataAsBytes = Encoding.UTF8.GetBytes(postData);
UnityWebRequest request = new UnityWebRequest(_trackUrl);
request.method = UnityWebRequest.kHttpVerbPOST;
request.timeout = 30;
request.SetRequestHeader("Authorization", _authorizationHeaderValue);
request.SetRequestHeader("Content-Type", "application/json");
request.uploadHandler = new UploadHandlerRaw(postDataAsBytes);
request.uploadHandler.contentType = "application/json";
Debug.Log(request.GetRequestHeader("Authorization"));
Debug.Log(request.GetRequestHeader("Content-Type"));
Debug.Log(postData);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.Log(request.error);
}
else
{
Debug.Log("Tracked");
}
request.Dispose();
}
}