Search Unity

Analytics JavaScript Examples

Discussion in 'Unity Analytics' started by mpinol, Oct 27, 2015.

  1. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Here are some JavaScript code examples while we are working on adding them to the Manuals!


    4.x - 5.1 SDK Integration
    import UnityEngine.Cloud.Analytics;

    Custom Events
    var params = new System.Collections.Generic.Dictionary.<System.String, System.Object>();
    params.Add("coins", 10);
    UnityAnalytics.CustomEvent("MyCustomEvent", params);

    Monetization
    UnityAnalytics.Transaction("12345abcde", 0.99, "USD", null, null);

    User Attributes
    var year = 1977;
    UnityAnalytics.SetUserBirthYear(year);


    5.2 Engine Integration
    # no import necessary

    Custom Event
    var params = new System.Collections.Generic.Dictionary.<System.String, System.Object>();
    params.Add("coins", 10);
    Analytics.Analytics.CustomEvent("MyCustomEvent", params);

    Monetization
    var year = 1977;
    Analytics.Analytics.SetUserBirthYear(year);

    User Attributes
    Analytics.Analytics.Transaction("12345abcde", 0.99, "USD", null, null);
     
  2. YoMucka

    YoMucka

    Joined:
    Jan 11, 2016
    Posts:
    2
    Hi I really need to use the funnel but don't get how to implement.

    I'm making a mobile game that has many levels. Each is 1 scene. I heard it's not a good way but anyway I'm making that way for the game at the moment. It's build with JS.

    Players must complete 1st level in order to proceed to 2nd level, it's linear.
    I want to track how many players proceeded to which level.
    I think that's a typical pattern you use the funnel.

    I'm using Unity 5.3, and already achieved the tutorial of custom event, although I have no idea what the dictionary means. I just set it to the button that loads the setting scene of the game.

    So to load a level-scene, players must push a button that has an attached JS script like this.

    --------------
    #pragma strict
    import UnityEngine.SceneManagement;
    private var textNum : int;
    textNum = int.Parse(this.gameObject.transform.GetChild(0).GetComponent(UI.Text).text);

    function SceneLoad()
    {
    //build index for the 1st level is 6.
    SceneManager.LoadScene( textNum + 5);
    }
    --------------

    I'm using no server so for now Unity Analytics is the only way I'm gaining progresses of players.
    Anybody knows how to implement custom code and how to set up in funnel GUI?
     
  3. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    YoMucka and sschan like this.
  4. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    Any chance of a similar post for IAP function, or simply having the IAP documentation have javascript parallels? It's currently C# only. I know, I'm the only dinosaur who hasn't switched to C#, this is my last project in Javascript I promise. :p
     
  5. mpinol

    mpinol

    Joined:
    Jul 29, 2015
    Posts:
    317
    Hey @MikaMobile,

    Currently it is not possible to use IAP functionality with JavaScript and I cannot really give you any real information about when/if it will be added.
     
  6. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    No worries, I actually figured out a pretty easy way to get my C# IAP stuff to talk to my javascript game logic, and I've already got it up and running. Gotta say, I'm impressed with both the IAP features and analytics, I went from knowing nothing about them to fully functional in like 2 days. :)
     
    erika_d and mpinol like this.
  7. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    Any JS examples for using AnalyticsResults?
    None of my events are showing in the validator.

    EDIT: One of my custom events is showing. The rest are not... need to investigate...
     
    Last edited: Apr 8, 2016
  8. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @cheezorg,

    Are you still having the issue with events not showing up in your validator? AnalyticsResults is a return value of an Analytics custom event, you should be able to wrap the return value in a print statement (in whatever language, but in Javascript's case Debug.Log()) and it will print the result. Here's an example:

    var params = new System.Collections.Generic.Dictionary.<System.String,System.Object>();
    params.Add("totalPotions", 5);
    params.Add("totalCoins", 100);
    var returnVal = Analytics.Analytics.CustomEvent("gameOver", params);
    Debug.Log(returnVal);

    Then in your console you will see one of the AnalyticsResults output:
    Screen Shot 2016-04-13 at 11.48.53 AM.png
     
  9. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Is their something separate I need to do to get it to work with Android?

    It seems to show up in the validator on PC but when I build to Android nothing shows up
    https://analytics.cloud.unity3d.com/integration this is the correct place to find the validator?


    Here is the code I am using to update on players death

    Code (JavaScript):
    1.         var params = new System.Collections.Generic.Dictionary.<System.String,System.Object>();
    2.         params.Add("totalKills", GetComponent(LevelS).score);
    3.         params.Add("totalCoins",  GetComponent(LevelS).gold);
    4.         params.Add("playedTime",  Time.timeSinceLevelLoad);
    5.         var returnVal = Analytics.Analytics.CustomEvent("gameOver", params);
    6.  
    7.         Debug.LogError(returnVal + " SCORE: " + GetComponent(LevelS).score + " GOLD: " + GetComponent(LevelS).gold);
    8.         Analytics.Analytics.CustomEvent("gameOver", params);
    Sometimes I will get returnVal (NotInitialized)
     
    Last edited: Nov 15, 2016
  10. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @Mike-Cotton

    The Validator will only show events sent from the Editor or Development Builds for Android and iOS.

    The NotInitialized result indicates that the Analytics system is either not initialized (called in Start or Awake) or is no longer initialzied (called in OnDestroy or OnApplicationQuit).

    Are you calling this code in the OnDestroy method?
     
  11. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    I got it working, is this the proper way to do it? I put this into the update function

    Code (JavaScript):
    1.     if( health <= 0){
    2.         if(alive == true){
    3.             //var play = GameObject.FindWithTag("Player");
    4.             //kill the player
    5.             alive = false;
    6.             gun.GetComponent(gun2).firing = false;
    7.             gameSet.dead = true;
    8.             GetComponent(CharacterController).enabled = false;
    9.  
    10.             var soldieraniOB = GameObject.FindWithTag("soldier").GetComponent(jsanimat);
    11.             soldieraniOB.dead=true;
    12.  
    13.             var params = new System.Collections.Generic.Dictionary.<System.String,System.Object>();
    14.             params.Add("totalKills", GetComponent(LevelS).score);
    15.             params.Add("totalCoins",  GetComponent(LevelS).gold);
    16.             params.Add("playedTime",  Time.timeSinceLevelLoad);
    17.             var returnVal = Analytics.Analytics.CustomEvent("gameOver", params);
    18.  
    19.             Debug.LogError(returnVal + " SCORE: " + GetComponent(LevelS).score + " GOLD: " + GetComponent(LevelS).gold);
    20.             Analytics.Analytics.CustomEvent("gameOver", params);
    21.         }
    22.     //get the time when the player dies
    23.         deathTime = Time.time;
    24.     }
     
    ap-unity likes this.