Download the PHP package nguyendachuy/laravel-analytics without Composer
On this page you can find all versions of the php package nguyendachuy/laravel-analytics. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nguyendachuy/laravel-analytics
More information about nguyendachuy/laravel-analytics
Files in nguyendachuy/laravel-analytics
Package laravel-analytics
Short Description A Laravel package to retrieve Google Analytics data. Edited to be able to use for lower php versions
License MIT
Homepage https://github.com/nguyendachuy/laravel-analytics
Informations about the package laravel-analytics
Retrieve data from Google Analytics
Using this package you can easily retrieve data from Google Analytics.
Here are a few examples of the provided methods:
Most methods will return an \Illuminate\Support\Collection
object containing the results.
Installation
This package can be installed through Composer.
Optionally, you can publish the config file of this package with this command:
The following config file will be published in config/analytics.php
How to obtain the credentials to communicate with Google Analytics
Getting credentials
The first thing you’ll need to do is to get some credentials to use Google API’s. I’m assuming that you’ve already created a Google account and are signed in. Head over to Google API’s site and select or create a project.
Next up we must specify which API’s the project may consume. Go to the API Library and search for "Google Analytics Data API".
Choose enable to enable the API.
Now that you’ve created a project that has access to the Analytics API it’s time to download a file with these credentials. Click "Credentials" in the sidebar. You’ll want to create a "Service account key".
On the next screen you can give the service account a name. You can name it anything you’d like. In the service account id you’ll see an email address. We’ll use this email address later on in this guide.
Go to the details screen of your created service account and select "keys", from the "Add key" dropdown select "Create new key".
Select "JSON" as the key type and click "Create" to download the JSON file.
Save the json inside your Laravel project at the location specified in the service_account_credentials_json
key of the config file of this package. Because the json file contains potentially sensitive information I don't recommend committing it to your git repository.
Granting permissions to your Analytics property
I'm assuming that you've already created a Analytics account on the Analytics site and are using the new GA4 properties.
First you will need to know your property ID. In Analytics, go to Settings > Property Settings. Here you will be able to copy your property ID. Use this value for the ANALYTICS_PROPERTY_ID
key in your .env file.
Now we will need to give access to the service account you created. Go to "Property Access Management" in the Admin-section of the property. Click the plus sign in the top right corner to add a new user.
On this screen you can grant access to the email address found in the client_email
key from the json file you download in the previous step. Analyst role is enough.
Usage
When the installation is done you can easily retrieve Analytics data. Nearly all methods will return an Illuminate\Support\Collection
-instance.
Here are a few examples using periods
$analyticsData
is a Collection
in which each item is an array that holds keys date
, visitors
and pageViews
If you want to have more control over the period you want to fetch data for, you can pass a startDate
and an endDate
to the period object.
Provided methods
Visitors and page views
The function returns a Collection
in which each item is an array that holds keys activeUsers
, screenPageViews
and pageTitle
.
Visitors and page views by date
The function returns a Collection
in which each item is an array that holds keys date
, activeUsers
, screenPageViews
and pageTitle
.
Total visitors and pageviews
The function returns a Collection
in which each item is an array that holds keys date
, date
, visitors
, and pageViews
.
Most visited pages
The function returns a Collection
in which each item is an array that holds keys fullPageUrl
, pageTitle
and screenPageViews
.
Top referrers
The function returns a Collection
in which each item is an array that holds keys screenPageViews
and pageReferrer
.
User Types
The function returns a Collection
in which each item is an array that holds keys activeUsers
and newVsReturning
which can equal to new
or returning
.
Top browsers
The function returns a Collection
in which each item is an array that holds keys screenPageViews
and browser
.
All other Google Analytics queries
For all other queries you can use the get
function.
Here's some extra info on the arguments you can pass:
Period $period
: a NguyenHuy\Analytics\Period object to indicate that start and end date for your query.
array $metrics
: an array of metrics to retrieve. You can find a list of all metrics here.
array $dimensions
: an array of dimensions to group the results by. You can find a list of all dimensions here.
int $limit
: the maximum number of results to return.
array $orderBy
: of OrderBy objects to sort the results by.
array $offset
: Defaults to 0, you can use this in combination with the $limit param to have pagination.
bool $keepEmptyRows
: If false or unspecified, each row with all metrics equal to 0 will not be returned. If true, these rows will be returned if they are not separately removed by a filter.
For example:
FilterExpression $dimensionFilter
: filter the result to include only specific dimension values. You can find more details here.
For example:
Testing
Run the tests with:
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- All Contributors
License
The MIT License (MIT). Please see License File for more information.