Google Analytics

Discussion and feedback on Construct 2

Post » Mon Apr 28, 2014 2:11 am

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:

Code: Select all
<script>

var scirraAnalytics = (function(){

   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
   })(window,document,'script','//www.google-analytics.com/analytics.js','__gaTracker');

   __gaTracker('create', 'UA-********-**', 'example.com');
   __gaTracker('send', 'pageview');

   function externalLink(url){
      try{
         __gaTracker('send', 'event', 'External Link', url);
      }catch(err){}
      setTimeout(function(){
         window.location = url;
      },300);
   }

   function fireEvent(category, action) {
      try{
         __gaTracker('send', 'event', category, action);
         console.log(category + " - " + action);
      }catch(err){}
   }

   return {
      externalLink: externalLink,
      fireEvent: fireEvent
   }

})();
</script>

(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: Select all
"scirraAnalytics.fireEvent('Level " & Level & " Completed', 'Score: " & Score & "')"


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:

Code: Select all
"scirraAnalytics.externalLink('https://www.scirra.com/')"


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!
B
30
S
7
Posts: 23
Reputation: 1,871

Post » Mon Apr 28, 2014 4:17 am

This is great! Thanks for the post, @mbe, I wanna take advantage of this soon. :)
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,826

Post » Mon Apr 28, 2014 6:31 am

Thanks @mbe
B
157
S
27
G
17
Posts: 910
Reputation: 32,588

Post » Sun May 11, 2014 10:45 pm

It works with cocoonjs ?
B
5
S
1
Posts: 8
Reputation: 513

Post » Tue May 13, 2014 5:13 am

@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!
B
30
S
7
Posts: 23
Reputation: 1,871

Post » Wed May 14, 2014 2:07 am

Thank for reply @MBE , i'll try with cocoonjs, if it work i may post a tutorial :D
B
5
S
1
Posts: 8
Reputation: 513


Return to Construct 2 General

Who is online

Users browsing this forum: Tinimations and 7 guests