[SOLVED] How/Where to add Google Analytics to a PlayCanvas game?

I want to track how many people start my game and how many people complete it. I assume it’s best to use Google Analytics to track that data.

On GA, I’ve found this guide on how to add google analytics into javascript.

I’ve tried inserting that code tag into one of my PlayCanvas js scripts, and I’ve tried adding it to a html asset and then loading that html asset as described here.

In either case, I get the error message “ga is not defined” when trying to log an event.

How do I define the global “ga” value as described in the Google guide, in order for me to log events?

Thanks in advance!

1 Like

Hi @Alice_Ruppert,

I haven’t tried that recently, but I don’t see any reason why it shouldn’t work. Two things you should take care to have in place:

  • When you add the GA code to a .js script first remove the boilerplate code Playcanvas puts in place. That .js file should have the raw GA code only and nothing else, no initialize or update methods.

  • Make sure that .js file is on top of your project’s script loading order queue, or at least before any other file using the GA global var:


And alternative to this if you are already hosting files elsewhere is to use an externally hosted .js script. Playcanvas now supports loading external scripts:

Hope this resolves your issue.


Hi Leonidas, thanks for the quick response.

Apparently the issue was that I tried to add the GA code tag to an existing script instead of creating a new one with only that code in it.

I now don’t have any errors anymore, thank you!

Am waiting for the events I logged to actually show up in my analytics dashboard now to see if it really works.



I tried many steps, including @Leonidas recommendation if no success. May some please provide a step by step guide for this. Having Google Analytics is very important for our company.

Best regards,

Which part is not working/that you are having trouble with?


Everything man, I cannot receive events on Google Analytics despite the analytics.js appearing on Network tab (Ctrl+Shift+I). I tried:

1 - Creating putting the gtag async script in the header part of the HTML file within Playcanvas
2 - Loading the analytics.js directly form google using the external scripts link in the setting menu
3 - Extracting the raw analytics js from google and importing into playcanvas so it loads when the experience loads

May someone please provide the steps one has taken in order for it work? Maybe have a sample project? THis is something that would be very useful for users and the developer community.

Thanks a lot!

BTW this is my project:

This is what I have done and the event comes through for me in the dashboard: https://playcanvas.com/editor/scene/855834


1 Like

My friend, you’re a god! After that I was able to add the following to enhance data collection:

ga('send', {
  hitType: 'event',
  eventCategory: 'WebAR',   //Type of application
  eventAction: 'THC08',  //Floorplan
  eventLabel: ''   //Version
ga(function(tracker) {
  var clientId = tracker.get('clientId');   //Very important!!!


This thread was very helpful to me, but I wanted to add some info for anyone looking to use gtag.js instead of ga.js. It’s also pretty simple.

Using the info from this link: https://developers.google.com/analytics/devguides/collection/gtagjs

Just add the async script with your own analytics ID to your External Scripts listing:

Then create a new script, I called mine gtag.js, and remove all the PlayCanvas boiler plate code. Just add the next four lines provided at the link:

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID');

Seems to work well when I go and at a gtag call to any useful spot in my project.

1 Like

Thanks @Travis for sharing it!

1 Like

I tried following both approaches in this thread but couldn’t get anything to work.

I created a property in my google analytics account. Set a name, set the default url (points to launch.playcanvas.com/projectId), set the industry category, clicked save.

I forked yaustar’s project in order to test it. replaced the code in ga.js with my property’s tracking id and hoped a pageview hit would show up. But it doesn’t.
From the dev tools console I can tell that the “ga” object is not defined. Copy pasting the code into the console creates the ga object and corresponding script tag which loads analytics.js.
After that I can issue the calls to ga without any errors. However no hits show up.

I tried setting up gtag as well based on Travis’s explanation too, but nothing happens that way either.

What am I doing wrong?

I am looking at the test project and don’t see any errors in console.

Can you add your usage example to debug please?

huh okay, so this is kind of weird but when I think about it it might just makes sense xD

The 1 user in the middle of the graph is you. The two right now on the right are me in 2 seperate google chrome tabs. The fact that they are chrome tabs is very important. My default browser is Brave… guess what is blocked by Brave … the browser which advertises specifically with privacy … of course! Google Analytics does track Brave.
The only weird part is that I don’t even see my console.log() before ga(‘create’ in brave. The entire file is blocked.

Thanks for helping and sorry for wasting your time


6 posts were split to a new topic: Google Analytics events are not being register on dashboard

A post was merged into an existing topic: Google Analytics events are not being register on dashboard

Has anyone tried integrating the new Google Analytics 4 tracking into their PlayCanvas project?

Hi, I am trying to do the same thing you did here and am having trouble with the last sentence you stated. How do I make a gtag call somewhere in my project where it will be activated? Previously in JS, I have used import/export to call functions outside of their own script but I understand this doesn’t work in PlayCanvas as it is an ES6 feature. How/where did you make your gtag call?

Thank you so much for the help!

Assuming that you’ve added the gtag script via External Scripts

You can add the gtag call to any script file, any where. It can be in a separate script in global scope or it can be in a initialise function of pc.script (as long as it is called once).

Hi, this is the method I used and it works great! (Referring to the gtag method in case it isn’t clear in this thread) But can you elaborate on what you mean by “gtag call to any useful spot in my project”? I’d like to create some kind of event during a particular button-push but I can’t figure out how to implement this. Thanks!

Hi, I think I figured it out. Isn’t it awesome when you can answer your own question a minute after you’ve posted it for the world to see? I added an event using this:
gtag(‘event’, ‘urlClick’);
which registers as an “urlClick” in google analytics.