Nethab Blog

Using PHP with Google Analytics API

Posted by Nethab at 5:35 PM

Since Google Analytics just released their API as a public beta, I decided to try and get it working with the PHP Zend GData library. I've seen several implementations using cURL, but the GData library seems ready-made for it, so I figured why not save some hassle.

Logging In

The first thing you have to do is login to the API service.

$email = "<email@domain.com>"; $pass = "<password>"; require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Gdata'); Zend_Loader::loadClass('Zend_Gdata_Query'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); $client = Zend_Gdata_ClientLogin::getHttpClient($email, $pass, "analytics"); $gdClient = new Zend_Gdata($client);

List of Accounts/Profiles

Now that we've logged in, we need to get a list of available Accounts/Profiles. Specifically we need to get the "profile ID" of the website we want to report on.

try { $results = $gdClient->getFeed("https://www.google.com/analytics/feeds/accounts/$email"); foreach ($results as $entry) { echo "Profile: " . $entry->title . " - Id: " . $entry->extensionElements[0] . "\n"; } } catch (Zend_Exception $e) { echo "Caught exception: " . get_class($e) . "\n"; echo "Message: " . $e->getMessage() . "\n"; }

Fetching the Data

Now that we've found our profile ID, we should decide which data we want to collect, and request it from Google's servers. I find it easiest to return it as CSV.

try { $dimensions = array("ga:region", "ga:city", "ga:latitude", "ga:longitude"); $metrics = array("ga:visits", "ga:pageviews"); $reportURL = "https://www.google.com/analytics/feeds/data?ids=<profile ID>&" . "dimensions=" . @implode(",", $dimensions) . "&" . "metrics=" . @implode(",", $metrics) . "&" . "start-date=<YYYY-MM-DD>&" . "end-date=<YYYY-MM-DD>"; $results = $gdClient->getFeed($reportURL); $titleRow = 1; // To output a row of column labels foreach ($results as $rep) { if ($titleRow) { foreach ($rep->extensionElements as $elem) { $titles[] = $elem->extensionAttributes["name"]["value"]; } echo implode(",", $titles) . "\n"; $titleRow = 0; } foreach ($rep->extensionElements as $elem) { $row[] = $elem->extensionAttributes["value"]["value"]; } echo implode(",", $row) . "\n"; } } catch (Zend_Exception $e) { echo "Caught exception: " . get_class($e) . "\n"; echo "Message: " . $e->getMessage() . "\n"; }

Labels: , ,


Geo Targeting vs. Language Targeting and Usability

Posted by Nethab at 8:51 PM

Target languages first, because the visitor might want information about a country other than their own.

Had a meeting with our VP of Marketing today about making our website "international". He (along with the VP of Sales) were adamant about breaking out the website by country, then by language. So for example you would choose France, and your language choice would be Français, and all the information on that page/site would be related to France.

Why that doesn't work

On the one hand your company has information specific to a country (ie. product X is only available in France, Germany, and Spain).

On the other hand, your visitor(s) have a specific language preference (ie. s/he only speaks French).

Let's say the visitors' company has offices in France, Spain, and Italy; and needs information about which products are available in those countries. If the "Products available in Spain" is only available in Español, our hypothetical French speaking visitor will not be able to find the information they need (resulting in a potential lost sale).

Arguably this places an artificial barrier between you and a prime demographic: multi-national companies.

That's not all

In a perfect world all country specific information would be available in every language, but even if that were the case, when you take into account the user experience of forcing the visitor to navigate through 3 different country specific sites (clicking to the country selection page, and then re-navigating to the product information) you're talking about a minimum of 3x the clicks to find their information (more if your country sites have a lot of pages).

Instead, if you first organize your site around the language(s) spoken by your visitors, you can reduce the number of clicks required to find information on your site. If you also take into account the ability to auto-detect the language of your visitors (using the browsers "Accept-Language" header), you can reduce the number of clicks further still.

In other words, you never know what your visitors are looking for, so don't limit them by translating country specific information into just that countries language(s). Also, by organizing your site around your visitors language, you will decrease the time and effort required to research your products.


Google Website Optimizer in an external JS file

Posted by Nethab at 11:27 AM

An external ".js" file can come in handy when you use a content management system, templates (or server-side-includes) and/or have a large site and want to test a large number of pages simultaneously. Thanks to some modifications to the GWO scripts made by ShoreTel it is now possible.

How-To

The first thing you will need to do is set up your experiment as normal (using the instructions provided by Google) in order to get your experiment "verified". I use "dummy" pages and the upload verification method so I don't have to mess with any real pages on my site.

Then you will take the section names you defined in your experiment above and "tag" the portions of your page with an ID parameter.

For Example: If your section name is "heading", change the ID of your heading tag like this:

<h1 id="heading">Original Headline</h1>

Any text or content within that tag will be replaced by the variations you defined for that section of the experiment.

The next step is to paste the scripts below into your <head>, substituting the experiment key you received (from GWO) for the control scripts, and the profile ID you received for the tracking scripts.

For Example:

<script type="text/javascript">
var _gwoKey = "XXXXXXXXX";
var _gwoUacct = "UA-XXXXXX-9";
</script>
<script type="text/javascript" src="/<your folder>/gwo.js"></script>

Download Sample

Labels:


Google Website Optimizer and XHTML

Posted by Nethab at 11:13 PM

Google Website Optimizer is not XHTML compliant.

If you're not familiar with the reasons for using XHTML and strict doctypes, then this is not the article for you. I'll just say that not using them will make your life more difficult than it has to be.

What makes GWO incompatible with XHTML 1.1

XHTML 1.1 does not allow <script> tags inside the <body> tag. Google Website Optimizer (GWO) uses <script> within the <body> to define the start of each "section" to test, and an unbalanced </noscript> tag to mark the end of the section.

Furthur, GWO uses document.write extensively. Not only to write the variations to the page, but also to load the control and tracking scripts. Remember that XHTML 1.1 only allows <script> tags in the <head>, which makes document.write useless since <head> is the only part of your page that doesn't get displayed in a browser.

Solution

The solution is to use the built-in DOM functions (like document.createElement and document.getElementById) to load the control scripts and manipulate the contents of the page. Not only are they supported by nearly all modern browsers, but any browser that doesn't support them will never know the difference and will display the original (non-optimized) page. In other words it fails gracefully.

The first thing you will need to do is set up your experiment as normal (using the instructions for utmx_section as provided) in order to get your experiment "verified" and ready to run. Then you will take those section names and identify the portions of your page that you want to test with that ID.

For Example: If your section name is "heading", change the ID of your heading tag like this:

<h1 id="heading">Original Headline</h1>

Any text or content within that tag will be replaced by the variations you defined for that section of the experiment.

The next step is to paste the scripts written by ShoreTel into your <head>, substituting the experiment key you received (from GWO) for the control scripts, and the profile ID you received for the tracking scripts.

For Example:

<script type="text/javascript">
var _gwoKey = "XXXXXXXXX";
var _gwoUacct = "UA-XXXXXX-9";
</script>
<script type="text/javascript" src="/<your folder>/gwo.js"></script>

Labels: ,


Subscribe to: Posts (Atom)