Import GA Information to Salesforce – Part 5, Libraries (Packages) and finally Code

In the last installment I promised to finally show some code, and I will – but there is one last hurdle to clear. Part 2 mentioned libraries, and how they extend the capabilities of a programming language.

Calling the Google- and Salesforce libraries requires a lot of code. But this is highly reusable code that does not change no matter which application uses it. Both companies provide libraries so you simply can call ready-made functions with your own credentials to get access (and you can actually see that Python is not my first language; libraries in Python would be usually referred to as “packages”, and I will use both word interchangeably).

There are several ways to install the required libraries/packages:

For beginners I recommend the last method, mainly because the other two methods forced me to debug problems with the respective tools first while running the setup file worked out of the box. The libraries are installed system-wide, so the setup file needs a few extra permission. I ran them as administrator by using the sudo command in the termimal (i.e. “sudo python setup.py” and then providing a root password).

Libraries may depend on other libraries. For example the google API client relies on the http2 library to make http requests. The Simple Salesforce API client uses the requests library, which actually does the same thing as http2 (and both aim to replace urllib2 from the standard library). A well written library will in its setup file define and automatically install its dependencies, so you do not have to worry about that.

Installing the Packages

You will find the Google API Python client, which is available on Github  (if you don’t know or don’t use Git you can click the “Download zip” button in the upper right) or you can find it via the Python package index (make sure you select the correct version for your Python version).  Google has also very good documentation which is a lot more technical than this tutorial. So download, extract, open a terminal windows, go to the folder and run “sudo python setup.py”.

Similarly you find the SalesForce API client either on the Python homepage  or on Github . Install procedure is the same as above.

If you need any more information on how to install libraries Python has a documentation page dedicated to the topic.

The code, and how to run it

Below is finally the code. To run it you need to provide your credentials. Find the lines where it says “sfuser”, “sfpass” and “sftoken” respectively and replace the “xxx” with your actual credentials.

Fine below the line where it says “service_account_email” and enter the mail from your Google service account. Make sure that the same e-mail address is added as a user to the Google Analytics Account you intend to query. For “key_file_location” enter the location of the credentials file you downloaded in part 3 (I have placed the file into the same folder as the script, so in my case the filemane suffices). Find the line where it says “profile” (“profile” being a deprecated name for what is now called a “view” in Analytics, however the API methods still refer to profiles and so do I) and the ID of the data view you want to use. You find that in the GA admin section/view settings/basic settings/view id. Store the script in a folder on your computer. I have named it gasf-integration.py, but you can call it more or less whatever you want.

That’s it, you can now run the program. Open the terminal window, go to the folder where you have stored your script, type in “python gasf-integration.py” (or “python <your-filename>.py) and the script should start to work. If it works as planned it will print out some status messages to stdout, if not you will get an error message (don’t worry, you’ll notice the difference. Error messages look scary.). You can also make this script executable and run it without explicitly calling the python interpreter, but that’s something I’ll cover later when I step through the code to explain what it does.

Final word of warning: this code will run okay on your computer, but it is by no means production level code. It is not a demonstration of good Python programming principles (I would be the wrong person to write about that), it is a demonstration of a few basic ideas. And now let’s go.

Beware:  Currently this will not work (with the current version of the Google client lib, that is), since there have been changes that break below code. I’ll do my best to fix this asap.I have fixed that.

#!/usr/bin/python

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

import requests 
from simple_salesforce import Salesforce

def get_service(api_name, api_version, scope, key_file_location,
 service_account_email):

 credentials = ServiceAccountCredentials.from_p12_keyfile(
 service_account_email,
 key_file_location,
 scopes = scope
 )

 http = credentials.authorize(httplib2.Http())

 # Build the service object.
 service = build(api_name, api_version, http=http)

 return service

def get_sf_service(sfuser,sfpass,sftoken):
 sf = Salesforce(username=sfuser, password=sfpass, security_token=sftoken)
 return sf

def get_results(service, profile_id):
 return service.data().ga().get(
 ids='ga:' + profile_id,
 start_date='30daysAgo',
 end_date='today',
 dimensions='ga:dimension1,ga:source,ga:medium,ga:campaign',
 metrics='ga:sessions').execute()

def update_sf_lead(sf_service, row):
 try:
 sf_row = sf_service.Lead.get_by_custom_id('gaid__c', row[0]) 
 if sf_row:
 id = sf_row['Id']
 sf_service.lead.update(id,{'source__c': row[1] , 'medium__c': row[2] , 'campaign__c': row[3]})
 print "Lead updated from row with Id " + row[0]
 except Exception:
 print "Row with Id " + row[0] + " did not update anything"


def main():

 scope = ['https://www.googleapis.com/auth/analytics.readonly']

 sfuser = "XXXXXXXXXXXXX.XXXXXXXXXXXXXXX.com"
 sfpass = "XXXXXXXXXXXXXX"
 sftoken = "XXXXXXXXXXXXXX"

 service_account_email = "my-project@boreal-furnace-118319.iam.gserviceaccount.com"
 key_file_location = "client_secrets.p12"
 profile = "XXXXXXXXXXXXXX"

 sf_service = get_sf_service(sfuser,sfpass,sftoken)
 service = get_service('analytics', 'v3', scope, key_file_location,service_account_email)

 results = get_results(service, profile)
 if results.get('rows', []):
 for row in results.get('rows'):
 update_sf_lead(sf_service, row)

if __name__ == '__main__': main()

That’s it, really. If you made sure that the key field got recorded both in GA and SF, and if you have waited until GA had time to process the data and resolve campaign information then your SF leads will now be updated with the source, medium and campaign name information. Most of the magic is in the external libraries, the program itself ist short and sweet. The next and last installment will break it down into its separate steps to explain what each one does, but if you are only interested in the results then you might as well stop here.

And in case it’s not obvious, the script above is rather generic code. If you want to use it no kind of attribution whatsoever is required.

Tutorial Table of Contents

Leave a Reply

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