refresh analytics: rest api
CONTENTS
1. Introduction 2. Version 3. API Access Prerequisites 4. PHP Library 5. Time Stamp Requirements 6. API Requests 7. Authentication 8. XML Format Returned 9. Application Management Methods 9.1 aplication.list 9.2 application.getInfo 9.3 application.getCode 10 Analytic Reporting Methods 10.1 analytics.getSummary 10.2 analytics.getHistorical

Introduction

The Refresh Analytics REST API allows application owners and application / advertising networks access to their applications' analytics without using the Refresh Analytics Facebook application directory. The API supports both application management & analytics reporting. The interface is a REST API over a HTTP GET or POST request and returning XML data.

Version

This is RELEASE 1.0 of the Refresh Analytics REST API specification.

API Access Prerequisites

Before accessing the Refresh Analytics REST API, the application owner or ad network representative needs to contact Refresh at info@refreshpartners.com to enable API access and receive a shared secret used to sign API requests.

PHP Library

Refresh has made a PHP client library available to access the REST API. If you are using this client library, then you only need to review the API Methods documentation.

Time Stamp Requirements

A valid time-stamp (using the 'time' URL argument) is mandatory for all requests. Furthermore, the client time-stamp must be within 15 minutes of the Refresh Analytics REST API server time when the request is received. If not, the request will fail with the API_ERROR_BAD_TIME error status code.

The intention of this restriction is to limit the possibility that intercepted requests could be replayed by an adversary.

The argument's format is that of a unix timestamp, which is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Please note that the time value needs to be a GMT timestamp!

API Requests

All REST calls are made in a similar fashion. A HTTP GET or POST request is sent to api.refreshanalytics.com with at least five URL arguments; method, time, uid, version and sig.

http://refreshanalytics.com/api?method=$method&time=$timestamp&uid=$uid[&parameters=...]&sig=$hash

Authentication

All API calls use a signature mechanism to both authenticate the user as well as sign the entire request to make sure that it wasn't modified in transit.

The process of signing is as follows.

XML Format Returned

The XML result returned will have the following format:

<?xml version="1.0" encoding="UTF-8"?>
<response>
 	<error>0</error>
	<method>##method##</method>
	<##objects## list="true">
		<##object##>
			<##object_field1##>##object_data1##</>
			<##object_field2##>##object_data2##</>
			...
		</##object##>
		...
	</##objects>
</response>

When an error is returned, the following format will be used:

<?xml version="1.0" encoding="UTF-8"?>
<response>
 	<error>##error_number##</error>
	<error_msg>##error_msg##</error_msg>
	<method>##method##</method>	
</response>

Application Management Methods

application.list

Description

Returns a list of applications that the user has access to as either a developer or as a viewer.

Parameters

Returns

Returns an array listing each application's name, api_key and other information.

<?xml version="1.0" encoding="UTF-8"?>
<response>
	<error>0</error>
	<method>application.list</method>
	<application_list_response list="true">
		<application>
			<api_key>. . .</api_key>
			<api_id>. . .</api_key>
			<name>Test application</name>
			<developers>321, 654, 987</developers>
			<viewers>123, 456, 789</viewers>
			<secret>/no</secret>
			<verified>yes</verified>
		</application>
		. . .
	</application_list_response>
</response>

Errors

API_ERROR_WRONG_VERSION (10)
Wrong API version
API_ERROR_BAD_TIME (8)
Bad time parameter; missing or not within 15 minutes of server gmmktime()
API_ERROR_UNKNOWN_USER (11)
Unknown User
API_ERROR_AUTHENTICATION_FAILED (1)
Authentication failed

application.getInfo

Description

Retrieves the information about an application that is stored with Refresh Analytics.

Parameters

Returns

Returns a single application's information, similar to that of application.list()

<?xml version="1.0" encoding="UTF-8"?>
<response>
	<error>0</error>
	<method>application.getInfo</method>
	<application_getInfo_response list="true">
		<application>
			<api_key>. . .</api_key>
			<api_id>. . .</api_key>
			<name>Test application</name>
			<developers>321, 654, 987</developers>
			<viewers>123, 456, 789</viewers>
			<secret>/no</secret>
			<verified>yes</verified>
		</application>
	</application_getInfo_response>
</response>

Errors

API_ERROR_WRONG_VERSION (10)
Wrong API version
API_ERROR_BAD_TIME (8)
Bad time parameter; missing or not within 15 minutes of server gmmktime()
API_ERROR_UNKNOWN_USER (11)
Unknown User
API_ERROR_AUTHENTICATION_FAILED (1)
Authentication failed
API_ERROR_NO_APP_ACCESS (7)
You do not have access to this application

application.getCode

Description

Returns the FBML/HTML or script code required to track the application's users demographic and analytic information

Parameters

Returns

If the application has a registered secret, then the following is an example of what is returned;

<?xml version="1.0" encoding="UTF-8"?>
<response>
	<error>0</error>
	<method>application.getCode</method>
	<application_getCode_response list="true">
		<code>
			&lt;fb:iframe src="http://demographics.refreshanalytics.com/get.php?api_key=. . ." frameborder="0" width="1" height="1" /&gt;
		</code>
	</application_getCode_response list="true">	
</response>

If no secret is registered, then the following would be an example of what is returned;

<?xml version="1.0" encoding="UTF-8"?>
<response>
	<error>0</error>
	<method>application.getCode</method>
	<application_getCode_response list="true">
		<code>
			#! /usr/bin/env python
			
			import urllib, md5
			
			def ra_getcode(uid, api_key, secret, session_key):
				url = 'http://refreshanalytics.com/demographics/get.php?api_key=%s&amp;uid=%d&amp;session_key=%s'
				file = urllib.urlopen(url % (api_key, uid, session_key));
				rs = file.readlines()
			
				if(len(rs) &lt; 4)
					return rs[0]
			
				sig = ''
			
				for i in range(2, len(rs) - 1):
					hash = md5.new()
					hash.update(rs[i].strip() + secret)
					sig += hash.hexdigest() + '+'
				
				return rs[0].strip() + 'sig=' + sig + '&quot;' + rs[1].strip()
			
			# you may need to change the uid, secret_key and session_key values 
			# to make this work in your environment
			
			print ra_getcode(facebook.uid, facebook.api_key, facebook.secret_key, facebook.session_key)
			
		</code>
	</application_getCode_response list="true">	
</response>

Errors

API_ERROR_WRONG_VERSION (10)
Wrong API version
API_ERROR_BAD_TIME (8)
Bad time parameter; missing or not within 15 minutes of server gmmktime()
API_ERROR_UNKNOWN_USER (11)
Unknown User
API_ERROR_AUTHENTICATION_FAILED (1)
Authentication failed
API_ERROR_NO_APP_ACCESS (7)
You do not have access to this application

Analytic Reporting Methods

analytics.getSummary

Description

Returns the summary report for an application or applications

Parameters

Returns

A call to analytics.getSummary() with a count of 3 and categories of 'country' and 'state' would produce data similar to the following:

<?xml version="1.0" encoding="UTF-8"?>
<response>
        <error>0</error>
        <method>analytics.getSummary</method>
        <analytics_getSummary_response list="true">
                <summary>
                        <api_key>. . .</api_key>
                        <category>country</category>
                        <total>239</total>
                        <values list="true">
                                <value>
                                        <name>Canada</name>
                                        <count>86</count>
                                </value>
                                <value>
                                        <name>Australia</name>
                                        <count>33</count>
                                </value>
                                <value>
                                        <name>United States</name>
                                        <count>26</count>
                                </value>
                        </values>
                </summary>
                <summary>
                        <api_key>. . .</api_key>
                        <category>state</category>
                        <total>235</total>
                        <values list="true">
                                <value>
                                        <name>Ontario</name>
                                        <count>54</count>
                                </value>
                                <value>
                                        <name>Western Australia</name>
                                        <count>17</count>
                                </value>
                                <value>
                                        <name>Ile-de-France</name>
                                        <count>16</count>
                                </value>
                        </values>
                </summary>
        </analytics_getSummary_response>
</response>

Errors

API_ERROR_WRONG_VERSION (10)
Wrong API version
API_ERROR_BAD_TIME (8)
Bad time parameter; missing or not within 15 minutes of server gmmktime()
API_ERROR_UNKNOWN_USER (11)
Unknown User
API_ERROR_AUTHENTICATION_FAILED (1)
Authentication failed
API_ERROR_NO_APP_ACCESS (7)
You do not have access to this application
API_ERROR_UNKNOWN_CATEGORY (6)
Unknown category

analytics.getHistorical

Description

Returns the historical report for an application or applications

Parameters

Returns

A call to analytics.getHistorical() with a count of 3, category of 'country', report of last31days and a time frame of 10 days would produce data similar to the following:

<?xml version="1.0" encoding="UTF-8"?>
<response>
        <error>0</error>
        <method>analytics.getHistorical</method>
        <analytics_getHistorical_response list="true">
                <historical>
                        <api_key>. . .</api_key>
                        <category>Country</category>
                        <dates>2008-06-26,2008-06-27,2008-06-28,2008-06-29,2008-06-30,2008-07-01,2008-07-02,2008-07-03,2008-07-04,2008-07-05</dates>
                        <values list="true">
                                <value>
                                        <name>United States</name>
                                        <counts>13,0,1,0,0,2,4,0,0,1,0,1,0,0,0,1,1,3,2,2,2,3,0,1,0,0,0,0,1,1,2,2</counts>
                                </value>
                                <value>
                                        <name>United Kingdom</name>
                                        <counts>0,0,0,1,2,1,1,3,5,2,1,5,2,1,2,0,2,2,0,1,1,2,1,0,0,0,0,0,1,1,0,0</counts>
                                </value>
                                <value>
                                        <name>Turkey</name>
                                        <counts>0,0,0,1,0,0,1,0,0,0,0,2,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0</counts>
                                </value>
                        </values>
                </historical>
        </analytics_getHistorical_response>
</response>

Errors

API_ERROR_WRONG_VERSION (10)
Wrong API version
API_ERROR_BAD_TIME (8)
Bad time parameter; missing or not within 15 minutes of server gmmktime()
API_ERROR_UNKNOWN_USER (11)
Unknown User
API_ERROR_AUTHENTICATION_FAILED (1)
Authentication failed
API_ERROR_NO_APP_ACCESS (7)
You do not have access to this application
API_ERROR_UNKNOWN_CATEGORY (6)
Unknown category
API_ERROR_UNKNOWN_TIMEFRAME (12)
Unknown time-frame/report; use LAST31DAYS, LAST26WEEKS, or LAST24MONTHS