Accessing Google Analytics via the GoogleAPI v3 using PHP

Step 1 – Create an API project from:

Step 2 – Give your project Analytics access

Step 3 – Create a service account set of OAuth creds.:

Step 4 – Download your newly created .p12 key

Step 5 – Link to your google analytics account from the admin section and make a note of the account id

The email you add to your account is the email generated when created the service oauth key in step 4. You need to give this email account full permissions.

Make a note of the account id here as you will need it for step 7.


Step 6 -Install the key and Google API Client to your server

1 – Download the php from:
2 – You should already have the key from step 4.
3 – Decide on somewhere sensible to store the google code and key in your project. Away from the publicly accessible webroot.

Step 7 – Time to write/copy some code.


  1. Ensure all your files on the server are correct, ie writeable by Apache
  2. Ensure the path to your .p12 key file is correct!
  3. Ensure the 1st param in the $cred is the email google made for you in the oauth console
  4. Ensure that that email is in your google analytics account with full permissions
  5. Ensure that you correct the account is in the first param passed to $service->data_ga->get(
  6. Ensure you update your project name in the code: $client->setApplicationName(‘YOURPOJECTNAME’);
  7. All the above code assumes you are simply dumping all the keys and google code into the same folder as the php file itself. But a better idea might be to place the google api code and keys somewhere completely separate to this analytics php sample in step 7. That way you can write separate files to access other google api’s without mixing things up.

That is about it. That took a while to find all that information as the docs are fairly useless for a newcomer to the GoogleAPI world.

Private git repository on your own debian/linux machine with SourceTree

This is assuming you have a server instance running Debian/Ubuntu

Step 1 Create the user for git

This is where the git repository will live

Follow the usual prompts and keep note of the password

Step 2 Install Git onto your server

Step 3 Create a new git repository (I have called this one ‘myprojectname’)

Step 4 Create an authorized_keys in the /home/git/.ssh

This is to allow access via ssh keys, see this post:

If you have set your keys up correctly you should be able to ssh into your server from:
“ssh git@”

You should be asked to add to the known hosts, after which you should get logged straight in.

Step 5 Get and configure SourceTree

  1. Download the latest source tree if you have not already got it.
  2. Ensure you have registered with atlasian:  file > registration

Step 6 Clone your new git repository

File > Clone / New …  which should lead you to a popup dialog box that looks a little like
Simply change the source path to include your own server ip or domain name, and change the project name to what ever you choose (note that there is not .git extension required). When done click “Clone”. This will clone the new repository to the Destination path.

Step 7 Add the ssh key location to source tree.

Tools > Add SSH Key
Then simply navigate to the location of your local ssh key, aka…

Step 8 Add your name and email

Whilst in your new repository click,   Repository > Repository settings
Go to the advanced tab and enter your details (this is so your name goes against the commits), the popup should look a little something like:
That is it. You should now be able to use SourceTree on your own server. Please visit source tree guides for more on sourcetree.