With my new revamped website, I now have a desire to track where my website viewers are coming from, and how they are accessing my website.
The Google Documentation is extremely detailed, but spread across many sections and to those who aren't as code savy, it can come across as a daunting task trying to find out what information is useful or not. What also doesn't help is the Google Example documentation is wrong! (You’ll see in Part 2)…
So this is my documentation as to how I got the Google Analytics API to work for me:
In this section, I will be going through how to get the API set up and how to obtain the view IDs of the views you would want to get the data for.
- Create a Project
- Enable the API
- Create Credentials
- Adding to Google Analytics
- Obtaining View IDs
Create a Project
1. Go to the Developer Console (https://console.developers.google.com/)
2. In the top left hand corner, there will be a drop down option with the label Project. Click on the arrow and click 'Create Project'.
3a. This loads a pop up modal as shown below.
Give your project a name.
3b. (This is optional) But I find it is good practice to change the projectID and the app engine. Click on the edit button, this will load a input field where you can enter a projectID. Make it relevant to your project.
3c. (This is optional) Click on ‘Show advanced options…”. This will load a drop down list. Select where your website is hosted (In this example, I have selected ‘europe-west’)
4. Click Create and then this will create your new project. This may take a few seconds.
Enable the API
The next set of steps are to ‘Enable the API’. I did this by clicking the link below, which is from the Google Tutorial.
1. First, select what project you wish to register your application. The click Continue.
2. This will enable the API which will display the next message below. Next step is to get the credentials. (Click on the button which will take you there…)
This is where I got confused when creating my credentials as Google does its best to try and help the user point them in the right direction. Unfortunately, there wasn’t a ‘Java’ option but through trial and error, I have found a better way (which will bypass Google’s ‘help’).
1. On the credential’s page, you are presented with an interface which is Google trying to point their users in the right direction as to what credential they need. By using Java, we know we need a service account, so click on ‘service account’ which is in the smaller font as highlighted in the screenshot below.
2. This will take you to the ‘Service Accounts’ section. Click on ‘Create Service Account’.
3. This will load up a dialog box, where you need to fill in the followng information :
- Service Account Name
- Just give it a name
- This is a drop down list which allows ‘admins’ of a certain project delegate who can access what data in their project. As I am the owner of this project, I have selected ‘Project Owner’.
- Furnish a new private key
- This key will be what we use in the Java application to access the API. This will then ask what type of key do you wish, in this example, click on P12.
- Enable Google Apps Domain-wide Delegation.
- I checked this as it meant there would be no need for manual authorisation when accessing the data. This will then ask for a product name. Write this product name down.
4. Once all the information has been filled out, click on Create.
5. This will create a Service Account. This will automatically download the key file and provide a private key password. Note down this password. This password isn’t required when making the Java application, but would be required if you wanted to open the file on your machine.
6. The final thing you need is the ‘Service account ID’ – which is in the form of an email address available from the Service accounts page. Write this down also. This will be required in the Java Application.
You now have all the information you need to access the API from Java. Only things left are to give the service account access to the Google Analytics Profile / Views and then to get the View IDs
Adding to Google Analytics
1. Head over to Google Analytics.
2. Click on the Admin tab.
3. Depending on what level of access you wish for this service account to have is dependent on where you do this next action. As I am the owner of my website, and want this service profile to be added to all my properties and views. I have added my service account the ‘Account’ section, which will casade this access to all its child elements but one could easily add said service account to a particular Property or View.
Click on User Management (which ever one you decide upon)
This will load the User Management Settings option. There is a section that says ‘Add permissions for’. Copy and Paste the service account email you have into the input field. Then select the level of access this service account should be able to access. (In this example, I have selected Read & Analyze).
4. Click Add and this will then add the service account to the account/property/view.
Obtaining View IDs
1. Finally, to obtain the View IDs, ensure you have selected the correct Property and View to obtain the data that you wish (by changing the Property/View information in the drop down menus near the top of the page (outlined in blue).
2. Once you have done this, in the view section, click on ‘View Settings’ (outlined in red).
3. This will load the View Settings option. Here, under Basic Settings, write down the View ID, as you will need this information when constructing the Java application.
That’s all for Part 1. In Part 2, I will go through how to access the data through Java. Click here to go to the next post.