在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-analytics开源软件地址(OpenSource Url):https://github.com/spatie/laravel-analytics开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Retrieve data from Google AnalyticsUsing this package you can easily retrieve data from Google Analytics. Here are a few examples of the provided methods: use Analytics;
use Spatie\Analytics\Period;
//fetch the most visited pages for today and the past week
Analytics::fetchMostVisitedPages(Period::days(7));
//fetch visitors and page views for the past week
Analytics::fetchVisitorsAndPageViews(Period::days(7)); Most methods will return an Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. InstallationThis package can be installed through Composer. composer require spatie/laravel-analytics Optionally, you can publish the config file of this package with this command: php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider" The following config file will be published in return [
/*
* The view id of which you want to display data.
*/
'view_id' => env('ANALYTICS_VIEW_ID'),
/*
* Path to the client secret json file. Take a look at the README of this package
* to learn how to get this file. You can also pass the credentials as an array
* instead of a file path.
*/
'service_account_credentials_json' => storage_path('app/analytics/service-account-credentials.json'),
/*
* The amount of minutes the Google API responses will be cached.
* If you set this to zero, the responses won't be cached at all.
*/
'cache_lifetime_in_minutes' => 60 * 24,
/*
* Here you may configure the "store" that the underlying Google_Client will
* use to store it's data. You may also add extra parameters that will
* be passed on setCacheConfig (see docs for google-api-php-client).
*
* Optional parameters: "lifetime", "prefix"
*/
'cache' => [
'store' => 'file',
],
]; How to obtain the credentials to communicate with Google AnalyticsGetting credentialsThe 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 click "Select a project" in the header. Next up we must specify which API’s the project may consume. In the list of 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. 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 Granting permissions to your Analytics propertyI'm assuming that you've already created a Analytics account on the Analytics site. When setting up your property, click on "Advanced options" and make sure you enable Go to "User management" in the Admin-section of the property. On this screen you can grant access to the email address found in the Getting the view idThe last thing you'll have to do is fill in the You'll need the UsageWhen the installation is done you can easily retrieve Analytics data. Nearly all methods will return an Here are a few examples using periods //retrieve visitors and pageview data for the current day and the last seven days
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
//retrieve visitors and pageviews since the 6 months ago
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::months(6));
//retrieve sessions and pageviews with yearMonth dimension since 1 year ago
$analyticsData = Analytics::performQuery(
Period::years(1),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth'
]
);
If you want to have more control over the period you want to fetch data for, you can pass a $startDate = Carbon::now()->subYear();
$endDate = Carbon::now();
Period::create($startDate, $endDate); Provided methodsVisitors and pageviewspublic function fetchVisitorsAndPageViews(Period $period): Collection The function returns a Total visitors and pageviewspublic function fetchTotalVisitorsAndPageViews(Period $period): Collection The function returns a Most visited pagespublic function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection The function returns a Top referrerspublic function fetchTopReferrers(Period $period, int $maxResults = 20): Collection The function returns a User Typespublic function fetchUserTypes(Period $period): Collection The function returns a Top browserspublic function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection The function returns a All other Google Analytics queriesTo perform all other queries on the Google Analytics resource use public function performQuery(Period $period, string $metrics, array $others = []) You can get access to the underlying Analytics::getAnalyticsService(); TestingRun the tests with: vendor/bin/phpunit ChangelogPlease see CHANGELOG for more information what has changed recently. ContributingPlease see CONTRIBUTING for details. SecurityIf you've found a bug regarding security please mail [email protected] instead of using the issue tracker. CreditsAnd a special thanks to Caneco for the logo LicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论