Part 2 talked about APIs, the programming interfaces that make data exchange between disparate application possible. This tutorial will use two specific APIs, the Google Analytics Core Reporting API to pull records from Google Analytics, and the Salesforce REST Api.
While the Google API is named for it’s function (Google Analytics has several APIs and this one returns reporting data), the Salesforce API is named after its’ architecture – REST ist the abbreviation for “Representational state transfer” and I invite you to read up the technical background on Wikipedia as it not really important here.
What is important is that you make requests to both APIs via http, and receive the response in kind – i.e. they use the same mechanism that a browser might use to display a website. You send a url with parameters, or a request with a body in a specified format, and you receive either data or an error message in return. The returned data will usually be a text-based format like xml or JSON.
Since Google Analytics does not want any arbitrary person to read your data, and Salesforce does not want them to write it both APIs require authentication.
First you need access to your Google Analytics Account. Actually that’s not quite accurate (as, by definition, you already have access to your account via your Google login) – it is the program we’re about to write that needs access.
For this Google has something called a “service account”, and in their really extensive documentation they explain the concept much better than I ever could. The practical offshoot of a service account is that it allows access to your Google Analytics account to a programm that runs on your computer or your server without requiring manual intervention like the standard Google Login.
To create a service account you need to go to the Google Developer Console and create a new project (which really is not harder than clicking “new project” and entering a name).
Google has a lot of services and therefore a lot of APIs. They will be disabled by default, and you need to enable the specific APIs you want to use. So, with your new project selected clicked “Use Google APIs”, and you’ll get a list with all available items (this might include APIs that are subject to a fee, however for the Analytics API a certain quota is free). Look for “Other popular APIs” and click “Analytics”. While there are several Google Analytics APIs (Core Reporting, Realtime, Management, Metadata) they are all enabled via the same link. In the following screen, click “Enable API”.
The API is now enabled for your project, but you need credentials to actually use it (i.e. you need login data that your program can use), so go to “Credentials”. When you first access this Google will show you some sort of wizard that helps you determine what kind of access you want to configure. For the Analytics API you want “other non-gui” access to “application data”, and you do not want to run this on the Google App Engine:
Following the suggestion of the wizard you will create a service account. Your need to enter a name (in my example “tutorial1”) and a “key type” – for the client library we intend to use choose “p12”:
This will give you two things: first a somewhat cryptically looking e-mail address, and secondly a download prompt for your key file (store it well, you can only download it once). The e-mail address is what your program will use as a username, so you need to add it to the Google Analytics account you want to query. The key files stores the actual credentials. It is not human-readable, but Google provides the necessary code to have it parsed by your program.
Now that we are done with Analytics we also need to get access credentials for the Salesforce API. First you have to make sure that your edition actually has API access. According to this knowledgebase article API access is enabled in Enterprise/Unlimited/Performance and Developer editions and can be enabled for a fee in the professional edition.
The Developer Edition is a special case – it allows Developers to log into a Salesforce account for free; this account cannot be used for productive work, but you can use it to test API integrations. This tutorial actually uses a free Developer Account.
The credentials for SF API access comprise three items: the e-mail address and password that you use to log in with your browser, and a security token that is mailed to you when you create your account or change your password (so when you change your password you will need to update your API integrations, too). The credentials will be stored in text format, so make extra sure nobody has access to the credentials file (e.g. if you run your program via a web server make sure to store the credentials outside the web root).
And that’s it, almost. In the next installment we will set up a few custom fields in Salesforce and set the key field that we need to join Google Analytics- and Salesforce data, before in the fifth part we will finally look at some code.
Tutorial Table of Contents