Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. namespace Snake.Scripts.Analytics {
  2.     using System;
  3.     using System.Collections.Generic;
  4.     using System.Linq;
  5.     using Facebook.Unity;
  6.     using GameAnalyticsSDK;
  7.     using UnityEngine;
  8.  
  9.     public interface IAnalyticsController {
  10.         void Initialize();
  11.  
  12.         /// <summary>
  13.         /// Track any type of design event that you want to measure i.e. GUI elements or tutorial steps. Custom dimensions are not supported.
  14.         /// </summary>
  15.         /// <param name="eventName">String can consist of 1 to 5 segments. Segments are seperated by ':' and segments can have a max length of 16. (e.g. segment1:anotherSegment:gold).</param>
  16.         void PushDesignEvent(string identifier, bool count = true);
  17.  
  18.         /// <summary>
  19.         /// Track any real money transaction in-game.
  20.         /// </summary>
  21.         /// <param name="currency">Currency code in ISO 4217 format. (e.g. USD).</param>
  22.         /// <param name="amount">Amount in cents (int). (e.g. 99).</param>
  23.         /// <param name="itemType">Item Type bought. (e.g. Gold Pack).</param>
  24.         /// <param name="itemId">Item bought. (e.g. 1000 gold).</param>
  25.         /// <param name="cartType">Cart type.</param>
  26.         void PushBusinessEvent(string currency, int amount, string itemType, string itemId, string cartType);
  27.     }
  28.  
  29.     public class AnalyticsController : IAnalyticsController {
  30.         public void Initialize() {
  31.             GameAnalytics.Initialize();
  32.             if (FB.IsInitialized) {
  33.                 FB.ActivateApp();
  34.             } else {
  35.                 //Handle FB.Init
  36.                 FB.Init( () => {
  37.                     FB.ActivateApp();
  38.                     FB.LogAppEvent("AppStart");
  39.                 });
  40.             }
  41.         }
  42.  
  43.         public void PushDesignEvent(string identifier, bool count = true) {
  44.             try {
  45.                 GameAnalytics.NewDesignEvent(identifier);
  46.                 SendAppMetrica(identifier, null, count);
  47.                 if(FB.IsInitialized) FB.LogAppEvent(identifier);
  48.             }
  49.             catch (Exception e) {
  50.                 Debug.LogException(e);
  51.                 throw;
  52.             }
  53.         }
  54.  
  55.         public void PushBusinessEvent(string currency, int amount, string itemType, string itemId, string cartType) {
  56.             GameAnalytics.NewBusinessEvent(currency, amount, itemType, itemId, cartType);
  57.  
  58.             var child = new Dictionary<string, object> {
  59.                 [nameof(currency)] = currency,
  60.                 [nameof(amount)]   = amount,
  61.                 [nameof(itemType)] = itemType,
  62.                 [nameof(itemId)]   = itemId,
  63.                 [nameof(cartType)] = cartType
  64.             };
  65.            
  66.             SendAppMetrica("cs:monetization:inapp:purchased", child ,false);
  67.         }
  68.        
  69.         private static object Req(Queue<string> queue, object data) {
  70.             while (true) {
  71.                 if (queue.Count == 0) {
  72.                     return data;
  73.                 }
  74.  
  75.                 var key = queue.Dequeue();
  76.                 data = new Dictionary<string, object> {[key] = data};
  77.             }
  78.         }
  79.  
  80.         private void SendAppMetrica(string identifier, Dictionary<string, object> child, bool count = true)
  81.         {
  82.             var split = new Queue<string>(identifier.Split(':').Reverse());
  83.  
  84.             string val = null;
  85.             if (count) {
  86.                 var i = PlayerPrefs.GetInt(identifier, 0);
  87.                 val = i.ToString();
  88.                 PlayerPrefs.SetInt(identifier, i + 1);
  89.             }
  90.  
  91.             var metricData = child == null ? Req(split, val) : Req(split, child);
  92.             if (metricData is Dictionary<string, object> dict) {
  93.                 var report = dict.First();
  94.                 AppMetrica.Instance.ReportEvent(report.Key, (Dictionary<string, object>) report.Value);
  95.             }
  96.             else {
  97.                 AppMetrica.Instance.ReportEvent((string) metricData);
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement