AnalyticsServiceInstance.CustomData.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Unity.Services.Analytics
  5. {
  6. partial class AnalyticsServiceInstance
  7. {
  8. public void CustomData(string eventName, IDictionary<string, object> eventParams)
  9. {
  10. if (!ServiceEnabled)
  11. {
  12. return;
  13. }
  14. dataBuffer.PushStartEvent(eventName, DateTime.Now, null);
  15. foreach (var paramPair in eventParams)
  16. {
  17. /*
  18. * Had a read of the performance of typeof - the two options were a switch on Type.GetTypeCode(paramType) or
  19. * the if chain below. Although the if statement involves multiple typeofs, this is supposedly a fairly light
  20. * operation, and the alternative switch option involved some messy/crazy cases for ints.
  21. */
  22. var paramType = paramPair.Value.GetType();
  23. if (paramType == typeof(string))
  24. {
  25. dataBuffer.PushString((string)paramPair.Value, paramPair.Key);
  26. }
  27. else if (paramType == typeof(int))
  28. {
  29. dataBuffer.PushInt((int)paramPair.Value, paramPair.Key);
  30. }
  31. else if (paramType == typeof(long))
  32. {
  33. dataBuffer.PushInt64((long)paramPair.Value, paramPair.Key);
  34. }
  35. else if (paramType == typeof(float))
  36. {
  37. dataBuffer.PushFloat((float)paramPair.Value, paramPair.Key);
  38. }
  39. else if (paramType == typeof(double))
  40. {
  41. dataBuffer.PushDouble((double)paramPair.Value, paramPair.Key);
  42. }
  43. else if (paramType == typeof(bool))
  44. {
  45. dataBuffer.PushBool((bool)paramPair.Value, paramPair.Key);
  46. }
  47. else if (paramType == typeof(DateTime))
  48. {
  49. dataBuffer.PushTimestamp((DateTime)paramPair.Value, paramPair.Key);
  50. }
  51. else
  52. {
  53. Debug.LogError($"Unknown type found for key {paramPair.Key}, this value will not be included.");
  54. }
  55. }
  56. dataBuffer.PushEndEvent();
  57. }
  58. }
  59. }