Google Analytics is a popular website visit statistics tool that can be used to help webmasters track user visits, website traffic, visit sources, page views and other information. In this article, we will introduce you how to implement Google Analytics using the ThinkPHP6 framework.
1. Register a Google Analytics account
If you don’t have a Google Analytics account yet, you need to register an account first. The website registration address is: https://analytics.google.com/analytics/web/provision/#/provision
After creating an account and logging in, click "Create New Attribute" and fill in the basic information of the website. A "Global Tracking Code" will be given on the "Property Settings" page, which we will need to use later.
2. Install Google Analytics SDK
ThinkPHP6 provides a third-party SDK, and we can install the Google Analytics SDK directly in the project. Run the command in the command line window to install:
composer require spatie/laravel-analytics
After the installation is complete, we can edit the app.php file in the config folder and add LaravelAnalyticsServiceProvider to the service provider array.
'providers' => [ // Other Service Providers SpatieLaravelAnalyticsLaravelAnalyticsServiceProvider::class, ],
3. Set up Google Analytics authorization
Create the google-analytics.php file in the config folder and add the following code to the file:
return [ 'view_id' => env('ANALYTICS_VIEW_ID','YOUR_VIEW_ID'), 'service_account_credentials_json' => env('ANALYTICS_CREDENTIALS_JSON','YOUR_ANALYTICS_CREDENTIALS_JSON'), ];
"view_id "comes from the attribute settings of your Google Analytics account, and "service_account_credentials_json" is the credential for accessing Google Analytics and needs to generate a JSON file.
4. Generate JSON credentials
Create a project on the Google Cloud platform, and then generate a pair of public and private key files for the project. Create a service account on the Google Cloud platform and select "Project>Api Service Management>Service Account>Create Service Account". After entering to create a service account, enter the "Account Name", "Service Account ID", "Role", and click Create. Once created successfully, this service account will automatically generate a pair of public and private key files. In this article, we can download them and save them in the project folder for use by the program.
5. Call Google Analytics in the controller
We can call the Google Analytics SDK in the controller to collect access analysis data. Add code in the controller method:
use Analytics; use SpatieAnalyticsPeriod; class AnalyticsController extends Controller { public function index() { //选择查看的时间段 $period = Period::days(7); $data = Analytics::fetchVisitorsAndPageViews($period); print_r($data); } }
Complete code:
namespace appcontroller; use appBaseController; use Analytics; use SpatieAnalyticsPeriod; class AnalyticsController extends BaseController { public function index() { $period = Period::days(7); $data = Analytics::fetchVisitorsAndPageViews($period); print_r($data); } }
6. Test code
We can access the corresponding controller method to test whether the code is normal Work. After accessing the method, the output results should be the number of visits and page views in the last seven days.
7. Conclusion
In this short tutorial, we introduced how to use the Google Analytics SDK in ThinkPHP6. This SDK provides us with access analytics capabilities and helps us collect useful data about visitor behavior. We hope this article was helpful to you.
The above is the detailed content of How to implement Google Analytics using ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!