Get your Website Stats from Google Analytics without Authentication

We always love to experiment different things which enhances technology, business and client needs.Let’s experiment how to get your website stats without logging into Google Analytics account every time. Here’s how:

Step 1:

First create an empty folder name under htdocs. Please refer the screenshot in step 2 for folder creation & composer dependency installation.
Step 2:

For Windows:-
Open cmd prompt then type “composer require google/apiclient:1.*” . Like below

For Ubuntu :-

Open your terminal then type “composer require google/apiclient:1.*” . Like below

Before implementing this library, you must do some basic steps in “Google Developer Console”, and “Google analytics”.
I. In Google Developer Console :-
Step 1.1). Login to your Google developer console
Step 1.2). Select your “Project Name”.

Step 1.3). Then Click Library.

If you want to get data from Google analytics, first you must enabled
“Google Analytics API”. So you have followed step d.
Step 1.4). Other popular APIs -> Click -> Analytics API.

Step 1.5). Now click “ENABLE” text on “Analytics API” page

Step 1.6). Then Go to “Credentials” Under “API API Manager”.

Step 1.7). Click “Create credentials” button.

Note: – If you use other keys like “api key ,client authentication key “, those access token only valid for certain time. So you have to use “service account key” because it creates “refresh token” for your keys. Refresh token never expires”.
Step 1.8). Now, Select “Service account key” from the popup box

Step 1.9). On “create service account” tab,

(a) . Service account -> new service account -> type any “service account name”

(b) Keytype -> click ->p12 -> click “CREATE WITHOUT ROLE”

Step 1.10).

Step 1.12). Please copy the file, and paste into your folder path /vendor/your_service_account.p12

Step 1.13). Copy your “Service account id “. Because mainly this id “used to get data from Google analytics”

2. In Google Analytics:-

Step 2.1). Login to your Google analytics account
Step 2.2). Click “admin”

Step 2.3).Click “User Management “

Here, you must add your service account id .If you have not added this details, Google analytics will not allow to access the Google analytics data by using php api . It will keep displaying “User doesn’t have Google analytics” account.

Now let us discuss about how to integrate this library to your code.
Create one new php file like “ganalytics_data.php”
3. Coding Block :-
require_once ‘../../vendor/autoload.php’; (path to your composer autoload file )

[html]
$analytics = initializeAnalytics();
initializeAnalytics () :-
Only for basic setup process (like service account id ,Refresh token creation)
$profile = getFirstProfileId($analytics);

[/html]

This function returns your Google analytics account profile id for your service account details
$results = getResults($analytics,$profile);
This function is used for display the “Your analytics data “.

[html]
function initializeAnalytics(){
$service_account_email =’your service account id’;
$key_file_location = ‘path of your service account key file ’;
$client = new Google_Client();
$client->setApplicationName("Your project name");
$analytics = new Google_Service_Analytics($client);
$key = file_get_contents($key_file_location);
If your account exists, create refresh token for access the Google analytics “data .
$cred = new Google_Auth_AssertionCredentials( $service_account_email,
array(Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
return $analytics;
}
[/html]

Have any clarifications on this blog post? We are glad to answer them for you. Consult with us now, let’s talk.

About the author: ShareSoftAdmin

Leave a Reply

Your email address will not be published.