My favorites | English | Sign in

Faster apps faster - GWT 2.0 with Speed Tracer New!

Google Analytics (Labs)

Website Optimizer API - Getting Started

The Google Website Optimizer Experiment Management API is an extension to the Google Analytics Data API that you use to programmatically create and modify Website Optimizer experiments.

See the Google Analytics Data API Protocol document for general information about using Google Analytics feeds. In particular, the following sections are relevant to the Website Optimizer API:

The Website Optimizer Experiment Management API does not provide access to the experiment results. To see the experiment results, you must use the Experiment Report page.

The rest of this guide contains Quick Start instructions to using the Experiment API, as well as some Script Utilities you can use to simplify interacting with the API.

Quick Start

You can use cURL to get an authenticaiton token and to access the API from the cURL command line as a way to easily interact with the Website Optimizer Experiment API. See Using cURL to interact with Google data services for more information on using cURL. This section describes how to use cURL and ClientLogin.

Getting an Authentication Token

You can get an authentication token via ClientLogin (recommended for desktop or mobile applications), or AuthSub (recommended for web-based applications). In this example, we use ClientLogin:

curl https://www.google.com/accounts/ClientLogin \
 -d Email=name@gmail.com \
 -d Passwd=your-password \
 -d accountType=GOOGLE \
 -d source=GWO-curl-example \
 -d service=analytics

Substitute your email and password in the appropriate place (see "Password Handler" for advice on entering passwords on the command line). For the service name, we use analytics because the Website Optimizer Experiment Management API is an extension to the base Google Analytics Data API. The accountType must be GOOGLE.

Back

Running this command successfully results in 3 lines being returned each with the following names:

  • SID=
  • LSID=
  • Auth=
SID=DQAAAHYBADCv2pSv7nflacDNwz3zEDUGtrSvNVDcpkSfddi77b3U5sEaHmP8YLWhmA36F9rk85mL8J5dqo4apn0T1vKz0fPGI9Xtnuet6cuE2ZzYvrNIwbSC_HjTqF4zudNQnnlDuD2wqZT-g1qXI8KhGAQZV4NexHZoQPlabTsGuRZeIBxj1A
LSID=EUBBBIaBADCl-kNxvRVmcQghpt3cqSMfEooKR9flLOUZqwgP9OrZS83gse-KSdTNeXhxsET7FYenDhceP9lIPOmesH-t9qh-AWUHjjMdZEbUNeF9mWyzln6Z-FajaiG-cVFkqW0ZJ8ZbnCP30xXj6xFK6QxaAcqy_9Pej8jhEnxS9E61ftQGPg
Auth=EUBBIacAAADK-kNxvRVmcQghpt3cqSMfEooLNMflLNIQqwgP9OrZS83gs-KSdTNeXhxsET7FYePWmaD8Vsy1V4LSUGMUP48Je2TO8OcjBj6HgAtPhiZeX-gKDfagZDK44j4n-Tkb44nhOnp2_QPSnBj3Z2vYwOEDjjG3Q53aQVC2132JKOuGh

You will need the last line to retrieve data from the API. Specifically, the string after Auth= is the authorization token needed in your data request to the API. This token is valid for 14 days, so you don't need to (and you shouldn't) request a new token for each interaction with the API.

Accessing the API

Use the authentication token generated from the previous process to access the API. The token is included as part of your request. The following code illustrates all the components of the request as they need to be made to the Experiment API:

curl --silent \
 --header "Authorization: GoogleLogin auth=EUBBIacAAADK-kNxvRVmcQghpt3cqSMfEooLNMflLNIQqwgP9OrZS83gs-KSdTNeXhxsET7FYePWmaD8Vsy1V4LSUGMUP48Je2TO8OcjBj6HgAtPhiZeX-gKDfagZDK44j4n-Tkb44nhOnp2_QPSnBj3Z2vYwOEDjjG3Q53aQVC2132JKOuGh" \
 -L "https://www.google.com/analytics/feeds/websiteoptimizer/experiments"

Back

Script Utilities

This section describes two simple utilities that you can use to interact with the Experiment API.

Password Handler

The first utility you can create is the password handler. If you type your password directly into the account access command, it (along with your password) will be recorded in your command history. Since the command requires your password as an argument, you can maintain your password privacy by creating a small shell script that prompts for the account and password information.

#!/bin/bash
read -p 'Username: ' username
stty -echo
read -p "Password: " password; echo
stty echo
curl -d  "accountType=GOOGLE&Email=$username&Passwd=$password&source=GWO-curl-example&service=analytics"  https://www.google.com/accounts/ClientLogin | grep 'Auth=' | sed  s/Auth=// > ~/.gwoAuthToken
echo "Token: "
cat ~/.gwoAuthToken

Save this code in a file called getAuthToken.sh. When you run this script, it will prompt for your account/password and produce a file called .gwoAuthToken that contains your authorization token.

Back

Feed Script

Write the following script to simplify making a feed request and save it as getGwoFeed.sh. It will handle the authToken string and issue the request to the Experiments API:

#!/bin/bash
authToken=`cat ~/.gwoAuthToken`
url="https://www.google.com/analytics/feeds/websiteoptimizer/$1"
curl --silent --header "Authorization: GoogleLogin Auth=$authToken" -L "$url"
echo ""

Use the script as follows:

getGwoFeed.sh "experiments"

You can use two prettifying options to make the resulting XML easier to read:

  • getGwoFeed.sh "experiments?prettyprint=true"
  • getGwoFeed.sh "experiments" | tidy -xml -indent -quiet

The tidy command adds newlines and other formatting to the output. If you plan on capturing the output to use as the input for a POST or PUT operation (adding/updating a new experiment, section, variation or abpagevariation), then you should not use tidy to clean up the output because the newlines might break the anticipated syntax for a POST or PUT.

Back