Google Analytics for Defold

#1

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!

19 Likes

Bring Me Cakes. Red Riding Hood Puzzle
#2

:scream: :scream: :scream:

Fantastic, thanks!!!

2 Likes

#3

Thanks!

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

3 Likes

#4

Thanks! I’ve updated the readme.

2 Likes

#5

@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'
0 Likes

#6

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

1 Like

#7

I try to

print(os.tmpname())

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

0 Likes

#8

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

0 Likes

#9

My device:
xiaomi redmi note 2
android 5.0.2 LR22G

0 Likes

#10

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

#11

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)
0 Likes

#12

-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
0 Likes

#13

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

0 Likes

#14

@britzl And one more question:


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

0 Likes

#15

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

#16

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

1 Like

#17

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

#18

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

#19

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

1 Like

#20

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”

0 Likes