Tuesday, November 29, 2016

How to integrate google analytics API with php using service account!

Here you will get a step by step details of how to get API respose by querying GA API using service account.Integrating google analytics with php using service account is really very very simple if you follow the link  https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-php
you just need to be patient and understand every step very clearly.It took me a couple of days to figure out and implement everything successfully.


STEP:1.First you need to crete Google Analytic account and link to your website/blog you want to track. Here you can get the step by step detail/; http://codethecomplex.blogspot.com/2016/11/steps-to-add-google-analytics-to-your.html

For quick recap:Type google analytics in search bar





STEP:2. It will take you to the login page.Sign into your GA account.





STEP:3.It will take you to home page of Google analytic.Click on the Admin in Navigation Bar





STEP:4.Chose the right account from the drop down.Click on the Tracking Info under Property
Admin->Property->tracking info





STEP:5.Click on tracking code
Admin->Property->tracking info->tracking code






STEP:6.It will give you tracking Id and script copy it and add it to your website as shown in above link




Now install composer following the steps shown here http://codethecomplex.blogspot.com/2016/11/learn-how-to-install-composer-on-windows.html



STEP:7.Once the composer is successfully installed in system type google analytics php Integrating in search bar.Click on first link



STEP:8.It will take you to https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-php





STEP:9.Scroll down.Click on service account link  under Create a Client ID




STEP:10.It will take you on service account page.Click on select a project.





STEP:11.Enter a project name and click on create





STEP:12.It will take few second and create a project of given name




STEP:13.When the project is created it will take you to library page


type analytic in search bar.


STEP:14.Click on analytics API.It will take you on API Manager page





STEP:15.Click on Enable on API Manager page




STEP:16.It will enable analytic API for your project



go back to hello analytic page
https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-php



STEP:17.Scroll down.Click on service account link  under Create a client id




STEP:18.It will take you on service account page.Click on select a project.





STEP:19.Select the project you have created and click open






STEP:20.It will take you to IAM and Admin Page displaying service account ID.Copy the service account ID and paste it some where for later use






STEP:21.Right Click as shown and select Create Key





STEP:22.Select the key type as JSON and click on create





STEP:23.Make a folder say helloanalytic insde htdocs and save the JSON file there





STEP:24.New private key in JSON format is created.Click on Close





STEP:25.Login to your google analytic account





STEP:26.Click on Admin in Navigation Bar





STEP:27.Click on user Management under Account
Admin->Account->User Management
Paste the service account ID (copied in step 19)under Add Permission for.Select Read and Analyze from drop down Click on Add





STEP:28.It will give the read and Analyze permission to email id created





STEP:29.Go back on hello Analytic Page.Scroll down to step2: Install the Google client Library





STEP:30.Go to helloanalytic folder type cmd before address in address bar as shown
(remember composer is already installed in system as discussed above)






STEP:31.It will open the terminal as shown





STEP:32.Copy the command  :composer require google/apiclient:^2.0





STEP:33.Paste the command on terminal and press enter








STEP:34.It will start loading the dependencies required for this project





STEP:35.You will see the screen something like this






STEP:36.After the loading is complete you will see the project folder something like this





STEP:37.Go back on hello analytic page.Scroll down to step 3 https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-php




Download the helloanalytic.php



STEP:38.Save helloanalytic.php in helloanalytic folder





STEP:39.Right click and copy the name of JSON file





STEP:40.Open the helloanalytic.php page
Look for service-account-credential and replace it with json key file name





STEP:41.Start the xamp using XAMPP control Panel






STEP:42.Run the helloanalytic.php page




It shows some error


STEP:43.It's related to cURL not being able to get the above file. Providing it solves it but it's better to fix the main problem.Normally Guzzle used to provide a cacert.pem before, but they are not doing it .Libcurl on Windows  used to drive the curl extension for PHP can't access the Windows's certificate store. It needs this information however to check if the SSL certificates provided on https:// URL's are valid. Cacert.pem  basically contain most well-known certificate authorities, these are the companies giving out commercial SSL certificates. So if curl/libcurl/guzzle/... doesn't have any list of valid certificate issuers that it can trust, it will just tell you everything is untrusted. Right way to fix this issue is to provide the cacert.pem.



STEP:44.Click on the Link https://curl.haxx.se/ca/cacert.pem go to the page.Right click on the page and copy this certificate bundle on your disk (to be used in php.ini)
Use this certificate root certificate bundle:





STEP:45.Save it as cacert.pem.txt



STEP:46.Copy it and go to xamp->php->ext




STEP:47.Paste it into "C:\xampp\php\ext"





STEP:48.Copy the location cacert.pem from address bar




STEP:49.Update the php.ini file (insert or edit the following line: curl.cainfo). Go on Xampp Control Panel.Click on config.




STEP:50.Click on php.ini



STEP:51.Search cainfo in php.ini




STEP:52.Find curl.cainfo



STEP:53.Update the line curl.cainfo= Paste the location of cacert.pem there




STEP:54.Uncomment curl.cainfo with the location of cacert.pem





STEP:55.Open XAMPP Control Panel and stop the Apache and MySQL





STEP:56.Restart Apache and MySQL from XAMPP Control Panel.





STEP:57.Go back in the page that was giving error and refresh it.






STEP:58.Run the helloanalytic.php page.
Error will be gone and you will get the result you were expecting.





No comments:

Post a Comment