Google Analytics Java client

Stop wasting your life on stats checking. Google Analytics Data API can save you a lot of time. Have a doubt? What would you say if I said that you could get the stats you need in no time?


Make yourself familiar with API

Spend some time reading Google Analytics Developer’s guide Java API. Pay attention to Authentication chapter. I find it difficult to understand. Accounts & Profiles page can help you.

Play with samples

Download gdata-samples.java-1.31.1.zip. The AnalyticsClient class (gdata-samples.java-1.31.1/gdata/java/sample/analytics/) is everything you need to start playing with API.

1. Build jar

ant –f analytics.xml

(Directory gdata-samples.java-1.31.1/gdata/java/build-samples)

Include google-collect-1.0-rc1.jar and gdata-core-1.0.jar in classpath if you have troubles.

(File gdata-samples.java-1.31.1/gdata/java/build-samples/analytics.xml)

add-jars-classpath

2. Specify username and password

(File gdata-samples.java-1.31.1/gdata/java/build-samples/build.properties)

user-password-properties

3. Run

ant sample.analytics.run -f analytics.xml

Create your own client

1. Draw a line

I can hardly believe you need all stats about each of your websites. I never look into Browser Capabilities. But I always check Visits and Keywords. Same to you. Decide what data you really need and check if you can get it.

2. Grab profile ID

You don’t need to retrieve an account feed in order to obtain the list of profiles available. Open Google Analytics, click account you need, click Edit (Profile Settings) and write down ID (8-digit number).

Update May 24, 09. Add ga: prefix to 8-digit number, i.e. ga:XXXXXXXX.

google-analytics-profile-id

Tip: You can put IDs into a file/database or hardcode it.

3. Implement a client

The funniest part is coding. You can create a simple web application or a command line client in a day or two. API is very user-friendly. Just don’t make your application complex. Remember that stat checking is time consuming process that must be automated. The real benefit appears when many websites should be checked on a regular basis.

My Google Analytics Java client sample (find it here)

1. How to compile

Copy the following jar files from gdata-samples.java-1.31.1.zip to lib directory (or specify path to jars in the build.xml.properties):

  • gdata-analytics-1.0.jar
  • sample-util.jar
  • google-collect-1.0-rc1.jar
  • gdata-core-1.0.jar

Run ant.

2. How to run

You need to specify the following parameters in order to run the client:

  • Username – Gmail user name
  • Password – Gmail password
  • Profile ID – Profile ID from Profile Settings
  • Start date – Date range start date
  • End date – Date range end date
  • Report type – “visits” (number of visits) or “keywords” (all keywords and number of visits)

You can run the client from the command line:

java -classpath jar/gastats.jar;lib/gdata-core-1.0.jar;lib/gdata-analytics-1.0.jar;lib/sample-util.jar;lib/google-collect-1.0-rc1.jar Stats -usr <user> -pwd <password> -prf <profile> -str 2009-05-14 -end 2009-05-20 -typ visits

cmd-run-output

or ant (specify parameters in build.xml.properties):

ant run

ant-run-output

Cheers!

3 thoughts on “Google Analytics Java client”

Leave a Reply

Your email address will not be published. Required fields are marked *