Continuing from Part 1 we want to extract data from Google Analytics and use it to enrich records for leads in Salesforce that have been created via a WebToLead process. For that we will use the respective APIs of GA and Salesforce.
API is short for Advanced Programming Interface. An API is a specification on how to send a request to an application in a way that solicits a useful response. Being a specification APIs are not bound to a specific language. The client – the program that makes the requests and evaluates the responses – can be written in one of a variety of languages (or even in a mix of different languages). We will get back to APIs in a later installment, first we need to set up the environment in which we will write and execute the program.
The Command Line Interface
This tutorial will make use of the CLI, the command line interface, a.k.a “terminal” (think of the terminal as a program that allows you to access the CLI; however I aim for practically rather than complete accuracy, and for all practical purposes CLI and terminal are the same and will be used interchangeably). The Terminal is an environment in which you can run scripts and programs that do not require a graphical interface. I have a Mac which is a Unix system, and frankly I will assume that you run some Unix-type system, too. Windows comes with two different CLIs, the generic command prompt and the Windows Powershell, a programming interface for a proprietary Microsoft scripting language (but you can access commands in other languages via the Powershell). I will focus on the Mac here, but the script we eventually come up with will also run on windows.
You can pass “arguments” to a CLI script (i.e. values that the script is supposed to process) via the “standard input”, or “stdin”. You can inspect the results via the “standard output”, or “stdout” (and there is also the standard error “stderr”, which by default looks like a specialized stdout). The terminal supports various scripting/programming languages (again there is a difference between scripts and programs that is not relevant to this tutorial, so there). There is no setup for the CLI involved (it’s there by default), but we need to add support for the programming language we intend to write the program in. We are going to use Python, so having python installed is the first prerequisite.
According to its documentation “Python is a clear and powerful object-oriented programming language, comparable to Perl, Ruby, Scheme, or Java”, which is not an explanation at all. it has some features that make it very well suited for this tutorial:
- it is free
- it can be executed on a large number of operating systems, so it’s easy to share programs between different computers
- Python is used a lot with Google projects, at least in part because it is traditionally the language for mathematical and statistical applications and “Big Data”
- fluent Python is not particularly easy (I am not fluent), but you can do a lot with very simple programs (it’s a bit like english for foreign speakers in that respect)
The reason for the latter is that other people have done much of the heavy lifting for you. Python, like other languages, has the concept of “libraries” that extend to original language with new capabilities. Python comes with a “standard library” that adds support for many basic operations (reading and writing files, access to operating system services, internet protocols etc.). There are other, external libraries, some of them building upon the modules of the standard library, some of them replacing standard modules with (hopefully) better versions, and some adding new features altogether.
Python exists in two actively developed branches (2/3) – current versions are Python 2.7.11 and Python 3.5.1. This tutorial uses the 2.7.x branch but should be easily adaptable to 3.x versions.
Most Unix machine should already have python installed (or have a package manager that lets you install Python), but if you are on Windows or if you are looking for a specific version you can go to the official downloads page.
With Python you write a program as a text file and then pass it to the Python interpreter that runs the program. So you that you do not have to give the full path to the interpreter every time you want to run your script you should add it to your environment variables so you can simply call the name (i.e. “python”). On a Unix machine your package manager might have already done this, for Windows there is nice installation tutorial at How to Geek that also talks about the necessary configurations (for a different version, but the process will be the same).
Now, without more ado –
Well, actually a little more ado. To write a program in Python you need a text editor. It does not matter which one (Notepad or Notepad++ on Windows, Vim/Emacs/Vi/Nano/etc. on Unix, on my Mac I use the Atom editor for small programs). It is however important that the files are saved as text only, without hidden characters or meta information.
Without more ado –
The traditional program in any new language is a program that prints out “Hello World” (in our case to stdout, see above). So create a new text file, type in
print "Hello World"
and save it as helloworld.py (the file extension is useful to recognize the file as Python script and in Windows it might be necessary to get the file processed as python).
Now open a the Terminal – on a Mac click the spotlight search icon and type “terminal” (and hit enter), on Windows – well admittedly I’m not sure, but apparently in Win10 the command prompt can be accessed via a “Power Users menu“.
On my command line I now type the name of the interpreter that runs the program, and a whitespace and then the name of my program. The resulting output should look something like this:
And there you have your first working Python program. Don’t worry, things are going to get more complex. But before they do, the next part of the series takes a lot at the necessary authorization for Google Analytics and Salesforce if we want to export/import data.
Tutorial Table of Contents