Google Analytics for Defold

Getting a good understanding of what your users are doing in your game and how often they come back to play can be critical to the success of a game. Defold provides key metrics such as daily users, installs and retention out of the box through the project dashboard. Additionally the Facebook SDK provides the facebook.post_event() function with which it is possible to create user funnels and drill down into user behaviour.

The above two solutions are often enough, but sometimes you want even more data. Perhaps you wish to track application crashes, load times or device specific metrics, in which case Google Analytics is a good choice. Google Analytics support in Defold has been requested both here on the forum and on Slack, but up until now there has been no implementation of the Google Analytics Measurement Protocol for Lua and Defold. With that in mind I decided to create an implementation that wraps the Google Analytics Measurement Protocol so that you guys can focus on making awesome games while quickly adding tracking in key parts of your game.

The implementation is provided as a Defold library project and it is available on GitHub: https://github.com/britzl/defold-googleanalytics

Enjoy!

23 Likes

:scream: :scream: :scream:

Fantastic, thanks!!!

3 Likes

Thanks!

You have a small typo on README in section USAGE: 13 and 20 lines, g o.get -> ga.get probably)

3 Likes

Thanks! I’ve updated the readme.

2 Likes

@britzl
Thank you for your work!
It’s really cool.

It’s working on desktop without problem, but on android I recive next error:

ERROR:SCRIPT: googleanalytics/internal/file.lua:43: unable to generate a unique filename
stack traceback:
	[C]: in function 'tmpname'
	googleanalytics/internal/file.lua:43: in function 'save'
	googleanalytics/tracker.lua:31: in function 'get_uuid'
	googleanalytics/tracker.lua:58: in function 'create'
	googleanalytics/ga.lua:37: in function 'get_default_tracker'

Really? Thanks, I’ll look into it immediately.

1 Like

I try to

print(os.tmpname())

and recive same error, i don’t know why, but os.tmpname() doesn’t work for me

Yup, I wonder if it’s an Android OS version thing. Testing right now.

My device:
xiaomi redmi note 2
android 5.0.2 LR22G

I’ve released a new version with a fix that doesn’t rely on os.tmpname(). Let me know if you have any issues with it!

1 Like

Thank you!!! :blush:

@britzl
now app started without bug!
but I recive next error (I tested app using dmengine - maybe it’s a problem?)

ERROR:SCRIPT: HTTP request to 'https://www.google-analytics.com/batch' failed (http result: -1  socket result: -29)

-29, that is RESULT_CONNREFUSED. Can you reach https://www.google-analytics.com/batch from a browser or using curl?

curl https://www.google-analytics.com/batch

yes, same error RESULT_CONNREFUSED
but from other phone or from mac - all fine, I think it’s some android “improvements” from china developers =)

@britzl And one more question:


it’s all time desktop, is it possible to fi change that?

Hmm, odd. The library is using “app” as data source. I would have expected this to result in hits being categorised as mobile. I’ll look into it. Perhaps I need to set a user agent or something.

1 Like

Ah, yes, the user agent is used to compute “browser, platform, and mobile capabilities”.

1 Like

Hmm, the non-realtime data that is collected seems to be correct. I see only Android and iOS visitors. Manually setting the User-Agent does nothing in the real time view… very strange.

1 Like

Doh, I’m a klutz. I created a user agent string but never actually used it… I’ve now added a user_agent module that will generate a reasonable user agent string per supported platform. The fix is included in version 1.2 of the library.

1 Like

Cool! Thank you!
As I remember @Deividas_Krunkauskas asked question about this issue, too. (in slack chat)

1 Like

When I prepared for GDC Competition I found an issue with library.
HTML5 build show an error in console :

Refused to set unsafe header “User-Agent”