Skip to content

Commit f1b7adb

Browse files
refactor(audience-sample): centralise log payload JSON keys in SampleAppUi.LogPayloadKeys
The sample-app builds Dictionary<string, object> echo payloads for each RunAndLog "Ok" row (Track outcomes, Identify / Alias outcomes, OnError rows, Init config echo). Each key was inline at the construction site, so a rename (e.g. "publishableKey" to "key") would touch six call sites today and grow as the demo expands. Adds a LogPayloadKeys sub-class to SampleAppUi with eighteen constants covering the existing payload schema (Event, Overload, Effects, Id, Accepted, From, To, Code, Message, Consent, Debug, FlushIntervalSeconds, FlushSize, PackageVersion, ShutdownFlushTimeoutMs, PublishableKey, PersistentDataPath) plus a nested OverloadValues for the "typed" / "string" enum-like values written under LogPayloadKeys.Overload. Migrates AudienceSample.cs to reference the constants. UXML element names (in the existing SampleAppUi sub-classes) are unchanged. Per the user's "everything random goes in a constant" stance. Follow-up to SDK-272 (centralisation of duplicated literals).
1 parent 318ce6e commit f1b7adb

2 files changed

Lines changed: 61 additions & 24 deletions

File tree

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ private void OnSendCatalogueEvent(EventSpec spec, Dictionary<string, VisualEleme
130130
ImmutableAudience.Track(typed);
131131
return Json.Serialize(new Dictionary<string, object>
132132
{
133-
["event"] = spec.Name,
134-
["overload"] = "typed",
133+
[SampleAppUi.LogPayloadKeys.Event] = spec.Name,
134+
[SampleAppUi.LogPayloadKeys.Overload] = SampleAppUi.LogPayloadKeys.OverloadValues.Typed,
135135
[MessageFields.Properties] = typed.ToProperties(),
136136
}, 2);
137137
}
138138

139139
ImmutableAudience.Track(spec.Name, props.Count > 0 ? props : null);
140140
return Json.Serialize(new Dictionary<string, object>
141141
{
142-
["event"] = spec.Name,
143-
["overload"] = "string",
142+
[SampleAppUi.LogPayloadKeys.Event] = spec.Name,
143+
[SampleAppUi.LogPayloadKeys.Overload] = SampleAppUi.LogPayloadKeys.OverloadValues.String,
144144
[MessageFields.Properties] = props,
145145
}, 2);
146146
});
@@ -154,7 +154,7 @@ private void OnSendCustomEvent() => RunAndLog(SampleAppUi.LogLabels.Track, () =>
154154
var f = CaptureCustomEventForm();
155155
var props = string.IsNullOrEmpty(f.RawProps) ? null : JsonReader.DeserializeObject(f.RawProps);
156156
ImmutableAudience.Track(f.Name, props);
157-
var echo = new Dictionary<string, object> { ["event"] = f.Name };
157+
var echo = new Dictionary<string, object> { [SampleAppUi.LogPayloadKeys.Event] = f.Name };
158158
if (props != null) echo[MessageFields.Properties] = props;
159159
return Json.Serialize(echo, 2);
160160
});
@@ -171,14 +171,14 @@ private void OnSetConsent(ConsentLevel level) => RunAndLog(SampleAppUi.LogLabels
171171
if (!level.CanIdentify()) ResetIdentityMirror();
172172
var payload = new Dictionary<string, object>
173173
{
174-
["from"] = previous.ToLowercaseString(),
175-
["to"] = level.ToLowercaseString(),
174+
[SampleAppUi.LogPayloadKeys.From] = previous.ToLowercaseString(),
175+
[SampleAppUi.LogPayloadKeys.To] = level.ToLowercaseString(),
176176
};
177177
var effects = new List<string>();
178178
if (previous == ConsentLevel.None && level != ConsentLevel.None) effects.Add(SampleAppUi.Messages.QueueStartedSessionCreated);
179179
if (level == ConsentLevel.None) effects.Add(SampleAppUi.Messages.QueuePurgedAnonymousIdCleared);
180180
if (!level.CanIdentify() && previous.CanIdentify()) effects.Add(SampleAppUi.Messages.UserIdCleared);
181-
if (effects.Count > 0) payload["effects"] = effects;
181+
if (effects.Count > 0) payload[SampleAppUi.LogPayloadKeys.Effects] = effects;
182182
OnSdkStateChanged();
183183
return Json.Serialize(payload, 2);
184184
});
@@ -198,9 +198,9 @@ private void OnIdentify() => RunAndLog(SampleAppUi.LogLabels.Identify, () =>
198198
OnSdkStateChanged();
199199
var payload = new Dictionary<string, object>
200200
{
201-
["id"] = f.Id,
202-
[MessageFields.IdentityType] = f.Type,
203-
["accepted"] = accepted,
201+
[SampleAppUi.LogPayloadKeys.Id] = f.Id,
202+
[MessageFields.IdentityType] = f.Type,
203+
[SampleAppUi.LogPayloadKeys.Accepted] = accepted,
204204
};
205205
if (traits != null) payload[MessageFields.Traits] = traits;
206206
return Json.Serialize(payload, 2);
@@ -233,9 +233,9 @@ private void OnAlias() => RunAndLog(SampleAppUi.LogLabels.Alias, () =>
233233
}
234234
return Json.Serialize(new Dictionary<string, object>
235235
{
236-
["from"] = new Dictionary<string, object> { ["id"] = f.FromId, [MessageFields.IdentityType] = f.FromType },
237-
["to"] = new Dictionary<string, object> { ["id"] = f.ToId, [MessageFields.IdentityType] = f.ToType },
238-
["accepted"] = accepted,
236+
[SampleAppUi.LogPayloadKeys.From] = new Dictionary<string, object> { [SampleAppUi.LogPayloadKeys.Id] = f.FromId, [MessageFields.IdentityType] = f.FromType },
237+
[SampleAppUi.LogPayloadKeys.To] = new Dictionary<string, object> { [SampleAppUi.LogPayloadKeys.Id] = f.ToId, [MessageFields.IdentityType] = f.ToType },
238+
[SampleAppUi.LogPayloadKeys.Accepted] = accepted,
239239
}, 2);
240240
});
241241

@@ -246,8 +246,8 @@ private void OnAlias() => RunAndLog(SampleAppUi.LogLabels.Alias, () =>
246246
private void OnSdkError(AudienceError err) =>
247247
AppendLog(SampleAppUi.LogLabels.OnError, Json.Serialize(new Dictionary<string, object>
248248
{
249-
["code"] = err.Code.ToString(),
250-
["message"] = err.Message,
249+
[SampleAppUi.LogPayloadKeys.Code] = err.Code.ToString(),
250+
[SampleAppUi.LogPayloadKeys.Message] = err.Message,
251251
}, 2), LogLevel.Err, LogSource.Sdk);
252252

253253
// SDK Log.Writer adapter. May fire from any thread; AppendLog handles
@@ -334,17 +334,17 @@ private static Dictionary<string, object> BuildConfigEcho(AudienceConfig config)
334334
{
335335
var echo = new Dictionary<string, object>
336336
{
337-
["consent"] = config.Consent.ToString(),
338-
["debug"] = config.Debug,
339-
["flushIntervalSeconds"] = config.FlushIntervalSeconds,
340-
["flushSize"] = config.FlushSize,
341-
["packageVersion"] = config.PackageVersion,
342-
["shutdownFlushTimeoutMs"] = config.ShutdownFlushTimeoutMs,
337+
[SampleAppUi.LogPayloadKeys.Consent] = config.Consent.ToString(),
338+
[SampleAppUi.LogPayloadKeys.Debug] = config.Debug,
339+
[SampleAppUi.LogPayloadKeys.FlushIntervalSeconds] = config.FlushIntervalSeconds,
340+
[SampleAppUi.LogPayloadKeys.FlushSize] = config.FlushSize,
341+
[SampleAppUi.LogPayloadKeys.PackageVersion] = config.PackageVersion,
342+
[SampleAppUi.LogPayloadKeys.ShutdownFlushTimeoutMs] = config.ShutdownFlushTimeoutMs,
343343
};
344344
if (!string.IsNullOrEmpty(config.PublishableKey))
345-
echo["publishableKey"] = RedactPublishableKey(config.PublishableKey);
345+
echo[SampleAppUi.LogPayloadKeys.PublishableKey] = RedactPublishableKey(config.PublishableKey);
346346
if (!string.IsNullOrEmpty(config.PersistentDataPath))
347-
echo["persistentDataPath"] = config.PersistentDataPath;
347+
echo[SampleAppUi.LogPayloadKeys.PersistentDataPath] = config.PersistentDataPath;
348348
return echo;
349349
}
350350

examples/audience/Assets/SampleApp/Scripts/SampleAppUi.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,42 @@ internal static class Consent
301301
internal const string Anonymous = "anonymous";
302302
internal const string Full = "full";
303303
}
304+
305+
// Log payload JSON keys used by RunAndLog "Ok" row dictionaries.
306+
307+
internal static class LogPayloadKeys
308+
{
309+
// Track outcomes
310+
internal const string Event = "event";
311+
internal const string Overload = "overload";
312+
internal const string Effects = "effects";
313+
314+
// Identify / Alias outcomes
315+
internal const string Id = "id";
316+
internal const string Accepted = "accepted";
317+
internal const string From = "from";
318+
internal const string To = "to";
319+
320+
// OnError row payload
321+
internal const string Code = "code";
322+
internal const string Message = "message";
323+
324+
// Init config echo
325+
internal const string Consent = "consent";
326+
internal const string Debug = "debug";
327+
internal const string FlushIntervalSeconds = "flushIntervalSeconds";
328+
internal const string FlushSize = "flushSize";
329+
internal const string PackageVersion = "packageVersion";
330+
internal const string ShutdownFlushTimeoutMs = "shutdownFlushTimeoutMs";
331+
internal const string PublishableKey = "publishableKey";
332+
internal const string PersistentDataPath = "persistentDataPath";
333+
334+
// Track overload values.
335+
internal static class OverloadValues
336+
{
337+
internal const string Typed = "typed";
338+
internal const string String = "string";
339+
}
340+
}
304341
}
305342
}

0 commit comments

Comments
 (0)