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:
<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>
[/code:11dycmzk]
(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:
[code:11dycmzk]"scirraAnalytics.externalLink('https://www.scirra.com/')"[/code:11dycmzk]
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!