Google Analytics

0 favourites
  • 6 posts
From the Asset Store
Google Analytics 4 Plugin for Construct 3 enables metrics and analytics for your games easily.
  • Hi Guys,

    As we use Construct 2 predominantly to make 'gamified' mobile ads for our clients, getting user engagement statistics is vitally important for us so we tend to put Google Analytics on most of the things we do.

    When I started using Construct 2, it quickly became apparent that just pasting the GA tracking code into the index.html file didn't work, so previously I had been using the callJS plugin (thanks Joe7!) to import an external JS file containing the tracking code. For reasons that elude me this only worked with the old ga.js analytics code, but it did allow me to fire off events when users started the game, got killed, clicked the more info button, etc.

    Last week Google finally took their new Universal Analytics system out of beta and are now not issuing the old tracking codes. As we have a campaign featuring a Construct 2 game that is about to go live, I was forced to find a new solution that works with the new code. I thought I would share my finding here partly to help anyone else who is having similar problems, but also to see if anyone else has a better solution.

    From my basic understanding of JavaScript, it appear that there is some sort of namespace pollution which is causing the issue, so I got around the problem but putting the tracking code inside a module and renaming the Google Analytics function. Putting this script in the index.html header did the job:

    var scirraAnalytics = (function(){
    	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    	__gaTracker('create', 'UA-********-**', '');
    	__gaTracker('send', 'pageview');
    	function externalLink(url){
    			__gaTracker('send', 'event', 'External Link', url);
    			window.location = url;
    	function fireEvent(category, action) {
    			__gaTracker('send', 'event', category, action);
    			console.log(category + " - " + action);
    	return {
    		externalLink: externalLink,
    		fireEvent: fireEvent
    (You will of course need to update the UA-********-** tracking code and URL)
    With this script I can now fire events off from within the game using the Execute JavaScript option in the Browser plugin , e.g,:
    [code:11dycmzk]"scirraAnalytics.fireEvent('Level " & Level & " Completed', 'Score: " & Score & "')"[/code:11dycmzk]
    By firing this at the end of each level we can see how far our users are progressing through the game and how much they've scored.
    I also put in an external link function in to record when our users click on an in game link to an external site. For anyone wondering you need to add a delay in to give Google time to record the event, hence the 300 milliseconds. It is used like this:
    In both function calls note the usage of both single and double quotation marks!
    Anyway I hope this helps anyone else who is having problems tracking how their games are being used and if anyone else has a better solution, please share it!
  • This is great! Thanks for the post, mbe, I wanna take advantage of this soon.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks mbe

  • It works with cocoonjs ?

  • jonathangoetz I'm afraid we only target mobile browser and in app webview users so I've no experience with any of the non web formats.

    I believe Google Analytics can be made to work with PhoneGap though so it's probably worth trying.

    I know Google Analytics doesn't like working in a local file that isn't served from a server, but the same is also true of Construct 2 games and obviously they work in CocoonJS!

  • Thank for reply mbe , i'll try with cocoonjs, if it work i may post a tutorial

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)