Download and Installation - GameAnalytics/GA-SDK-JAVASCRIPT GitHub Wiki

To download the SDK use this link or use the Download ZIP button from the repository.

Unzip the downloaded .zip and include this in your project (the files are located in the dist folder):

<script src="/path/to/GameAnalytics.js" />

or

<script src="/path/to/GameAnalytics.min.js" />

GameAnalytics.js includes local logging to the console and is 60Kb in size, while GameAnalytics.min.js is without logging and is 48Kb in size. It is recommended to start out with using GameAnalytics.js to quickly if you have instrumented the SDK correctly to send events and then you can switch to GameAnalytics.min.js in production if you need to save the extra kilobytes.

npm

If you use node packages you can also install the SDK via npm:

npm install gameanalytics

And use it like this:

var gameanalytics = require('gameanalytics');

gameanalytics .GameAnalytics.setEnabledInfoLog(true);

⚠️
NOTE: The SDK still expects to be run in a browser environment where the navigator object is defined

The JavaScript GA snippet

It is also possible to use the following snippet to asynchronously load GameAnalytics SDK when your website loads and still be able to configure, initialize and send events before the SDK is fully loaded (just like you can do with Google Analytics). The code should be added near the top of the tag and before any other script or CSS tags:

<!-- GameAnalytics -->
<script>
(function(w,d,a,m){var s='script';var g='GameAnalytics';w[g]=w[g]||function(){(w[g].q=w[g].q||[]).push(arguments)},a=d.createElement(s),m=d.getElementsByTagName(s)[0];a.async=1;a.src='http://download.gameanalytics.com/js/GameAnalytics-[VERSION].min.js';m.parentNode.insertBefore(a,m)})(window,document);

GameAnalytics("setEnabledInfoLog", true);
GameAnalytics("initialize", "GAME_KEY", "SECRET_KEY");
</script>
<!-- End GameAnalytics -->

The above code does four main things:

  1. Creates a <script> element that starts asynchronously downloading the GameAnalytics.js JavaScript library from http://download.gameanalytics.com/js/GameAnalytics-[VERSION].min.js (replace [VERSION] with the latest or desired version of the SDK)
  2. Initializes a global GameAnalytics function (called the GameAnalytics() command queue) that allows you to schedule commands to be run once the GameAnalytics.js library is loaded and ready to go.
  3. Adds a command to the GameAnalytics() command queue to enable info logging.
  4. Adds another command to the GameAnalytics() command queue to initialize the SDK (replace GAME_KEY and SECRET_KEY with your actual keys).

Alternative async GA snippet

While the JavaScript GA snippet described above ensures the script will be loaded and executed asynchronously on all browsers, it has the disadvantage of not allowing modern browsers to preload the script.

The alternative async GA snippet below adds support for preloading, which will provide a small performance boost on modern browsers, but can degrade to synchronous loading and execution on IE 9 and older mobile browsers that do not recognize the async script attribute. Only use this tracking snippet if your visitors primarily use modern browsers to access your site.

<!-- GameAnalytics -->
<script>
window.GameAnalytics=window.GameAnalytics||function(){(GameAnalytics.q=GameAnalytics.q||[]).push(arguments)};
GameAnalytics("setEnabledInfoLog", true);
GameAnalytics("initialize", "GAME_KEY", "SECRET_KEY");
</script>
<script async src='http://download.gameanalytics.com/js/GameAnalytics-[VERSION].min.js'></script>
<!-- End GameAnalytics -->

 

NEXT  →

⚠️ **GitHub.com Fallback** ⚠️