{"id":28088921,"url":"https://github.com/mediafigaro/google-analytics-api-symfony","last_synced_at":"2025-08-09T21:11:50.866Z","repository":{"id":57016722,"uuid":"63780866","full_name":"mediafigaro/google-analytics-api-symfony","owner":"mediafigaro","description":"Google Analytics API Symfony Bundle","archived":false,"fork":false,"pushed_at":"2023-03-29T18:21:59.000Z","size":168,"stargazers_count":46,"open_issues_count":5,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-24T23:04:02.098Z","etag":null,"topics":["analytics","api","google","google-analytics","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"https://mediafigaro.github.io","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mediafigaro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-20T12:47:59.000Z","updated_at":"2025-03-12T10:12:45.000Z","dependencies_parsed_at":"2024-06-21T07:27:22.659Z","dependency_job_id":"01d59919-54f0-455b-bdba-f8d617cd3d83","html_url":"https://github.com/mediafigaro/google-analytics-api-symfony","commit_stats":{"total_commits":39,"total_committers":4,"mean_commits":9.75,"dds":0.6153846153846154,"last_synced_commit":"45ff6042481cfad6ffa779d8020a766cfbd6d2cd"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediafigaro%2Fgoogle-analytics-api-symfony","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediafigaro%2Fgoogle-analytics-api-symfony/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediafigaro%2Fgoogle-analytics-api-symfony/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediafigaro%2Fgoogle-analytics-api-symfony/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mediafigaro","download_url":"https://codeload.github.com/mediafigaro/google-analytics-api-symfony/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253947743,"owners_count":21988942,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","api","google","google-analytics","symfony","symfony-bundle"],"created_at":"2025-05-13T12:52:29.667Z","updated_at":"2025-05-13T12:52:30.235Z","avatar_url":"https://github.com/mediafigaro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Google Analytics API v4 Symfony bundle\n======================================\n\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/da6423cf-b198-402a-8d23-e82e7833f9f6/big.png)](https://insight.sensiolabs.com/projects/da6423cf-b198-402a-8d23-e82e7833f9f6)\n\n[![Latest Stable Version](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/v/stable)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n[![Total Downloads](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/downloads)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n[![Latest Unstable Version](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/v/unstable)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n[![License](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/license)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n[![Monthly Downloads](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/d/monthly)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n[![Daily Downloads](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/d/daily)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n[![composer.lock](https://poser.pugx.org/mediafigaro/google-analytics-api-symfony/composerlock)](https://packagist.org/packages/mediafigaro/google-analytics-api-symfony)\n\n# Use\n\nAt MEDIA.figaro http://media.figaro.fr, the advertising department of the french newspaper Le Figaro and part of the Figaro Group (CCM Benchmark), we use this bundle to monitor our digital platforms with Google Analytics. \n\nIt's a simple package that wraps the Google Analytics API version 4, and that gives you all the information to go straight to the point of getting some main metrics from GA.\n\nDevelopment by Kendrick https://github.com/kendrick-k.\n\nTo be able to use it, you have to setup a project on Google Console for Google Analytics, get the json key, then configure this package by setting the path for it. You'll have to add the developer email defined into the Google Console to the GA views to authorize it, otherwise the view won't be accessible through the API. \n\nYou can use the debug routes to go live and test a profile (ex id : 111111111, here with [Docker](https://github.com/mediafigaro/docker-symfony)) :\n\nhttp://symfony.dev/app_dev.php/analytics-api/111111111 \n\n![debug](doc/debug.png)\n\n# Installation\n\n    composer require mediafigaro/google-analytics-api-symfony\n    \nwithout Flex, add to /app/AppKernel.php :\n\n    $bundles = [\n        ...\n        new MediaFigaro\\GoogleAnalyticsApi\\GoogleAnalyticsApi(),\n    ];\n\n# Versions\n\n## 1.2\n\nAdding filterMetric and filterDimension to getDataDateRangeMetricsDimensions method which is a simple wrapper to Google Api Client ex :\n\n    $analyticsService = $this-\u003eget('google_analytics_api.api');\n\n    $data = $analyticsService-\u003egetDataDateRangeMetricsDimensions(\n        'myanalyticsviewid',    // viewid\n        '2018-01-01',   // date start\n        'today',        // date end\n        ['sessions','users','percentNewSessions','bounceRate'],             // metric\n        ['source','campaign','fullReferrer','sourceMedium','pagePath'],     // dimension\n        [   // order metric and/or dimension\n            'fields'    =\u003e  ['sessions'],\n            'order'     =\u003e  'descending'\n        ],\n        [   // metric\n            'metric_name'       =\u003e  'sessions',\n            'operator'          =\u003e  'LESS_THAN',\n            'comparison_value'  =\u003e  '100'\n        ],\n        [   // dimension\n            'dimension_name'    =\u003e  'sourceMedium',\n            'operator'          =\u003e  'EXACT',\n            'expressions'       =\u003e  [\n                'trading / native'\n            ]\n        ]\n    );\n\n## 1.1\n\nSymfony 4 simple adaptation with a public service and a new public method that takes in charge metrics and dimensions with sorting options :\n\n    getDataDateRangeMetricsDimensions($viewId,$dateStart,$dateEnd,$metrics='sessions',$dimensions=null,$sorting=null)\n\nQuery explorer https://ga-dev-tools.appspot.com/query-explorer/ to build your query.\n\n$viewId : https://developers.google.com/analytics/devguides/reporting/core/v3/reference#ids\n\n$dateStart : https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startDate\n\n$dateEnd: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#endDate\n\n$metrics: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#metrics (without the 'ga:', array or string)\n\n$dimensions: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#dimensions (without the 'ga:', array or string)\n\n$sorting: https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort\n \nwithout the 'ga:', array or string, eg. :\n\n    [\n        'fields'    =\u003e  ['pagePath','sessions'], // or : 'sessions'\n        'order'     =\u003e  'descending'\n    ]\n\nexample :\n\n    $analyticsService = $this-\u003eget('google_analytics_api.api');\n    \n    $data = $analyticsService-\u003egetDataDateRangeMetricsDimensions(\n        'myanalyticsviewid',\n        '30daysAgo',\n        'today',\n        ['sessions','users','percentNewSessions','bounceRate'],\n        ['source','campaign','fullReferrer','sourceMedium','pagePath'],\n        [\n            'fields'    =\u003e  ['pagePath','sessions'],\n            'order'     =\u003e  'descending'\n        ]\n    )\n\n$data :\n\n    array(329) {\n      [0]=\u003e\n      array(2) {\n        [\"metrics\"]=\u003e\n        array(4) {\n          [\"sessions\"]=\u003e\n          string(5) \"16738\"\n          [\"users\"]=\u003e\n          string(5) \"15602\"\n          [\"percentNewSessions\"]=\u003e\n          string(17) \"88.39168359421676\"\n          [\"bounceRate\"]=\u003e\n          string(17) \"83.95268251881946\"\n        }\n        [\"dimensions\"]=\u003e\n        array(5) {\n          [\"source\"]=\u003e\n          string(7) \"trading\"\n          [\"campaign\"]=\u003e\n          string(7) \"my-campaign\"\n          [\"fullReferrer\"]=\u003e\n          string(7) \"trading\"\n          [\"sourceMedium\"]=\u003e\n          string(16) \"trading / native\"\n          [\"pagePath\"]=\u003e\n          string(50) \"/my-url\"\n        }\n      }\n      [1]=\u003e\n      array(2) {\n        [\"metrics\"]=\u003e\n        array(4) {\n          [\"sessions\"]=\u003e\n          string(4) \"6506\"\n          [\"users\"]=\u003e\n          string(4) \"6200\"\n          [\"percentNewSessions\"]=\u003e\n          string(17) \"87.05810021518599\"\n          [\"bounceRate\"]=\u003e\n          string(17) \"87.74976944359054\"\n        }\n        [\"dimensions\"]=\u003e\n        array(5) {\n          [\"source\"]=\u003e\n          string(7) \"trading\"\n          [\"campaign\"]=\u003e\n          string(7) \"my-campaign-2\"\n          [\"fullReferrer\"]=\u003e\n          string(7) \"trading\"\n          [\"sourceMedium\"]=\u003e\n          string(19) \"trading / 320x480-1\"\n          [\"pagePath\"]=\u003e\n          string(50) \"/my-url-2\"\n        }\n      }\n      [2]=\u003e\n      ...\n\n**Session - Dimensions \u0026 Metrics Explorer** : https://developers.google.com/analytics/devguides/reporting/core/dimsmets\n\n## 1.0\n\nFirst version with a quick connector to consume Google Analytics v4 with Google API client with getDataDateRange private method (that handles only metrics), wrapped with some public methods to get a quick access to main metrics such as sessions (eg. getBounceRateDateRange($viewId,$dateStart,$dateEnd)), bounce rate, average time on page, page view per session, new visits, page views and average page load time.\nDebug route included for a simple setup and test.\n\n# Configuration\n\n    google_analytics_api.google_analytics_json_key\n\nSet the relative path for your json key (set it on your server, better not into your repository) from execution path, ex: /data/analytics/analytics-27cef1a4c0fd.json.\n\n/app/config/parameters.yml\n\n    google_analytics_json_key: \"../data/analytics/analytics-27cef1a4c0fd.json\"\n\n/app/config/config.yml\n\n    google_analytics_api:\n        google_analytics_json_key: \"%google_analytics_json_key%\"\n        \n# Google API key\n\nGenerate the json file from https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com\u0026credential=client_key by creating a project, check the documentation : https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php.\n\n# Google Analytics API v4\n\nList of metrics for report building with search engine : https://developers.google.com/analytics/devguides/reporting/core/dimsmets eg. ga:sessions, ga:visits, ga:bounceRate ...\n\nObjects : https://github.com/google/google-api-php-client-services/tree/master/AnalyticsReporting\n\n(example : ReportData object : https://github.com/google/google-api-php-client-services/blob/master/AnalyticsReporting/ReportData.php)\n\nSamples : https://developers.google.com/analytics/devguides/reporting/core/v4/samples\n\n# Debug\n\nAdd the debug routes for development purposes :\n\n/app/config/routing_dev.yml\n\n    _google_analytics_api:\n        resource: \"@GoogleAnalyticsApi/Resources/config/routing_dev.yml\"\n\nhttp://symfony.dev/app_dev.php/analytics-api/000000000 \n\n000000000 = profile id that you can find in the analytics URL, p000000000 :\n\nhttps://analytics.google.com/analytics/web/?hl=en\u0026pli=1#management/Settings/a222222222w1111111111p000000000/%3Fm.page%3DPropertySettings/\n\nResult of this debug page :\n\n![debug](doc/debug.png)\n\n# Errors\n\nIn that 403 error case, follow the link and authorize the API v4.\n\n    ...\n        \"message\": \"Google Analytics Reporting API has not been used in project xxxxxx-xxxxxx-000000 \n        before or it is disabled. Enable it by visiting \n        https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=xxxxxx-xxxxxx-000000 \n        then retry. If you enabled this API recently, wait a few minutes for the action to propagate \n        to our systems and retry.\",\n        \"domain\": \"global\",\n        \"reason\": \"forbidden\"\n    }\n    ],\n    \"status\": \"PERMISSION_DENIED\"\n\n# Example\n\nCall the service :\n\n    $analyticsService = $this-\u003eget('google_analytics_api.api');\n    $analytics = $analyticsService-\u003egetAnalytics();\n    \nUse the method helpers to get the main metrics within a date range :\n    \n    $viewId = '000000000'; // set your view id\n    \n    // get some metrics (last 30 days, date format is yyyy-mm-dd)\n    $sessions = $analyticsService-\u003egetSessionsDateRange($viewId,'30daysAgo','today');\n    $bounceRate = $analyticsService-\u003egetBounceRateDateRange($viewId,'30daysAgo','today');\n    $avgTimeOnPage = $analyticsService-\u003egetAvgTimeOnPageDateRange($viewId,'30daysAgo','today');\n    $pageViewsPerSession = $analyticsService-\u003egetPageviewsPerSessionDateRange($viewId,'30daysAgo','today');\n    $percentNewVisits = $analyticsService-\u003egetPercentNewVisitsDateRange($viewId,'30daysAgo','today');\n    $pageViews = $analyticsService-\u003egetPageViewsDateRange($viewId,'30daysAgo','today');\n    $avgPageLoadTime = $analyticsService-\u003egetAvgPageLoadTimeDateRange($viewId,'30daysAgo','today');\n\n# Contribution\n\nYou are welcome to contribute to this small Google Analytics v4 wrapper, to create more helpers or more.\n\n# More tools\n\nTry the Symfony Debug Toolbar Git : https://github.com/kendrick-k/symfony-debug-toolbar-git and the docker Service Oriented Architecture for Symfony : https://github.com/mediafigaro/docker-symfony.\n\n# Tutorial\n\nFrench [tutorial](https://www.supinfo.com/articles/single/2423-symfony-27-integration-google-analytics) by Jérémy PERCHE, SUPINFO student.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmediafigaro%2Fgoogle-analytics-api-symfony","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmediafigaro%2Fgoogle-analytics-api-symfony","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmediafigaro%2Fgoogle-analytics-api-symfony/lists"}