How to Use Google Analytics in a Flash Game

For this tutorial I will be using Flash Develop and the Google Analytics for Flash library. I will show how to use the trackPageView function to track player progress in your game. You will need to sign up for a Google Analytics account.

Setup Library
First, download Google Analytics for Flash. Copy the two .swc files from the lib directory in the zip into your project. In Flash Develop edit your project properties and under Compiler Options add the two .swc files to SWC Libraries.

Add Tracking Code to Project
In your AS3 class you will need to add the import statements, initialize the tracker, and call trackPageView.


import com.google.analytics.AnalyticsTracker;
import com.google.analytics.GATracker;
....
tracker = new GATracker(stage, analyticsCode, "AS3", false )
tracker.trackPageview("/FisherGirl");


Create the tracker by passing in a reference to the stage and your tracking code. The analyticsCode is your Google Analytics tracking code you can use any site for your tracking code. I am actually using the same tracking code for this blog as I use for the game. (Where can I find my tracking code?) Once the tracker is created you can call tracker.trackPageview anytime you want to send a new page view event to your Google Analytics. The page does not need to actually exist. For my game I send page views for each rod and a page view for finishing the game.

Setting Up a Goal
On the Website Profile page in GA click Edit next to the profile you are using for this game. Under Conversion Goals and Funnel click Edit to create a new Goal. Set the Goal Active to On. The Goal URL is the "page" you are going to use for game over or whatever you want to have the user finish. The Goal Name is just a readable title for that goal. Now enter the steps to reach that goal, so for my game I have the steps of starting the game and getting each of the rods. Leave out the final game over step, that will be included for you. Click Save Changes.

Today's Data
Most people (including me) assume that you only get data the next day because by default Analytics only shows up till yesterday. However, you can get today's data by opening the date range calendar and clicking on today's date.

Tracking Player Progress using Google Analytics

I've been trying to find an easy way to track the progress of players in my Fisher Girl game. Up till now I've been using Mochibot to track the number of people playing the game, but there is no information about how many players actually made it to the first rod or how many players come back and play again. Google Analytics is great for giving tons of details about your users for websites and can be used in a flash game to give the same information even if the game is hosted on multiple portals.

The latest version of the Fisher Girl game was created for mindjolt and includes the analytics tracking code. In the Fisher Girl game the fishing rod is the leveling mechanic. The better your rod level is the farther you can cast and the better fish you can catch. To win the game you must purchase all the rods and save the Fisher Boy. I setup a Goal in Google Analytics for tracking how many people go through all the steps to finish the game. The goal steps are:
  1. Game start
  2. Purchased silver rod
  3. Purchased gold rod
  4. Purchased legendary rod
  5. Game Finished saved fisher boy.
Using the goal I can track where users are dropping out of the game. Here is the Goal Funnel Visualization for three days on mindjolt.com:


Click the image to see full size.


So you can see how many people dropped out at each rod level. The worst area seems to be between level one and level two where only 34% of the players keep playing. The percentage of players progressing between each level is staying very consistent with only a few percentage points of difference each day. That should make it obvious if any changes increase player retention or not. For my next game update I'll focus on making that second part of the game more interesting and minimizing the frustration of losing lures to turtles.

Update (7/21/2009): After just moving the turtles near the second buoy down, so they wouldn't eat the lure as soon as it hit the water and removing one turtle, the average progress rate between level one a level two went up from 34% to 42% , which increased the game finish average from 6% to 10%.

Flash Game Development Tutorial 2: Loading assets

The first problem I ran across when trying to publish the Fisher Girl game onto portals was that I had used more than one file when building the game. I was dynamically loading in external assets like xml, png, and mp3. Most game portal sites only want one file, so all of those assets need to be embedded in one .swf file. Once you have a single swf your game can go viral because now when a portal sees your game and they can download it and host it easily.

In this tutorial I am going to be embedding assets from the Lost Garden Fishing Girl game, so you will need to download the .swf file from Fishing Girl:Game Prototyping Challenge. Of course you could use any swf that has linkage ids set.

Opening a Shared Library in FlashDevelop

Start a new AS3 project in FlashDevelop. Create a new folder inside the src folder called assets and copy the FishingGirl.swf into that folder. In FlashDevelop you should now see your assets folder and the swf file. Expand the swf file by clicking on it. You should see a small grayed out folder called Symbols, expand the Symbols folder. The items inside the Symbols folder that have a small image next to them are the symbols in the Flash file that have a linkage id set.

Embedding Assets

Open your Main.as file and put your editing cursor at the top of the class and then double click on the fishSmall1 symbol. This will auto fill in the Embed code for you. Directly under the Embed code type in a class name, so that your code should end up looking something like this:

...
public class Main extends Sprite
{
[Embed(source = 'assets/FishingGirl.swf', symbol = 'fishSmall1')]
private var FishSmall1:Class;
public function Main():void
...

To learn more about the Embed syntax: http://www.bit-101.com/blog/?p=853

Now when you compile your .swf the asset FarGround1 is actually embedded in your swf and can by referenced by the class name "FarGround1." To get a DisplayObject you can now call
var fish:DisplayObject = new FishSmall1();

However, It's too much work to always be referencing these assets as Classes, I'd rather be able to say something like SpriteManger.getSprite("fishSmall1") and get back a DisplayObject that I can put right onto the stage no matter how the fishSmall1 was embedded. My SpriteManager does just that. It is a singleton so it can be accessed anywhere and you can add a Class and give that class an id String. Then whenever you want a copy of that image you can just call getSprite with that String. Example:
//add the class to the SpriteManager
SpriteManager.getInstance().addClass("fishSmall1", FishSmall1);

//create a display object that can be shown on the stage
var fish:DisplayObject = SpriteManager.getSprite("fishSmall1");

//add to the stage
stage.addChild(fish);

So now you can embed an asset from a shared library swf and display it on the stage. Feel free to use the SpriteManager class in your project and if you find a better way to manage sprite assets in a code only Flash project let me know.

Flash Game Development Tutorial 1: Setting up FlashDevelop

I developed the Fisher Girl game as part of a prototyping challenge. The Lost Garden blog regularly holds game prototyping challenges and gives away a game design and graphics. So starting with the Fishing Girl design and graphics I created the Fisher Girl game (play it: http://www.nonoba.com/darkzak/fisher-girl).

I don't have a recent version of the Flash editor, so this game was developed entirely using FlashDevelop (http://flashdevelop.org) and the open source Flex (http://www.adobe.com/products/flex/) framework. Since this game was build completely using open source/royalty free tools, graphics, and music I am going to give back to the community by posting tutorials on how I built the game.

To follow these tutorials you will need:
Setup

Install FlashDevelop and unzip the flex sdk to somewhere simple you will remember (I installed to C:\tools\flex). Then open up FlashDevelop and in the main menu click Tools > Program Settings. In the left hand list choose AS3Context, then in the right hand list find the Flex SDK Location and enter the location you unzipped the Flex sdk to (On mine I entered C:\tools\flex).

Test Setup

You should be all setup to compile in AS3! Let's test it out.

In the main menu click Project > New Project..., choose AS3 Project, set the Name to be HelloWorld, select a location for the project, and select the Create a directory for project checkbox. After you click OK, you should see a new project created in the right hand box under Projects. Open up the src folder in the Projects area and open the Main.as file. FlashDevelop does a lot of the initialization code for you. Find the "//entry point" comment, this is where you first have access to the flash stage. Directly under the entry point comment paste the following code:
var output:TextField = new TextField();
output.text = "Hello World";
this.addChild(output);
You will need to add the following import statement: "import flash.text.TextField;"

To test your code hit CTRL+Enter or Project > Test Movie. You should see "Hello World" printed at the top of the movie!

In the next tutorial I'll show how to embed game assets from a swf or from png files into your game.