Analytics JavaScript Examples

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);

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?

Hi @YoMucka_1 ,

Have you read the thread about funnel examples, Funnel Examples - Unity Services - Unity Discussions The second example on that page is probably the one you would want to read as it includes a little piece of code.

2 Likes

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. :stuck_out_tongue:

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.

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. :slight_smile:

2 Likes

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…

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:
2594715--181589--Screen Shot 2016-04-13 at 11.48.53 AM.png

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

        var params = new System.Collections.Generic.Dictionary.<System.String,System.Object>();
        params.Add("totalKills", GetComponent(LevelS).score);
        params.Add("totalCoins",  GetComponent(LevelS).gold);
        params.Add("playedTime",  Time.timeSinceLevelLoad);
        var returnVal = Analytics.Analytics.CustomEvent("gameOver", params);

        Debug.LogError(returnVal + " SCORE: " + GetComponent(LevelS).score + " GOLD: " + GetComponent(LevelS).gold);
        Analytics.Analytics.CustomEvent("gameOver", params);

Sometimes I will get returnVal (NotInitialized)

@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?

I got it working, is this the proper way to do it? I put this into the update function

    if( health <= 0){
        if(alive == true){
            //var play = GameObject.FindWithTag("Player");
            //kill the player
            alive = false;
            gun.GetComponent(gun2).firing = false;
            gameSet.dead = true;
            GetComponent(CharacterController).enabled = false;
 
            var soldieraniOB = GameObject.FindWithTag("soldier").GetComponent(jsanimat);
            soldieraniOB.dead=true;

            var params = new System.Collections.Generic.Dictionary.<System.String,System.Object>();
            params.Add("totalKills", GetComponent(LevelS).score);
            params.Add("totalCoins",  GetComponent(LevelS).gold);
            params.Add("playedTime",  Time.timeSinceLevelLoad);
            var returnVal = Analytics.Analytics.CustomEvent("gameOver", params);

            Debug.LogError(returnVal + " SCORE: " + GetComponent(LevelS).score + " GOLD: " + GetComponent(LevelS).gold);
            Analytics.Analytics.CustomEvent("gameOver", params);
        }
    //get the time when the player dies
        deathTime = Time.time;
    }
1 Like