Untitled

mail@pastecode.io avatar
unknown
csharp
20 days ago
6.9 kB
1
Indexable
Never
    private static readonly string FirebaseUserID = SystemInfo.deviceUniqueIdentifier;
    private void SetUserProperties()
    {
        FirebaseAnalytics.SetUserProperty("balance", EconomyManager.Instance.Resource.Current.ToString());
        FirebaseAnalytics.SetUserProperty("game_version", Application.version);
        switch (Application.internetReachability)
        {
            case NetworkReachability.NotReachable:
                FirebaseAnalytics.SetUserProperty("network_status", "offline");
                break;
            case NetworkReachability.ReachableViaCarrierDataNetwork:
                FirebaseAnalytics.SetUserProperty("network_status", "data");

                break;
            case NetworkReachability.ReachableViaLocalAreaNetwork:
                FirebaseAnalytics.SetUserProperty("network_status", "wifi");
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
        
        FirebaseAnalytics.SetUserProperty("test_user", FirebaseStatics.IsTestUser.ToString());
        FirebaseAnalytics.SetUserProperty("level", 1.ToString());
        FirebaseAnalytics.SetUserProperty("user_id", FirebaseUserID);
    }
    
    public void Track_PurchaseInitiate(string productName, float price, string currency, string result = "success", string source = "shop", string time = "Now")
    {
        FirebaseAnalytics.LogEvent("purchase_initate", 
            new Parameter("product_name", productName), 
            new Parameter(FirebaseAnalytics.ParameterPrice, price),
            new Parameter(FirebaseAnalytics.ParameterCurrency, currency),
            new Parameter("result", result),
             new Parameter("source", source),
            new Parameter("time", time)
            );
        
        SetUserProperties();
    }
    
    public void Track_PurchaseEnd(string productName, float price, string currency, string result = "success", string source = "shop", string time = "Now")
    {
        FirebaseAnalytics.LogEvent("purchase_end", 
            new Parameter("product_name", productName), 
            new Parameter(FirebaseAnalytics.ParameterPrice, price),
            new Parameter(FirebaseAnalytics.ParameterCurrency, currency),
            new Parameter("result", result),
            new Parameter("source", source),
            new Parameter("time", time)
        );
        
        SetUserProperties();
    }
    
    public void Track_CustomAdImpression(MaxSdkBase.AdInfo adInfo)
    {
        double revenue = adInfo.Revenue;
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("ad_platform", "AppLovin"),
            new Firebase.Analytics.Parameter("ad_source", adInfo.NetworkName),
            new Firebase.Analytics.Parameter("ad_unit_name", adInfo.AdUnitIdentifier),
            new Firebase.Analytics.Parameter("ad_format", adInfo.AdFormat),
            new Firebase.Analytics.Parameter("value", revenue),
            new Firebase.Analytics.Parameter("currency", "USD"),
        };
        Firebase.Analytics.FirebaseAnalytics.LogEvent("custom_ad_impression", impressionParameters);
    }

    /// <summary>
    /// Triggers when clicked on smth.
    /// </summary>
    /// <param name="adInfo"></param>
    public void Track_CustomAdClick(MaxSdkBase.AdInfo adInfo)
    {
        double revenue = adInfo.Revenue;
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("ad_platform", "AppLovin"),
            new Firebase.Analytics.Parameter("ad_source", adInfo.NetworkName),
            new Firebase.Analytics.Parameter("ad_unit_name", adInfo.AdUnitIdentifier),
            new Firebase.Analytics.Parameter("ad_format", adInfo.AdFormat),
            new Firebase.Analytics.Parameter("value", revenue),
            new Firebase.Analytics.Parameter("currency", "USD"),
        };
        
        FirebaseAnalytics.LogEvent("custom_ad_click", impressionParameters);
        SetUserProperties();
    }

    public void Track_CustomAdFail(MaxSdkBase.AdInfo adInfo, string reason = "dismiss")
    {
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("ad_source", adInfo.NetworkName),
            new Firebase.Analytics.Parameter("ad_unit_name", adInfo.AdUnitIdentifier),
            new Firebase.Analytics.Parameter("ad_format", adInfo.AdFormat),
        };
        
        FirebaseAnalytics.LogEvent("custom_ad_fail", impressionParameters);
        SetUserProperties();
    }
    
    public void Track_LevelStart(string level)
    {
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("level", level),
        };
        
        FirebaseAnalytics.LogEvent("level_start", impressionParameters);
        SetUserProperties();
    }
    
    public void Track_LevelEnd(string level)
    {
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("level", level),
        };
        
        FirebaseAnalytics.LogEvent("level_end", impressionParameters);
        SetUserProperties();
    }

    /// <summary>
    /// Tracks item transactions like money, etc.
    /// </summary>
    /// <param name="itemName"></param>
    /// <param name="changeAmount"></param>
    /// <param name="balance"></param>
    /// <param name="source"></param>
    public void Track_ItemTransaction(string itemName, int changeAmount, int balance, string source = "shop")
    {
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("item_name", itemName),
            new Firebase.Analytics.Parameter("change_amount", changeAmount),
            new Firebase.Analytics.Parameter("balance", balance),
            new Firebase.Analytics.Parameter("source", source),
        };
        
        FirebaseAnalytics.LogEvent("item_transaction", impressionParameters);
        SetUserProperties();
    }

    /// <summary>
    /// Tracks daily rewards etc.
    /// </summary>
    /// <param name="id"></param>
    /// <param name="giftName"></param>
    /// <param name="giftValue"></param>
    /// <param name="loginStreak"></param>
    public void Track_DailyGift(string id, string giftName, int giftValue, int loginStreak)
    {
        var impressionParameters = new[]
        {
            new Firebase.Analytics.Parameter("daily_gift_id", id),
            new Firebase.Analytics.Parameter("gift_name", giftName),
            new Firebase.Analytics.Parameter("gift_value", giftValue),
            new Firebase.Analytics.Parameter("login_streak", loginStreak),
        };
        
        FirebaseAnalytics.LogEvent("daily_gift", impressionParameters);
        SetUserProperties();
    }

    public void Track_TutorialPlayed(string tutorialName)
    {
        FirebaseAnalytics.LogEvent("tutorial_played", new Parameter("tutorialName", tutorialName));
        SetUserProperties();
    }
Leave a Comment