GA4 – 报告 API – PHP 客户端 – 过滤器错误

huangapple go评论107阅读模式
英文:

GA4 - report API - php client - filter error

问题

在查询中尝试应用过滤器时,出现了致命错误,以下是源代码:

  1. require 'vendor/autoload.php';
  2. use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
  3. use Google\Analytics\Data\V1beta\OrderBy;
  4. use Google\Analytics\Data\V1beta\OrderBy\MetricOrderBy;
  5. use Google\Analytics\Data\V1beta\DateRange;
  6. use Google\Analytics\Data\V1beta\Dimension;
  7. use Google\Analytics\Data\V1beta\Metric;
  8. use Google\Analytics\Data\V1beta\Filter;
  9. use Google\Analytics\Data\V1beta\Filter\StringFilter;
  10. use Google\Analytics\Data\V1beta\FilterExpression;
  11. use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;
  12. $property_id = XXXX;
  13. $response_cpc = $client->runReport([
  14. 'property' => 'properties/' . $property_id,
  15. 'dateRanges' => [
  16. new DateRange([
  17. 'start_date' => $_GET["date"],
  18. 'end_date' => $_GET["date"],
  19. ]),
  20. ],
  21. 'dimensions' => [new Dimension(
  22. [
  23. 'name' => 'firstUserSourceMedium',
  24. ]
  25. )],[new Dimension(
  26. [
  27. 'name' => 'firstUserCampaignName',
  28. ]
  29. )],
  30. 'metrics' => [new Metric(
  31. [
  32. 'name' => 'sessions',
  33. ]
  34. ),new Metric(
  35. [
  36. 'name' => 'bounceRate',
  37. ]
  38. ),
  39. new Metric(
  40. [
  41. 'name' => 'conversions:Newsletter_signup',
  42. ]
  43. ),
  44. new Metric(
  45. [
  46. 'name' => 'screenPageViewsPerSession',
  47. ]
  48. ),
  49. new Metric(
  50. [
  51. 'name' => 'transactions',
  52. ]
  53. ),
  54. new Metric(
  55. [
  56. 'name' => 'conversions:go_to_amazon',
  57. ]
  58. )
  59. ],
  60. 'dimensionFilter' => [new FilterExpression (
  61. [
  62. 'filter' => [new Filter(
  63. [
  64. 'field_name' => 'firstUserSourceMedium',
  65. 'string_filter' => [new StringFilter([
  66. 'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
  67. 'value' => 'cpc',
  68. 'case_sensitive' => false,
  69. ]
  70. )]
  71. ]
  72. )]
  73. ]
  74. )],
  75. 'limit' => 25,
  76. 'orderBys' =>[ new OrderBy([
  77. 'metric' => new MetricOrderBy([
  78. 'metric_name' =>'sessions']
  79. ),
  80. 'desc'=>true,
  81. ])],
  82. ]);
  83. print_r($response_cpc);

这里是我遇到的错误:

致命错误:未捕获异常:在 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:198 中期望 Google\Analytics\Data\V1beta\Filter\StringFilter。堆栈跟踪:#0 /home/Server1/xagyrxxhed/public_html/vendor/google/analytics-data/src/V1beta/Filter.php(116):Google\Protobuf\Internal\GPBUtil::checkMessage() #1 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1082):Google\Analytics\Data\V1beta\Filter->setStringFilter() #2 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(80):Google\Protobuf\Internal\Message->mergeFromArray() #3 /home/Server1/xagyrxxhed/public_html/vendor/google/analytics-data/src/V1beta/Filter.php(54):Google\Protobuf\Internal\Message->__construct() #4 /home/Server1/xagyrxxhed/public_html/quickstart.php(280):Google\Analytics\Data\V1beta\Filter->__construc 在 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php 第198行发生错误

有什么帮助吗?

英文:

Im try to apply a filter on a query, and getting an fatal error
here is the source code:

  1. require 'vendor/autoload.php';
  2. use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
  3. use Google\Analytics\Data\V1beta\OrderBy;
  4. use Google\Analytics\Data\V1beta\OrderBy\MetricOrderBy;
  5. use Google\Analytics\Data\V1beta\DateRange;
  6. use Google\Analytics\Data\V1beta\Dimension;
  7. use Google\Analytics\Data\V1beta\Metric;
  8. use Google\Analytics\Data\V1beta\Filter;
  9. use Google\Analytics\Data\V1beta\Filter\StringFilter;
  10. use Google\Analytics\Data\V1beta\FilterExpression;
  11. use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;
  12. $property_id = XXXX;
  13. $response_cpc = $client->runReport([
  14. 'property' => 'properties/' . $property_id,
  15. 'dateRanges' => [
  16. new DateRange([
  17. 'start_date' => $_GET["date"],
  18. 'end_date' => $_GET["date"],
  19. ]),
  20. ],
  21. 'dimensions' => [new Dimension(
  22. [
  23. 'name' => 'firstUserSourceMedium',
  24. ]
  25. )],[new Dimension(
  26. [
  27. 'name' => 'firstUserCampaignName',
  28. ]
  29. )
  30. ],
  31. 'metrics' => [new Metric(
  32. [
  33. 'name' => 'sessions',
  34. ]
  35. ),new Metric(
  36. [
  37. 'name' => 'bounceRate',
  38. ]
  39. ),
  40. new Metric(
  41. [
  42. 'name' => 'conversions:Newsletter_signup',
  43. ]
  44. ),
  45. new Metric(
  46. [
  47. 'name' => 'screenPageViewsPerSession',
  48. ]
  49. ),
  50. new Metric(
  51. [
  52. 'name' => 'transactions',
  53. ]
  54. ),
  55. new Metric(
  56. [
  57. 'name' => 'conversions:go_to_amazon',
  58. ]
  59. )
  60. ],
  61. 'dimensionFilter' => [new FilterExpression (
  62. [
  63. 'filter' => [new Filter(
  64. [
  65. 'field_name' => 'firstUserSourceMedium',
  66. 'string_filter' => [new StringFilter([
  67. 'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
  68. 'value' => 'cpc',
  69. 'case_sensitive'=> false,
  70. ]
  71. )]
  72. ]
  73. )]
  74. ]
  75. )],
  76. 'limit'=> 25,
  77. 'orderBys' =>[ new OrderBy([
  78. 'metric' => new MetricOrderBy([
  79. 'metric_name' =>'sessions']
  80. ),
  81. 'desc'=>true,
  82. ])],
  83. ]);
  84. print_r($response_cpc);

and here is the error I'm getting:

Fatal error: Uncaught Exception: Expect Google\Analytics\Data\V1beta\Filter\StringFilter. in /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php:198 Stack trace: #0 /home/Server1/xagyrxxhed/public_html/vendor/google/analytics-data/src/V1beta/Filter.php(116): Google\Protobuf\Internal\GPBUtil::checkMessage() #1 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1082): Google\Analytics\Data\V1beta\Filter->setStringFilter() #2 /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(80): Google\Protobuf\Internal\Message->mergeFromArray() #3 /home/Server1/xagyrxxhed/public_html/vendor/google/analytics-data/src/V1beta/Filter.php(54): Google\Protobuf\Internal\Message->__construct() #4 /home/Server1/xagyrxxhed/public_html/quickstart.php(280): Google\Analytics\Data\V1beta\Filter->__construc in /home/Server1/xagyrxxhed/public_html/vendor/google/protobuf/src/Google/Protobuf/Internal/GPBUtil.php on line 198

Any help ?

答案1

得分: 0

I can help you translate the code portion you provided:

  1. Fixed with:
  2. 1- memory increase
  3. 2- change in the code: Remove some [
  4. 'dimensionFilter' => new FilterExpression (
  5. [
  6. 'filter' => new Filter(
  7. [
  8. 'field_name' => 'firstUserSourceMedium',
  9. 'string_filter' => new Filter\StringFilter([
  10. 'match_type' => 4,
  11. 'value' => 'cpc',
  12. 'case_sensitive' => false,
  13. ]
  14. )
  15. ]
  16. )
  17. ]
  18. ),

Please note that I've only translated the code portion and removed the unnecessary characters.

英文:

Fixed with:
1- memory increase
2- change in the code: Remove some [

  1. 'dimensionFilter' => new FilterExpression (
  2. ``[
  3. 'filter' => new Filter(
  4. [
  5. 'field_name' => 'firstUserSourceMedium',
  6. 'string_filter' => new Filter\StringFilter([
  7. 'match_type' => 4,
  8. 'value' => 'cpc',
  9. 'case_sensitive'=> false,
  10. ]
  11. )
  12. ]
  13. )
  14. ]
  15. ),

huangapple
  • 本文由 发表于 2023年5月25日 02:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定