Skip to content

Commit 23a482b

Browse files
refactor(audience-sdk): introduce EventNames
Names the auto-fired and typed event-name strings so runtime emit and the sample-app catalogue read from one place. - Constants.cs: adds EventNames with SessionStart, SessionEnd, SessionHeartbeat, GameLaunch, Progression, Resource, Purchase, MilestoneReached. - Session.cs: SafeTrack calls for session_start / session_end / session_heartbeat read from EventNames. - ImmutableAudience.cs: auto-fired game_launch event reads from EventNames.GameLaunch. - TypedEvents.cs: Progression / Resource / Purchase / MilestoneReached EventName properties read from EventNames. - AudienceSample.Events.cs: typed-event catalogue and the BuildTypedEvent dispatch read from EventNames.
1 parent 0f93ad9 commit 23a482b

5 files changed

Lines changed: 35 additions & 17 deletions

File tree

examples/audience/Assets/SampleApp/Scripts/AudienceSample.Events.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ internal readonly struct EventSpec
5151
EventField.Text("platform", optional: true),
5252
}),
5353
new EventSpec("wishlist_remove", new[] { EventField.Text("gameId") }),
54-
new EventSpec("purchase", new[] {
54+
new EventSpec(EventNames.Purchase, new[] {
5555
EventField.Text("currency"),
5656
EventField.Number("value"),
5757
EventField.Text("itemId", optional: true),
@@ -62,22 +62,22 @@ internal readonly struct EventSpec
6262
// game_launch is deliberately absent. The Event Reference v1 defines
6363
// it as auto-tracked on Init with no public typed class; firing it
6464
// from the Send button would double-emit.
65-
new EventSpec("progression", new[] {
65+
new EventSpec(EventNames.Progression, new[] {
6666
EventField.Enum("status", new[] { "start", "complete", "fail" }),
6767
EventField.Text("world", optional: true),
6868
EventField.Text("level", optional: true),
6969
EventField.Text("stage", optional: true),
7070
EventField.Number("score", optional: true),
7171
EventField.Number("durationSec", optional: true),
7272
}),
73-
new EventSpec("resource", new[] {
73+
new EventSpec(EventNames.Resource, new[] {
7474
EventField.Enum("flow", new[] { "sink", "source" }),
7575
EventField.Text("currency"),
7676
EventField.Number("amount"),
7777
EventField.Text("itemType", optional: true),
7878
EventField.Text("itemId", optional: true),
7979
}),
80-
new EventSpec("milestone_reached", new[] { EventField.Text("name") }),
80+
new EventSpec(EventNames.MilestoneReached, new[] { EventField.Text("name") }),
8181
new EventSpec("game_page_viewed", new[] {
8282
EventField.Text("gameId"),
8383
EventField.Text("gameName", optional: true),
@@ -99,7 +99,7 @@ internal readonly struct EventSpec
9999
{
100100
switch (name)
101101
{
102-
case "progression":
102+
case EventNames.Progression:
103103
return new Progression
104104
{
105105
Status = ParseProgressionStatus(props),
@@ -109,7 +109,7 @@ internal readonly struct EventSpec
109109
Score = OptionalInt(props, "score"),
110110
DurationSec = OptionalFloat(props, "durationSec"),
111111
};
112-
case "resource":
112+
case EventNames.Resource:
113113
return new Resource
114114
{
115115
Flow = ParseResourceFlow(props),
@@ -118,7 +118,7 @@ internal readonly struct EventSpec
118118
ItemType = OptionalString(props, "itemType"),
119119
ItemId = OptionalString(props, "itemId"),
120120
};
121-
case "purchase":
121+
case EventNames.Purchase:
122122
return new Purchase
123123
{
124124
Currency = OptionalString(props, "currency") ?? "",
@@ -128,7 +128,7 @@ internal readonly struct EventSpec
128128
Quantity = OptionalInt(props, "quantity"),
129129
TransactionId = OptionalString(props, "transactionId"),
130130
};
131-
case "milestone_reached":
131+
case EventNames.MilestoneReached:
132132
return new MilestoneReached { Name = OptionalString(props, "name") ?? "" };
133133
default:
134134
return null;

src/Packages/Audience/Runtime/Core/Constants.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ internal static class ResponseFields
7676
internal const string Rejected = "rejected";
7777
}
7878

79+
// Event names we send on Track.
80+
internal static class EventNames
81+
{
82+
// Session lifecycle (auto-fired)
83+
internal const string SessionStart = "session_start";
84+
internal const string SessionEnd = "session_end";
85+
internal const string SessionHeartbeat = "session_heartbeat";
86+
87+
// Init lifecycle (auto-fired)
88+
internal const string GameLaunch = "game_launch";
89+
90+
// Typed events (IEvent implementations)
91+
internal const string Progression = "progression";
92+
internal const string Resource = "resource";
93+
internal const string Purchase = "purchase";
94+
internal const string MilestoneReached = "milestone_reached";
95+
}
96+
7997
// Wire-format field names that cross module boundaries inside the SDK
8098
// (read by one module, written by another).
8199
internal static class MessageFields

src/Packages/Audience/Runtime/Core/Session.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal void Start()
101101
_heartbeatTimer = new Timer(_ => OnHeartbeat(), null, _heartbeatIntervalMs, _heartbeatIntervalMs);
102102
}
103103

104-
SafeTrack("session_start", new Dictionary<string, object>
104+
SafeTrack(EventNames.SessionStart, new Dictionary<string, object>
105105
{
106106
["sessionId"] = sessionId
107107
});
@@ -177,7 +177,7 @@ internal void End()
177177

178178
// duration is engagement-aware (excludes pause). Web SDK emits
179179
// wall-clock; dashboards should not assume parity.
180-
SafeTrack("session_end", new Dictionary<string, object>
180+
SafeTrack(EventNames.SessionEnd, new Dictionary<string, object>
181181
{
182182
["sessionId"] = sessionId,
183183
["durationSec"] = duration
@@ -202,7 +202,7 @@ internal void EmitEndAndSeal()
202202
ResetSessionStateLocked();
203203
}
204204

205-
SafeTrack("session_end", new Dictionary<string, object>
205+
SafeTrack(EventNames.SessionEnd, new Dictionary<string, object>
206206
{
207207
["sessionId"] = sessionId,
208208
["durationSec"] = duration
@@ -250,7 +250,7 @@ internal void OnHeartbeat()
250250
["durationSec"] = duration
251251
};
252252

253-
SafeTrack("session_heartbeat", properties);
253+
SafeTrack(EventNames.SessionHeartbeat, properties);
254254
}
255255

256256
// Stops exceptions from the track callback from reaching upstream.

src/Packages/Audience/Runtime/Events/TypedEvents.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class Progression : IEvent
7878
public float? DurationSec { get; set; }
7979

8080
/// <inheritdoc/>
81-
public string EventName => "progression";
81+
public string EventName => EventNames.Progression;
8282

8383
/// <inheritdoc/>
8484
public Dictionary<string, object> ToProperties()
@@ -163,7 +163,7 @@ public class Resource : IEvent
163163
public string? ItemId { get; set; }
164164

165165
/// <inheritdoc/>
166-
public string EventName => "resource";
166+
public string EventName => EventNames.Resource;
167167

168168
/// <inheritdoc/>
169169
public Dictionary<string, object> ToProperties()
@@ -227,7 +227,7 @@ public class Purchase : IEvent
227227
public string? TransactionId { get; set; }
228228

229229
/// <inheritdoc/>
230-
public string EventName => "purchase";
230+
public string EventName => EventNames.Purchase;
231231

232232
// Hand-rolled to avoid pulling System.Text.RegularExpressions into the IL2CPP build.
233233
private static bool IsIso4217(string s)
@@ -278,7 +278,7 @@ public class MilestoneReached : IEvent
278278
public string? Name { get; set; }
279279

280280
/// <inheritdoc/>
281-
public string EventName => "milestone_reached";
281+
public string EventName => EventNames.MilestoneReached;
282282

283283
/// <inheritdoc/>
284284
public Dictionary<string, object> ToProperties()

src/Packages/Audience/Runtime/ImmutableAudience.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ private static void FireGameLaunch(AudienceConfig config, ConsentLevel consentAt
10201020

10211021
// No sessionId on game_launch per Event Reference. Pipeline correlates
10221022
// via eventTimestamp with the session_start that fires just before.
1023-
Track("game_launch", properties.Count > 0 ? properties : null);
1023+
Track(EventNames.GameLaunch, properties.Count > 0 ? properties : null);
10241024
}
10251025
}
10261026
}

0 commit comments

Comments
 (0)