Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Snake.Scripts.Analytics {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Facebook.Unity;
- using GameAnalyticsSDK;
- using UnityEngine;
- public interface IAnalyticsController {
- void Initialize();
- /// <summary>
- /// Track any type of design event that you want to measure i.e. GUI elements or tutorial steps. Custom dimensions are not supported.
- /// </summary>
- /// <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>
- void PushDesignEvent(string identifier, bool count = true);
- /// <summary>
- /// Track any real money transaction in-game.
- /// </summary>
- /// <param name="currency">Currency code in ISO 4217 format. (e.g. USD).</param>
- /// <param name="amount">Amount in cents (int). (e.g. 99).</param>
- /// <param name="itemType">Item Type bought. (e.g. Gold Pack).</param>
- /// <param name="itemId">Item bought. (e.g. 1000 gold).</param>
- /// <param name="cartType">Cart type.</param>
- void PushBusinessEvent(string currency, int amount, string itemType, string itemId, string cartType);
- }
- public class AnalyticsController : IAnalyticsController {
- public void Initialize() {
- GameAnalytics.Initialize();
- if (FB.IsInitialized) {
- FB.ActivateApp();
- } else {
- //Handle FB.Init
- FB.Init( () => {
- FB.ActivateApp();
- FB.LogAppEvent("AppStart");
- });
- }
- }
- public void PushDesignEvent(string identifier, bool count = true) {
- try {
- GameAnalytics.NewDesignEvent(identifier);
- SendAppMetrica(identifier, null, count);
- if(FB.IsInitialized) FB.LogAppEvent(identifier);
- }
- catch (Exception e) {
- Debug.LogException(e);
- throw;
- }
- }
- public void PushBusinessEvent(string currency, int amount, string itemType, string itemId, string cartType) {
- GameAnalytics.NewBusinessEvent(currency, amount, itemType, itemId, cartType);
- var child = new Dictionary<string, object> {
- [nameof(currency)] = currency,
- [nameof(amount)] = amount,
- [nameof(itemType)] = itemType,
- [nameof(itemId)] = itemId,
- [nameof(cartType)] = cartType
- };
- SendAppMetrica("cs:monetization:inapp:purchased", child ,false);
- }
- private static object Req(Queue<string> queue, object data) {
- while (true) {
- if (queue.Count == 0) {
- return data;
- }
- var key = queue.Dequeue();
- data = new Dictionary<string, object> {[key] = data};
- }
- }
- private void SendAppMetrica(string identifier, Dictionary<string, object> child, bool count = true)
- {
- var split = new Queue<string>(identifier.Split(':').Reverse());
- string val = null;
- if (count) {
- var i = PlayerPrefs.GetInt(identifier, 0);
- val = i.ToString();
- PlayerPrefs.SetInt(identifier, i + 1);
- }
- var metricData = child == null ? Req(split, val) : Req(split, child);
- if (metricData is Dictionary<string, object> dict) {
- var report = dict.First();
- AppMetrica.Instance.ReportEvent(report.Key, (Dictionary<string, object>) report.Value);
- }
- else {
- AppMetrica.Instance.ReportEvent((string) metricData);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement