Advertisement
Guest User

Oculus Analytics

a guest
Apr 4th, 2016
3,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.82 KB | None | 0 0
  1. namespace Logging
  2. {
  3.     public class Analytics : SingletonFactory.Singleton
  4.     {
  5.         public static class EventNames
  6.         {
  7.             public const string SECTION_ENTRY = "section_entry";
  8.  
  9.             public const string SECTION_EXIT = "section_exit";
  10.  
  11.             public const string SECTION_ACTIVITY = "section_activity";
  12.  
  13.             public const string HOME_INIT = "oculus_home_init";
  14.  
  15.             public const string HOME_ERROR = "home_error";
  16.  
  17.             public const string FRAMETIME = "frametime";
  18.  
  19.             public const string MEMORY_USAGE = "memory_usage";
  20.  
  21.             public const string OCULUS_WATERFALL = "oculus_waterfall";
  22.         }
  23.  
  24.         public static class IAPConstants
  25.         {
  26.             public const string FLOW_NAME = "iap_checkout";
  27.  
  28.             public const string IAP_START = "iap_checkout_start";
  29.  
  30.             public const string IAP_CANCELLED = "iap_checkout_cancelled";
  31.  
  32.             public const string IAP_FINISHED = "iap_checkout_finished";
  33.  
  34.             public const string PURCHASE_TOTAL = "purchase_total";
  35.  
  36.             public const string PURCHASE_PIN = "purchase_pin";
  37.         }
  38.  
  39.         public static class ExtraKeys
  40.         {
  41.             public const string FLOW_NAME = "flow";
  42.  
  43.             public const string STEP = "step";
  44.  
  45.             public const string IAP_FBID = "iap_id";
  46.  
  47.             public const string SECTION_NAME = "section_name";
  48.  
  49.             public const string SUB_SECTION = "sub_section";
  50.  
  51.             public const string TIME_SPENT = "active_duration_ms";
  52.  
  53.             public const string ACTIVITY_TYPE = "activity_type";
  54.  
  55.             public const string ACTIVITY_EXTRA = "activity_extra";
  56.  
  57.             public const string SORT_TYPE = "sort_type";
  58.  
  59.             public const string REFERRER = "referrer";
  60.  
  61.             public const string INIT_STAGE = "stage";
  62.  
  63.             public const string INIT_START = "time_start";
  64.  
  65.             public const string INIT_END = "time_end";
  66.  
  67.             public const string INIT_DURATION = "duration";
  68.  
  69.             public const string MIN_FRAME_TIME = "min_frame_time";
  70.  
  71.             public const string AVG_FRAME_TIME = "avg_frame_time";
  72.  
  73.             public const string MAX_FRAME_TIME = "max_frame_time";
  74.  
  75.             public const string ERROR_CODE = "code";
  76.  
  77.             public const string ERROR_DESCRIPTION = "description";
  78.  
  79.             public const string ERROR_METHOD = "method";
  80.  
  81.             public const string MEMORY_USED = "memory_used";
  82.  
  83.             public const string ORIGIN_APP_NAME = "origin_app_name";
  84.  
  85.             public const string BUILD_VERSION = "build_version";
  86.  
  87.             public const string PLUGIN_VERSION = "plugin_version";
  88.         }
  89.  
  90.         public class SectionKeys
  91.         {
  92.             public const string UM = "um";
  93.  
  94.             public const string HOME_PRIME = "prime";
  95.  
  96.             public const string HOME_STORE = "store";
  97.  
  98.             public const string HOME_LIBRARY = "library";
  99.  
  100.             public const string PDP = "pdp";
  101.  
  102.             public const string HSW = "hsw";
  103.  
  104.             public const string COMMERCE = "commerce";
  105.         }
  106.  
  107.         public class SubSectionKeys
  108.         {
  109.             public const string PDP_OVERVIEW = "overview";
  110.  
  111.             public const string PDP_GAMEPLAY = "gameplay";
  112.  
  113.             public const string PDP_DEVELOPER = "developer";
  114.  
  115.             public const string PDP_MEDIA = "media";
  116.  
  117.             public const string COMMERCE_PURCHASETOTAL = "purchaseTotal";
  118.  
  119.             public const string COMMERCE_PINENTRY = "pinEntry";
  120.  
  121.             public const string COMMERCE_ADDPAYMENT = "addPaymentMethod";
  122.  
  123.             public const string UM_ACTIVITY = "activity";
  124.  
  125.             public const string UM_NOTIFICATIONS = "notification";
  126.  
  127.             public const string UM_FRIENDS = "friends";
  128.  
  129.             public const string EMPTY = "";
  130.         }
  131.  
  132.         public class ActivityTypes
  133.         {
  134.             public const string MAIN_NAVIGATION = "main_navigation";
  135.         }
  136.  
  137.         public class Event
  138.         {
  139.             public string Name;
  140.  
  141.             public Dictionary<string, object> Extras;
  142.  
  143.             private static readonly string[] _KEYS_TO_LOG = new string[]
  144.             {
  145.                 "section_name",
  146.                 "sub_section",
  147.                 "active_duration_ms",
  148.                 "stage",
  149.                 "sort_type"
  150.             };
  151.  
  152.             public Event(string name)
  153.             {
  154.                 this.Name = name;
  155.                 this.Extras = new Dictionary<string, object>();
  156.             }
  157.  
  158.             public Analytics.Event AddExtra(string key, string value)
  159.             {
  160.                 this.Extras.Add(key, value);
  161.                 return this;
  162.             }
  163.  
  164.             public Analytics.Event AddCurrentSection(bool triggerSectionInterceptor = false)
  165.             {
  166.                 if (Analytics.Instance.timeTracking.ContainsKey(Analytics.TimeTrackerKey.SECTION))
  167.                 {
  168.                     Analytics.TimeTrackerItems.Section section = (Analytics.TimeTrackerItems.Section)Analytics.Instance.timeTracking[Analytics.TimeTrackerKey.SECTION].Value;
  169.                     this.Extras.Add("section_name", section.Name);
  170.                     if (triggerSectionInterceptor && section.Interceptor != null)
  171.                     {
  172.                         section.Interceptor(this);
  173.                     }
  174.                 }
  175.                 if (Analytics.Instance.timeTracking.ContainsKey(Analytics.TimeTrackerKey.SUB_SECTION))
  176.                 {
  177.                     Analytics.TimeTrackerItems.SubSection subSection = (Analytics.TimeTrackerItems.SubSection)Analytics.Instance.timeTracking[Analytics.TimeTrackerKey.SUB_SECTION].Value;
  178.                     this.Extras.Add("sub_section", subSection.Name);
  179.                     this.Extras.Add("sort_type", subSection.Sort);
  180.                 }
  181.                 return this;
  182.             }
  183.  
  184.             public Analytics.Event AddTimeSpent(long timeSpent)
  185.             {
  186.                 this.Extras.Add("active_duration_ms", timeSpent);
  187.                 return this;
  188.             }
  189.  
  190.             public override string ToString()
  191.             {
  192.                 string text = string.Empty;
  193.                 int num = 0;
  194.                 for (int i = 0; i < Analytics.Event._KEYS_TO_LOG.Length; i++)
  195.                 {
  196.                     if (this.Extras.ContainsKey(Analytics.Event._KEYS_TO_LOG[i]))
  197.                     {
  198.                         string text2 = text;
  199.                         text = string.Concat(new object[]
  200.                         {
  201.                             text2,
  202.                             (num != 0) ? ", " : string.Empty,
  203.                             Analytics.Event._KEYS_TO_LOG[i],
  204.                             " = ",
  205.                             this.Extras[Analytics.Event._KEYS_TO_LOG[i]]
  206.                         });
  207.                         num++;
  208.                     }
  209.                 }
  210.                 return string.Concat(new object[]
  211.                 {
  212.                     "Analytics.Event[",
  213.                     this.Name,
  214.                     "] => ",
  215.                     text,
  216.                     " + (",
  217.                     this.Extras.Count - num,
  218.                     ") extras"
  219.                 });
  220.             }
  221.         }
  222.  
  223.         private enum TimeTrackerKey
  224.         {
  225.             SECTION,
  226.             SUB_SECTION
  227.         }
  228.  
  229.         private class TimeTrackerItems
  230.         {
  231.             public abstract class TimeTrackerItem
  232.             {
  233.                 public Analytics.LogInterceptor Interceptor;
  234.             }
  235.  
  236.             public class Section : Analytics.TimeTrackerItems.TimeTrackerItem
  237.             {
  238.                 public string Name;
  239.             }
  240.  
  241.             public class SubSection : Analytics.TimeTrackerItems.TimeTrackerItem
  242.             {
  243.                 public string Name;
  244.  
  245.                 public string Sort;
  246.             }
  247.         }
  248.  
  249.         private class TimeTracker
  250.         {
  251.             public readonly object Value;
  252.  
  253.             private readonly long TimeStart;
  254.  
  255.             public TimeTracker(object value)
  256.             {
  257.                 this.Value = value;
  258.                 this.TimeStart = Analytics.TimeTracker.Now();
  259.             }
  260.  
  261.             public long GetTimeSpent()
  262.             {
  263.                 return Analytics.TimeTracker.Now() - this.TimeStart;
  264.             }
  265.  
  266.             private static long Now()
  267.             {
  268.                 return DateTime.Now.Ticks / 10000L;
  269.             }
  270.         }
  271.  
  272.         public delegate void LogInterceptor(Analytics.Event loggingEvent);
  273.  
  274.         public const string ORIGIN_APP_NAME = "home";
  275.  
  276.         private static readonly IDictionary<string, string> REFERRER_MAP = new Dictionary<string, string>
  277.         {
  278.             {
  279.                 "prime",
  280.                 "prime"
  281.             },
  282.             {
  283.                 "store",
  284.                 "store"
  285.             },
  286.             {
  287.                 "library",
  288.                 "library"
  289.             },
  290.             {
  291.                 "pdp",
  292.                 "pdp"
  293.             },
  294.             {
  295.                 "commerce",
  296.                 "commerce"
  297.             }
  298.         };
  299.  
  300.         private string lastIAPRoute;
  301.  
  302.         private long lastIapRouteElapsedTime;
  303.  
  304.         private Stopwatch stopWatch;
  305.  
  306.         private readonly IDictionary<Analytics.TimeTrackerKey, Analytics.TimeTracker> timeTracking = new Dictionary<Analytics.TimeTrackerKey, Analytics.TimeTracker>();
  307.  
  308.         private Analytics.TimeTrackerItems.Section preFocusLostSection;
  309.  
  310.         private Analytics.TimeTrackerItems.SubSection preFocusLostSubsection;
  311.  
  312.         private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  313.  
  314.         private static readonly DateTime sessionStart = DateTime.UtcNow;
  315.  
  316.         public static Analytics Instance
  317.         {
  318.             get
  319.             {
  320.                 return SingletonFactory.Instance.Get<Analytics>();
  321.             }
  322.         }
  323.  
  324.         public void LogEvent(Analytics.Event loggingEvent, Analytics.LogInterceptor interceptor)
  325.         {
  326.             this.AddStandardData(loggingEvent);
  327.             if (interceptor != null)
  328.             {
  329.                 interceptor(loggingEvent);
  330.             }
  331.             Lumberjack.V("Analytics", loggingEvent, new object[0]);
  332.             Singleton<OafConnector>.Instance.RequestMarauderEvent(loggingEvent.Name, loggingEvent.Extras, null);
  333.         }
  334.  
  335.         private Analytics.Event AddStandardData(Analytics.Event loggingEvent)
  336.         {
  337.             loggingEvent.AddExtra("origin_app_name", "home");
  338.             loggingEvent.AddExtra("build_version", "197670");
  339.             loggingEvent.AddExtra("plugin_version", "198758");
  340.             return loggingEvent;
  341.         }
  342.  
  343.         public static string GetUnixTimestamp()
  344.         {
  345.             return (DateTime.UtcNow - Analytics.unixEpoch).TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
  346.         }
  347.  
  348.         public static string GetUnixTimestamp(DateTime dt)
  349.         {
  350.             return (dt - Analytics.unixEpoch).TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
  351.         }
  352.  
  353.         public static string GetGlobalSectionReferrer(string route)
  354.         {
  355.             string[] array = SnapPath.Split(route, StringSplitOptions.RemoveEmptyEntries);
  356.             if (array.Length >= 1)
  357.             {
  358.                 int num = (!array[0].Equals("global")) ? 0 : 1;
  359.                 return (!Analytics.REFERRER_MAP.ContainsKey(array[num])) ? null : array[num];
  360.             }
  361.             return null;
  362.         }
  363.  
  364.         public void OnSectionEnter(string section, Analytics.LogInterceptor interceptor = null)
  365.         {
  366.             this.timeTracking.Add(Analytics.TimeTrackerKey.SECTION, new Analytics.TimeTracker(new Analytics.TimeTrackerItems.Section
  367.             {
  368.                 Name = section,
  369.                 Interceptor = interceptor
  370.             }));
  371.         }
  372.  
  373.         public void OnSectionExit(string section)
  374.         {
  375.             Analytics.TimeTracker timeTracker = this.timeTracking[Analytics.TimeTrackerKey.SECTION];
  376.             Analytics.TimeTrackerItems.Section section2 = (Analytics.TimeTrackerItems.Section)timeTracker.Value;
  377.             this.timeTracking.Remove(Analytics.TimeTrackerKey.SECTION);
  378.         }
  379.  
  380.         public void OnSubSectionEnter(string subsection, string sort, Analytics.LogInterceptor interceptor = null)
  381.         {
  382.             this.timeTracking.Add(Analytics.TimeTrackerKey.SUB_SECTION, new Analytics.TimeTracker(new Analytics.TimeTrackerItems.SubSection
  383.             {
  384.                 Name = subsection,
  385.                 Sort = sort,
  386.                 Interceptor = interceptor
  387.             }));
  388.             this.LogEvent(new Analytics.Event("section_entry").AddCurrentSection(true), interceptor);
  389.         }
  390.  
  391.         public void OnFrameRateUpdate(float maxFrameTime, float minFrameTime, float avgFrameTime)
  392.         {
  393.             Lumberjack.F("Perf", string.Concat(new object[]
  394.             {
  395.                 "max:",
  396.                 maxFrameTime,
  397.                 ", min:",
  398.                 minFrameTime,
  399.                 ", avg:",
  400.                 avgFrameTime
  401.             }), new string[0]);
  402.             this.LogEvent(new Analytics.Event("frametime").AddExtra("max_frame_time", maxFrameTime.ToString()).AddExtra("min_frame_time", minFrameTime.ToString()).AddExtra("avg_frame_time", avgFrameTime.ToString()), null);
  403.         }
  404.  
  405.         public void OnMemoryUsageUpdate(string memoryUsage)
  406.         {
  407.             Lumberjack.F("Perf", "memory:" + memoryUsage, new string[0]);
  408.             this.LogEvent(new Analytics.Event("memory_usage").AddExtra("memory_used", memoryUsage), null);
  409.         }
  410.  
  411.         public void OnSubSectionExit(string subsection, string sort)
  412.         {
  413.             Analytics.TimeTracker timeTracker = this.timeTracking[Analytics.TimeTrackerKey.SUB_SECTION];
  414.             Analytics.TimeTrackerItems.SubSection subSection = (Analytics.TimeTrackerItems.SubSection)timeTracker.Value;
  415.             this.LogEvent(new Analytics.Event("section_exit").AddCurrentSection(true).AddTimeSpent(timeTracker.GetTimeSpent()), subSection.Interceptor);
  416.             this.timeTracking.Remove(Analytics.TimeTrackerKey.SUB_SECTION);
  417.         }
  418.  
  419.         public void OnActivity(string type, string extra, Analytics.LogInterceptor interceptor = null)
  420.         {
  421.             this.LogEvent(new Analytics.Event("section_activity").AddExtra("activity_type", type).AddExtra("activity_extra", extra).AddCurrentSection(true), interceptor);
  422.         }
  423.  
  424.         public void OnNewIAPRootLoaded(string currentLocation, Analytics.LogInterceptor interceptor = null)
  425.         {
  426.             Analytics.Event @event = new Analytics.Event("oculus_waterfall");
  427.             @event.AddExtra("flow", "iap_checkout");
  428.             if (this.lastIAPRoute == null)
  429.             {
  430.                 @event.AddExtra("step", "iap_checkout_start");
  431.                 @event.AddExtra("active_duration_ms", "0");
  432.                 this.stopWatch = Stopwatch.StartNew();
  433.             }
  434.             else
  435.             {
  436.                 @event.AddExtra("step", this.lastIAPRoute);
  437.                 this.lastIAPRoute = currentLocation;
  438.                 long elapsedMilliseconds = this.stopWatch.ElapsedMilliseconds;
  439.                 long num = elapsedMilliseconds - this.lastIapRouteElapsedTime;
  440.                 this.lastIapRouteElapsedTime = elapsedMilliseconds;
  441.                 @event.AddExtra("active_duration_ms", num.ToString());
  442.             }
  443.             this.lastIAPRoute = currentLocation;
  444.             this.LogEvent(@event, interceptor);
  445.         }
  446.  
  447.         public void OnIAPCommerceCancelled(Analytics.LogInterceptor interceptor = null)
  448.         {
  449.             this.LogEvent(this.OnIAPCommerceCompletedHelper("iap_checkout_cancelled", interceptor), interceptor);
  450.         }
  451.  
  452.         public void OnIAPCommerceFinished(string iapFbid, Analytics.LogInterceptor interceptor = null)
  453.         {
  454.             Analytics.Event @event = this.OnIAPCommerceCompletedHelper("iap_checkout_finished", interceptor);
  455.             @event.AddExtra("iap_id", iapFbid);
  456.             this.LogEvent(@event, interceptor);
  457.         }
  458.  
  459.         private Analytics.Event OnIAPCommerceCompletedHelper(string finishStepName, Analytics.LogInterceptor interceptor = null)
  460.         {
  461.             this.OnNewIAPRootLoaded(null, interceptor);
  462.             Analytics.Event @event = new Analytics.Event("oculus_waterfall");
  463.             @event.AddExtra("flow", "iap_checkout");
  464.             @event.AddExtra("step", finishStepName);
  465.             @event.AddExtra("active_duration_ms", this.stopWatch.ElapsedMilliseconds.ToString());
  466.             this.lastIAPRoute = null;
  467.             this.lastIapRouteElapsedTime = 0L;
  468.             this.stopWatch.Stop();
  469.             this.stopWatch = null;
  470.             return @event;
  471.         }
  472.  
  473.         public void OnVRFocusChanged(bool hasFocus)
  474.         {
  475.             if (!hasFocus)
  476.             {
  477.                 if (this.timeTracking.ContainsKey(Analytics.TimeTrackerKey.SUB_SECTION))
  478.                 {
  479.                     this.preFocusLostSubsection = (Analytics.TimeTrackerItems.SubSection)this.timeTracking[Analytics.TimeTrackerKey.SUB_SECTION].Value;
  480.                     this.OnSubSectionExit(this.preFocusLostSubsection.Name, this.preFocusLostSubsection.Sort);
  481.                 }
  482.                 else
  483.                 {
  484.                     this.preFocusLostSubsection = null;
  485.                 }
  486.                 if (this.timeTracking.ContainsKey(Analytics.TimeTrackerKey.SECTION))
  487.                 {
  488.                     this.preFocusLostSection = (Analytics.TimeTrackerItems.Section)this.timeTracking[Analytics.TimeTrackerKey.SECTION].Value;
  489.                     this.OnSectionExit(this.preFocusLostSection.Name);
  490.                 }
  491.                 else
  492.                 {
  493.                     this.preFocusLostSection = null;
  494.                 }
  495.             }
  496.             else
  497.             {
  498.                 if (this.preFocusLostSection != null)
  499.                 {
  500.                     this.OnSectionEnter(this.preFocusLostSection.Name, null);
  501.                     this.preFocusLostSection = null;
  502.                 }
  503.                 if (this.preFocusLostSubsection != null)
  504.                 {
  505.                     this.OnSubSectionEnter(this.preFocusLostSubsection.Name, this.preFocusLostSubsection.Sort, null);
  506.                     this.preFocusLostSubsection = null;
  507.                 }
  508.             }
  509.         }
  510.  
  511.         public void OnApplicationQuit()
  512.         {
  513.             if (this.timeTracking.ContainsKey(Analytics.TimeTrackerKey.SUB_SECTION))
  514.             {
  515.                 Analytics.TimeTrackerItems.SubSection subSection = (Analytics.TimeTrackerItems.SubSection)this.timeTracking[Analytics.TimeTrackerKey.SUB_SECTION].Value;
  516.                 this.OnSubSectionExit(subSection.Name, subSection.Sort);
  517.             }
  518.             if (this.timeTracking.ContainsKey(Analytics.TimeTrackerKey.SECTION))
  519.             {
  520.                 Analytics.TimeTrackerItems.Section section = (Analytics.TimeTrackerItems.Section)this.timeTracking[Analytics.TimeTrackerKey.SECTION].Value;
  521.                 this.OnSectionExit(section.Name);
  522.             }
  523.         }
  524.  
  525.         public void OnErrorReceived(Error error, string requestMethod, Analytics.LogInterceptor interceptor = null)
  526.         {
  527.             this.LogEvent(new Analytics.Event("home_error").AddExtra("code", error.ErrorCode.ToString()).AddExtra("description", error.Description).AddExtra("method", requestMethod), interceptor);
  528.         }
  529.  
  530.         public void OnHomeInitStep(InitializationAction.Step step, Analytics.LogInterceptor interceptor = null)
  531.         {
  532.             this.LogEvent(new Analytics.Event("oculus_home_init").AddExtra("stage", step.Name).AddExtra("time_start", step.GetTimeStart().ToString()).AddExtra("time_end", step.GetTimeEnd().ToString()).AddExtra("duration", step.GetDuration().ToString()), interceptor);
  533.         }
  534.     }
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement