tourze/baidu-tongji-api-bundle

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/baidu-tongji-api-bundle


README

English | 中文

A Symfony bundle for integrating Baidu Tongji (Baidu Analytics) API into your application. This bundle provides comprehensive functionality to manage Baidu Tongji sites, retrieve analytics reports, and synchronize data with local storage.

Features

  • Site Management: Sync and manage Baidu Tongji sites and subdirectories
  • Report Retrieval: Access various types of analytics reports (trends, sources, visits, etc.)
  • Data Storage: Store report data locally using Doctrine ORM entities
  • CLI Commands: Convenient console commands for data synchronization
  • Admin Interface: EasyAdmin integration for managing data
  • OAuth2 Integration: Seamless integration with Baidu OAuth2 authentication

Requirements

  • PHP 8.1+
  • Symfony 6.4+
  • Doctrine ORM
  • tourze/baidu-oauth2-integrate-bundle - For Baidu OAuth2 authentication

Installation

Install the bundle using Composer:

composer require tourze/baidu-tongji-api-bundle

Configuration

Enable the bundle in your Symfony application:

// config/bundles.php
return [
    // ...
    Tourze\BaiduTongjiApiBundle\BaiduTongjiApiBundle::class => ['all' => true],
];

Console Commands

tongji:sync-sites

Synchronize Baidu Tongji sites for users.

Basic Usage

# Sync sites for all users
php bin/console tongji:sync-sites

# Sync sites for specific user
php bin/console tongji:sync-sites --user-id=123

# Force sync, ignore token expiration
php bin/console tongji:sync-sites --force

Options

  • --user-id, -u: Specific user ID to sync (syncs all users if not specified)
  • --force, -f: Force sync, ignore token expiration check

Examples

# Sync all user sites
php bin/console tongji:sync-sites

# Sync specific user sites
php bin/console tongji:sync-sites -u 12345

# Force sync sites and update tokens
php bin/console tongji:sync-sites --force

tongji:sync-report

Synchronize Baidu Tongji analytics reports.

Basic Usage

# Sync trend report for all sites
php bin/console tongji:sync-report trend/time/a --start-date=2024-01-01 --end-date=2024-01-31

# Sync report for specific site
php bin/console tongji:sync-report trend/time/a --site-id=123456 --start-date=2024-01-01 --end-date=2024-01-31

# Force sync, ignore token expiration
php bin/console tongji:sync-report trend/time/a --start-date=2024-01-01 --end-date=2024-01-31 --force

Parameters

Required Parameters:

  • method: Report method to retrieve (see table below)

Optional Parameters:

  • --site-id, -s: Specific site ID to sync (syncs all sites if not specified)
  • --start-date: Start date in YYYY-MM-DD format
  • --end-date: End date in YYYY-MM-DD format
  • --params, -p: Additional parameters in JSON format, default: {}
  • --force, -f: Force sync, ignore token expiration check

Available Report Methods

Method Description
trend/time/a Trend Analysis
trend/latest/a Real-time Visitors
pro/product/a Promotion Methods
pro/hour/a Baidu Promotion Trend
source/all/a All Sources
source/engine/a Search Engines
source/searchword/a Search Words
source/link/a External Links
custom/media/a Custom Media Tracking
visit/toppage/a Top Pages
visit/landingpage/a Landing Pages
visit/topdomain/a Top Domains
visit/district/a Regional Distribution (China)
visit/world/a World Regional Distribution
overview/getTimeTrendRpt Site Overview (Trend Data)
overview/getDistrictRpt Site Overview (Regional Distribution)
overview/getCommonTrackRpt Site Overview (Common Tracks)

Examples

# Sync trend report for all sites
php bin/console tongji:sync-report trend/time/a --start-date=2024-01-01 --end-date=2024-01-31

# Sync search engine report for specific site
php bin/console tongji:sync-report source/engine/a -s 123456 --start-date=2024-01-01 --end-date=2024-01-31

# Sync report with custom parameters
php bin/console tongji:sync-report trend/time/a --start-date=2024-01-01 --end-date=2024-01-31 -p '{"gran":"day"}'

# Force sync report and update tokens
php bin/console tongji:sync-report trend/time/a --start-date=2024-01-01 --end-date=2024-01-31 --force

Entities

TongjiSite

Represents a Baidu Tongji site with the following properties:

  • siteId: Baidu site ID
  • domain: Site domain
  • status: Site status (0: normal, 1: paused)
  • user: Associated Baidu OAuth2 user
  • subDirectories: Collection of subdirectories
  • siteCreateTime: Site creation time
  • rawData: Raw API response data

TongjiSubDirectory

Represents a Baidu Tongji subdirectory with the following properties:

  • subDirId: Subdirectory ID
  • subDir: Subdirectory path
  • status: Subdirectory status
  • site: Associated site

FactTrafficTrend

Stores aggregated traffic trend data:

  • siteId: Site ID
  • date: Report date
  • pageViewPv: Page views count
  • visitorUv: Unique visitors count
  • ipCount: IP count
  • bounceRatio: Bounce rate
  • avgVisitTime: Average visit time

RawTongjiReport

Stores raw report data from Baidu Tongji API:

  • reportMethod: Report method name
  • siteId: Site ID
  • reportDate: Report date
  • rawData: Raw JSON data from API

Services

TongjiApiClient

Core API client for communicating with Baidu Tongji API endpoints.

TongjiSiteService

Manages site synchronization and site-related operations.

TongjiReportSyncService

Handles report data synchronization and processing.

UserManagementService

Manages user-related operations and authentication.

Testing

Run the test suite:

vendor/bin/phpunit packages/baidu-tongji-api-bundle/tests/

Run static analysis:

vendor/bin/phpstan analyse packages/baidu-tongji-api-bundle/src/ --level=8

License

MIT License