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:

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: