From The Blog: Announcing the Google Analytics plugin

Under the category of “The more you know,” we’ve increased our suite of analytics engine support with the Google Analytics plugin.

In addition to basics like the number of users and sessions, you get a wealth of demographic information about your users such as city, country, language, and “new” versus “returning” users. You also get detailed information like the app version, which OS it’s running on, the device type, screen resolution, and more. Finally, you can track custom events within the app, for example the user’s actions, which UI elements are interacted with the most, and more.

Setting up Google Analytics

To use Google Analytics, you must first set up an account with Google at www.google.com/analytics/. You can have up to 100 accounts under a single login, each account can have up to 50 properties , and each property can have up to 25 views. While each of your apps could be its own account, you’ll probably just need one account with each app as a unique property of it.

To begin, activate the Google Analytics plugin here.

Next, you’ll need to include the plugin within the app’s build.settings:

settings = { plugins = { ["plugin.googleAnalytics"] = { publisherId = "com.coronalabs", supportedPlatforms = { iphone=true, android=true, osx=true, win32=true } }, }, }

When you create a property in the Google Analytics dashboard — assigning the app name along with it — you’ll get a unique Tracking ID such as UA-12345678-1. You’ll need to pass both of these values to the googleAnalytics.init() API. For example:

local googleAnalytics = require( "plugin.googleAnalytics" ) googleAnalytics.init( "Mega Race 3000", "UA-12345678-1" )

Logging analytics events

While simply setting up Google Analytics will give you insight into various demographic and device-related details as mentioned above, you’ll probably want to gather specific data about the user’s actions within the app. Some examples include:

  • Checking how long users play the game. If the results aren’t meeting your expectations, you may consider adding incentives or additional features.
  • Checking how often — and how long — users play a specific level in the game.
  • Checking which options/settings are the most popular among a wide user demographic.

All of these examples involve logging events with Google Analytics. To log an event, simply call the googleAnalytics.logEvent() function:

googleAnalytics.logEvent( category, action, label )

The first parameter, category, is a required parameter used to create a “group” of events within Google Analytics. For example, a category such as "userAction" may be used to track actions performed by the user. Or, if your app interacts with a server and data is processed by the server, you might want a category named "serverAction".

The second parameter, action, is a required parameter which associates a specific action with the category. For instance, if the user interacts with a button in your game, you could log an action of "buttonPressed" in the "userAction" category.

The third parameter, label, is an optional parameter which lets you define additional information for the action, for instance the specific button which was pressed. For example, if the user pressed the “Play” button in your game, your googleAnalytics.logEvent() call might look like this:

googleAnalytics.logEvent( "userAction", "Button pressed", "Play" )

Ultimately, it’s your choice on which events should be logged, when, and why. We recommend a certain level of discretion in terms of which events/actions will be useful for analyzing user behavior in your app.

Tracking screens/scenes

The Google Analytics plugin features an additional tracking call which is used to track different screens. Google Analytics’ heritage is based on tracking website activity, which is “screen”-oriented, but the concept of screens can easily be compared to app scenes. This can give you insight on how frequently users visit particular scenes in your app, for example how often the “settings” scene is visited or whether users even view your “help” scene.

To track screen usage, use the googleAnalytics.logScreenName() function:

googleAnalytics.logScreenName( screenName )

This call simply requires a string which names your screen/scene. If you are using Corona’s Composer scene management library, you can simply add one line of code in the scene:show() function’s "did" phase to log the screen/scene name with Google Analytics:

function scene:show( event ) if ( event.phase == "did" ) then -- Log the screen/scene name with Google Analytics googleAnalytics.logScreenName( composer.getSceneName( "current" ) ) end end

Additional notes

Like any analytics engine, data isn’t very useful until you’ve accumulated enough to analyze. A majority of the reports you can view need “aggregated” data, although some information is available “live.”

If you intend to track user information, you will likely need to include a privacy policy  in your app or on your website so that users can learn about what is being tracked. If you require a privacy policy, please search the web for a template that can be adapted to your company/app.

In summary

As you can see, the Google Analytics plugin can be used to gain virtually any degree of insight about your apps, from user demographics to specific in-app usage patterns.

To learn more about the Google Analytics plugin, please proceed to our documentation, and if you have further questions, please visit the Corona forums.

View the full article

The logEvent function only sends  category , action and label.

How about the label value  (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ev)??)

This parameter is also important in application.

I have asked the engineers about this.

Rob

The logEvent function only sends  category , action and label.

How about the label value  (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ev)??)

This parameter is also important in application.

I have asked the engineers about this.

Rob