GA.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. <?php
  2. namespace app\lib;
  3. class GA{
  4. const SUPERNOVA_KEY_PATH= BASE_DIR . 'config/supernova.json';
  5. const ALIPEARL_KEY_PATH= BASE_DIR . 'config/alipearl.json';
  6. const WIGGINS_KEY_PATH= BASE_DIR . 'config/wiggins.json';
  7. const YOLISSA_KEY_PATH= BASE_DIR . 'config/yolissa.json';
  8. const ASTERIA_KEY_PATH= BASE_DIR . 'config/asteria.json';
  9. private static $insances=[];
  10. private static function initializeAnalytics($dp){
  11. $KEY_FILE_LOCATION = self::getPath($dp);
  12. if(!$KEY_FILE_LOCATION){
  13. throw new \Exception("store 配置不存在");
  14. }
  15. $client = new \Google_Client();
  16. $client->setApplicationName("Hello Analytics Reporting");
  17. $client->setAuthConfig($KEY_FILE_LOCATION);
  18. $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  19. $analytics = new \Google_Service_AnalyticsReporting($client);
  20. return $analytics;
  21. }
  22. public static function getInstance($dp){
  23. if(isset(self::$insances[$dp])){
  24. return self::$insances[$dp];
  25. }else{
  26. self::$insances[$dp]=self::initializeAnalytics($dp);
  27. return self::$insances[$dp];
  28. }
  29. }
  30. private function getViewId($dp){
  31. $VIEW_ID="";
  32. switch($dp){
  33. case "1"://supernovahair
  34. $VIEW_ID="155703065";
  35. break;
  36. case "2"://asteriahair
  37. $VIEW_ID="162723112";
  38. break;
  39. case "3"://alipearlhair
  40. $VIEW_ID="148377136";
  41. break;
  42. case "5"://yolissahair
  43. $VIEW_ID="157127813";
  44. break;
  45. case "6"://wiggins
  46. $VIEW_ID="154631720";
  47. break;
  48. }
  49. return $VIEW_ID;
  50. }
  51. public static function getPath($dp){
  52. $path="";
  53. switch($dp){
  54. case "1"://supernovahair
  55. $path=self::SUPERNOVA_KEY_PATH;
  56. break;
  57. case "2"://asteriahair
  58. $path=self::ASTERIA_KEY_PATH;
  59. break;
  60. case "3"://alipearlhair
  61. $path=self::ALIPEARL_KEY_PATH;
  62. break;
  63. case "5"://yolissahair
  64. $path=self::YOLISSA_KEY_PATH;
  65. break;
  66. case "6"://alipearlhair
  67. $path=self::WIGGINS_KEY_PATH;
  68. break;
  69. }
  70. return $path;
  71. }
  72. public function contentpages($dp,$start_at,$end_at){
  73. $analytics=self::getInstance($dp);
  74. $metricArr=[
  75. 'pageviews'=>"ga:pageviews",
  76. 'uniquePageviews'=>"ga:uniquePageviews",
  77. 'avgTimeOnPage'=>"ga:avgTimeOnPage",
  78. 'entrances'=>"ga:entrances",
  79. 'bounceRate'=>"ga:bounceRate",
  80. 'exitRate'=>"ga:exitRate",
  81. 'pageValue'=>"ga:pageValue",
  82. ];
  83. $dimensionArr=[
  84. 'pagePath'=>'ga:pagePath',
  85. 'date'=>"ga:date",
  86. ];
  87. $VIEW_ID = $this->getViewId($dp);//
  88. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  89. $dateRange->setStartDate($start_at);
  90. $dateRange->setEndDate($end_at);
  91. $metrics=[];
  92. $dimensions=[];
  93. foreach($metricArr as $k=>$v){
  94. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  95. $Metric->setExpression($v);
  96. $Metric->setAlias($k);
  97. array_push($metrics,$Metric);
  98. }
  99. foreach($dimensionArr as $k=>$v){
  100. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  101. $Dimension->setName($v);
  102. array_push($dimensions,$Dimension);
  103. }
  104. $pageviewsFilter = new \Google_Service_AnalyticsReporting_MetricFilter();
  105. $pageviewsFilter->setMetricName("ga:pageviews");
  106. $pageviewsFilter->setOperator("GREATER_THAN");
  107. $pageviewsFilter->setComparisonValue("1");
  108. $MetricFilterClause=new \Google_Service_AnalyticsReporting_MetricFilterClause();
  109. $MetricFilterClause->setFilters(array($pageviewsFilter));
  110. $segmentDimensions = new \Google_Service_AnalyticsReporting_Dimension();
  111. $segmentDimensions->setName("ga:segment");
  112. array_push($dimensions,$segmentDimensions);
  113. // Create Dimension Filter.
  114. $dimensionFilter = new \Google_Service_AnalyticsReporting_SegmentDimensionFilter();
  115. $dimensionFilter->setDimensionName("ga:pagePath");
  116. $dimensionFilter->setOperator("REGEXP");
  117. $dimensionFilter->setExpressions(array("(fbclid|catalogsearch)"));
  118. // Create Segment Filter Clause.
  119. $segmentFilterClause = new \Google_Service_AnalyticsReporting_SegmentFilterClause();
  120. $segmentFilterClause->setDimensionFilter($dimensionFilter);
  121. // Create the Or Filters for Segment.
  122. $orFiltersForSegment = new \Google_Service_AnalyticsReporting_OrFiltersForSegment();
  123. $orFiltersForSegment->setSegmentFilterClauses(array($segmentFilterClause));
  124. // Create the Simple Segment.
  125. $simpleSegment = new \Google_Service_AnalyticsReporting_SimpleSegment();
  126. $simpleSegment->setOrFiltersForSegment(array($orFiltersForSegment));
  127. // Create the Segment Filters.
  128. $segmentFilter = new \Google_Service_AnalyticsReporting_SegmentFilter();
  129. $segmentFilter->setSimpleSegment($simpleSegment);
  130. $segmentFilter->setNot(True);
  131. // Create the Segment Definition.
  132. $segmentDefinition = new \Google_Service_AnalyticsReporting_SegmentDefinition();
  133. $segmentDefinition->setSegmentFilters(array($segmentFilter));
  134. $dynamicSegment = new \Google_Service_AnalyticsReporting_DynamicSegment();
  135. $dynamicSegment->setSessionSegment($segmentDefinition);
  136. $dynamicSegment->setName('pagePath');
  137. $segment = new \Google_Service_AnalyticsReporting_Segment();
  138. $segment->setDynamicSegment($dynamicSegment);
  139. $orderBy=new \Google_Service_AnalyticsReporting_OrderBy();
  140. $orderBy->setFieldName('ga:pageviews');
  141. $orderBy->setSortOrder("DESCENDING");
  142. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  143. $request->setViewId($VIEW_ID);
  144. $request->setDateRanges($dateRange);
  145. $request->setMetrics($metrics);
  146. $request->setDimensions($dimensions);
  147. $request->setSegments(array($segment));
  148. $request->setOrderBys($orderBy);
  149. $request->setPageSize(5000);
  150. $request->setMetricFilterClauses($MetricFilterClause);
  151. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  152. $body->setReportRequests( array( $request) );
  153. $reports= $analytics->reports->batchGet( $body );
  154. $result=$this->getResults($reports);
  155. return $result;
  156. }
  157. public function trafficsource($dp,$start_at,$end_at){
  158. $analytics=self::getInstance($dp);
  159. $metricArr=[
  160. 'users'=>"ga:users",
  161. 'newUsers'=>"ga:newUsers",
  162. 'sessions'=>"ga:sessions",
  163. 'bounceRate'=>"ga:bounceRate",
  164. 'pageviewsPerSession'=>"ga:pageviewsPerSession",
  165. 'avgSessionDuration'=>"ga:avgSessionDuration",
  166. 'transactionsPerVisit'=>"ga:transactionsPerVisit",
  167. 'transactions'=>"ga:transactions",
  168. 'transactionRevenue'=>"ga:transactionRevenue",
  169. ];
  170. $dimensionArr=[
  171. 'pagePath'=>'ga:source',
  172. 'date'=>"ga:date",
  173. 'channelGrouping'=>"ga:channelGrouping",
  174. 'landingPagePath'=>"ga:landingPagePath",
  175. 'keyword'=>"ga:keyword",
  176. 'socialNetwork'=>"ga:socialNetwork",
  177. ];
  178. $VIEW_ID = $this->getViewId($dp);//
  179. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  180. $dateRange->setStartDate($start_at);
  181. $dateRange->setEndDate($end_at);
  182. $metrics=[];
  183. $dimensions=[];
  184. foreach($metricArr as $k=>$v){
  185. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  186. $Metric->setExpression($v);
  187. $Metric->setAlias($k);
  188. array_push($metrics,$Metric);
  189. }
  190. foreach($dimensionArr as $k=>$v){
  191. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  192. $Dimension->setName($v);
  193. array_push($dimensions,$Dimension);
  194. }
  195. $segmentDimensions = new \Google_Service_AnalyticsReporting_Dimension();
  196. $segmentDimensions->setName("ga:segment");
  197. array_push($dimensions,$segmentDimensions);
  198. // Create Dimension Filter.
  199. $dimensionFilter = new \Google_Service_AnalyticsReporting_SegmentDimensionFilter();
  200. $dimensionFilter->setDimensionName("ga:source");
  201. $dimensionFilter->setOperator("REGEXP");
  202. $dimensionFilter->setExpressions(array("(fbclid|catalogsearch)"));
  203. // Create Segment Filter Clause.
  204. $segmentFilterClause = new \Google_Service_AnalyticsReporting_SegmentFilterClause();
  205. $segmentFilterClause->setDimensionFilter($dimensionFilter);
  206. // Create the Or Filters for Segment.
  207. $orFiltersForSegment = new \Google_Service_AnalyticsReporting_OrFiltersForSegment();
  208. $orFiltersForSegment->setSegmentFilterClauses(array($segmentFilterClause));
  209. // Create the Simple Segment.
  210. $simpleSegment = new \Google_Service_AnalyticsReporting_SimpleSegment();
  211. $simpleSegment->setOrFiltersForSegment(array($orFiltersForSegment));
  212. // Create the Segment Filters.
  213. $segmentFilter = new \Google_Service_AnalyticsReporting_SegmentFilter();
  214. $segmentFilter->setSimpleSegment($simpleSegment);
  215. $segmentFilter->setNot(True);
  216. // Create the Segment Definition.
  217. $segmentDefinition = new \Google_Service_AnalyticsReporting_SegmentDefinition();
  218. $segmentDefinition->setSegmentFilters(array($segmentFilter));
  219. $dynamicSegment = new \Google_Service_AnalyticsReporting_DynamicSegment();
  220. $dynamicSegment->setSessionSegment($segmentDefinition);
  221. $dynamicSegment->setName('trafficsources filter');
  222. $segment = new \Google_Service_AnalyticsReporting_Segment();
  223. $segment->setDynamicSegment($dynamicSegment);
  224. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  225. $request->setViewId($VIEW_ID);
  226. $request->setDateRanges($dateRange);
  227. $request->setMetrics($metrics);
  228. $request->setDimensions($dimensions);
  229. $request->setSegments(array($segment));
  230. $request->setPageSize(5000);
  231. // $request->setOrderBys($orderBy);
  232. // $request->setMetricFilterClauses($MetricFilterClause);
  233. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  234. $body->setReportRequests( array( $request) );
  235. $reports= $analytics->reports->batchGet( $body );
  236. $result=$this->getResults($reports);
  237. return $result;
  238. }
  239. public function visitors_geo($dp,$start_at,$end_at){
  240. $analytics=self::getInstance($dp);
  241. $dimensionArr=[
  242. 'country'=>'ga:country',
  243. 'region'=>"ga:region",
  244. ];
  245. $metricArr=[
  246. 'transactions'=>"ga:transactions",
  247. 'transactionRevenue'=>"ga:transactionRevenue",
  248. ];
  249. $VIEW_ID = $this->getViewId($dp);//
  250. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  251. $dateRange->setStartDate($start_at);
  252. $dateRange->setEndDate($end_at);
  253. $metrics=[];
  254. $dimensions=[];
  255. foreach($metricArr as $k=>$v){
  256. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  257. $Metric->setExpression($v);
  258. $Metric->setAlias($k);
  259. array_push($metrics,$Metric);
  260. }
  261. foreach($dimensionArr as $k=>$v){
  262. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  263. $Dimension->setName($v);
  264. array_push($dimensions,$Dimension);
  265. }
  266. $orderBy=new \Google_Service_AnalyticsReporting_OrderBy();
  267. $orderBy->setFieldName('ga:transactions');
  268. $orderBy->setSortOrder("DESCENDING");
  269. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  270. $request->setViewId($VIEW_ID);
  271. $request->setDateRanges($dateRange);
  272. $request->setMetrics($metrics);
  273. $request->setDimensions($dimensions);
  274. $request->setOrderBys($orderBy);
  275. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  276. $body->setReportRequests( array( $request) );
  277. $reports= $analytics->reports->batchGet( $body );
  278. $result=$this->getResults($reports);
  279. return $result;
  280. }
  281. public function product($dp,$start_at,$end_at){
  282. $analytics=self::getInstance($dp);
  283. $dimensionArr=[
  284. 'productName'=>'ga:productName',
  285. ];
  286. $metricArr=[
  287. 'itemRevenue'=>"ga:itemRevenue",
  288. 'uniquePurchases'=>"ga:uniquePurchases",
  289. 'itemQuantity'=>"ga:itemQuantity",
  290. ];
  291. $VIEW_ID = $this->getViewId($dp);//
  292. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  293. $dateRange->setStartDate($start_at);
  294. $dateRange->setEndDate($end_at);
  295. $metrics=[];
  296. $dimensions=[];
  297. foreach($metricArr as $k=>$v){
  298. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  299. $Metric->setExpression($v);
  300. $Metric->setAlias($k);
  301. array_push($metrics,$Metric);
  302. }
  303. foreach($dimensionArr as $k=>$v){
  304. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  305. $Dimension->setName($v);
  306. array_push($dimensions,$Dimension);
  307. }
  308. $orderBy=new \Google_Service_AnalyticsReporting_OrderBy();
  309. $orderBy->setFieldName('ga:itemQuantity');
  310. $orderBy->setSortOrder("DESCENDING");
  311. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  312. $request->setViewId($VIEW_ID);
  313. $request->setDateRanges($dateRange);
  314. $request->setMetrics($metrics);
  315. $request->setDimensions($dimensions);
  316. $request->setOrderBys($orderBy);
  317. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  318. $body->setReportRequests( array( $request) );
  319. $reports= $analytics->reports->batchGet( $body );
  320. $result=$this->getResults($reports);
  321. return $result;
  322. }
  323. public function visitors_type($dp,$start_at,$end_at){
  324. $analytics=self::getInstance($dp);
  325. $dimensionArr=[
  326. 'userType'=>'ga:userType',
  327. 'date'=>'ga:date',
  328. ];
  329. $metricArr=[
  330. 'users'=>"ga:users",
  331. 'newUsers'=>"ga:newUsers",
  332. ];
  333. $VIEW_ID = $this->getViewId($dp);//
  334. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  335. $dateRange->setStartDate($start_at);
  336. $dateRange->setEndDate($end_at);
  337. $metrics=[];
  338. $dimensions=[];
  339. foreach($metricArr as $k=>$v){
  340. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  341. $Metric->setExpression($v);
  342. $Metric->setAlias($k);
  343. array_push($metrics,$Metric);
  344. }
  345. foreach($dimensionArr as $k=>$v){
  346. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  347. $Dimension->setName($v);
  348. array_push($dimensions,$Dimension);
  349. }
  350. // $orderBy=new \Google_Service_AnalyticsReporting_OrderBy();
  351. // $orderBy->setFieldName('ga:itemQuantity');
  352. // $orderBy->setSortOrder("DESCENDING");
  353. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  354. $request->setViewId($VIEW_ID);
  355. $request->setDateRanges($dateRange);
  356. $request->setMetrics($metrics);
  357. $request->setDimensions($dimensions);
  358. // $request->setOrderBys($orderBy);
  359. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  360. $body->setReportRequests( array( $request) );
  361. $reports= $analytics->reports->batchGet( $body );
  362. $result=$this->getResults($reports);
  363. return $result;
  364. }
  365. public function productInfo($productName,$dp,$start_at,$end_at){
  366. $analytics=self::getInstance($dp);
  367. $dimensionArr=[
  368. 'productName'=>'ga:productName',
  369. ];
  370. $metricArr=[
  371. 'itemRevenue'=>"ga:itemRevenue",
  372. 'revenuePerItem'=>"ga:revenuePerItem",
  373. 'productRefundAmount'=>"ga:productRefundAmount",
  374. 'quantityAddedToCart'=>"ga:quantityAddedToCart",
  375. // 'quantityRemovedFromCart'=>"ga:quantityRemovedFromCart",
  376. 'itemQuantity'=>"ga:itemQuantity",
  377. 'productListViews'=>"ga:productListViews",
  378. 'productDetailViews'=>"ga:productDetailViews",
  379. 'productAddsToCart'=>"ga:productAddsToCart",
  380. // 'productRemovesFromCart'=>"ga:productRemovesFromCart",
  381. 'productCheckouts'=>"ga:productCheckouts",
  382. 'uniquePurchases'=>"ga:uniquePurchases",
  383. ];
  384. $VIEW_ID = $this->getViewId($dp);//
  385. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  386. $dateRange->setStartDate($start_at);
  387. $dateRange->setEndDate($end_at);
  388. $metrics=[];
  389. $dimensions=[];
  390. foreach($metricArr as $k=>$v){
  391. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  392. $Metric->setExpression($v);
  393. $Metric->setAlias($k);
  394. array_push($metrics,$Metric);
  395. }
  396. foreach($dimensionArr as $k=>$v){
  397. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  398. $Dimension->setName($v);
  399. array_push($dimensions,$Dimension);
  400. }
  401. $skuFilter = new \Google_Service_AnalyticsReporting_DimensionFilter();
  402. $skuFilter->setDimensionName("ga:productName");
  403. $skuFilter->setOperator("EXACT");
  404. $skuFilter->setExpressions([$productName]);
  405. $MetricFilterClause=new \Google_Service_AnalyticsReporting_MetricFilterClause();
  406. $MetricFilterClause->setFilters(array($skuFilter));
  407. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  408. $request->setViewId($VIEW_ID);
  409. $request->setDateRanges($dateRange);
  410. $request->setMetrics($metrics);
  411. $request->setDimensions($dimensions);
  412. $request->setDimensionFilterClauses($MetricFilterClause);
  413. // $request->setOrderBys($orderBy);
  414. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  415. $body->setReportRequests( array( $request) );
  416. $reports= $analytics->reports->batchGet( $body );
  417. $result=$this->getResults($reports);
  418. return $result;
  419. }
  420. public function goals($dp,$start_at,$end_at){
  421. $analytics=self::getInstance($dp);
  422. $dimensionArr=[
  423. 'date'=>'ga:date',
  424. ];
  425. $metricArr=[
  426. 'pageviews'=>"ga:pageviews",
  427. 'sessionCount'=>"ga:sessionCount",
  428. 'users'=>"ga:users",
  429. 'newUsers'=>"ga:newUsers",
  430. 'bounceRate'=>"ga:bounceRate",
  431. 'goal3Completions'=>"ga:goal3Completions",
  432. 'goal3ConversionRate'=>"ga:goal3ConversionRate",
  433. 'goal7Completions'=>"ga:goal7Completions",
  434. 'goal7ConversionRate'=>"ga:goal7ConversionRate",
  435. 'transactionRevenue'=>"ga:transactionRevenue",
  436. 'revenuePerTransaction'=>"ga:revenuePerTransaction",
  437. ];
  438. $VIEW_ID = $this->getViewId($dp);//
  439. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  440. $dateRange->setStartDate($start_at);
  441. $dateRange->setEndDate($end_at);
  442. $metrics=[];
  443. $dimensions=[];
  444. foreach($metricArr as $k=>$v){
  445. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  446. $Metric->setExpression($v);
  447. $Metric->setAlias($k);
  448. array_push($metrics,$Metric);
  449. }
  450. foreach($dimensionArr as $k=>$v){
  451. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  452. $Dimension->setName($v);
  453. array_push($dimensions,$Dimension);
  454. }
  455. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  456. $request->setViewId($VIEW_ID);
  457. $request->setDateRanges($dateRange);
  458. $request->setMetrics($metrics);
  459. $request->setDimensions($dimensions);
  460. // $request->setOrderBys($orderBy);
  461. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  462. $body->setReportRequests( array( $request) );
  463. $reports= $analytics->reports->batchGet( $body );
  464. $result=$this->getResults($reports);
  465. return $result;
  466. }
  467. public function regionProduct($region,$dp,$start_at,$end_at){
  468. $analytics=self::getInstance($dp);
  469. $dimensionArr=[
  470. 'region'=>'ga:region',
  471. ];
  472. $metricArr=[
  473. 'itemRevenue'=>"ga:itemRevenue",
  474. 'revenuePerItem'=>"ga:revenuePerItem",
  475. 'productRefundAmount'=>"ga:productRefundAmount",
  476. 'quantityAddedToCart'=>"ga:quantityAddedToCart",
  477. // 'quantityRemovedFromCart'=>"ga:quantityRemovedFromCart",
  478. 'itemQuantity'=>"ga:itemQuantity",
  479. 'productListViews'=>"ga:productListViews",
  480. 'productDetailViews'=>"ga:productDetailViews",
  481. 'productAddsToCart'=>"ga:productAddsToCart",
  482. // 'productRemovesFromCart'=>"ga:productRemovesFromCart",
  483. 'productCheckouts'=>"ga:productCheckouts",
  484. // 'uniquePurchases'=>"ga:uniquePurchases",
  485. 'transactions'=>"ga:transactions",
  486. ];
  487. $VIEW_ID = $this->getViewId($dp);//
  488. $dateRange = new \Google_Service_AnalyticsReporting_DateRange();
  489. $dateRange->setStartDate($start_at);
  490. $dateRange->setEndDate($end_at);
  491. $metrics=[];
  492. $dimensions=[];
  493. foreach($metricArr as $k=>$v){
  494. $Metric = new \Google_Service_AnalyticsReporting_Metric();
  495. $Metric->setExpression($v);
  496. $Metric->setAlias($k);
  497. array_push($metrics,$Metric);
  498. }
  499. foreach($dimensionArr as $k=>$v){
  500. $Dimension = new \Google_Service_AnalyticsReporting_Dimension();
  501. $Dimension->setName($v);
  502. array_push($dimensions,$Dimension);
  503. }
  504. $regionFilter = new \Google_Service_AnalyticsReporting_DimensionFilter();
  505. $regionFilter->setDimensionName("ga:region");
  506. $regionFilter->setOperator("EXACT");
  507. $regionFilter->setExpressions([$region]);
  508. $MetricFilterClause=new \Google_Service_AnalyticsReporting_MetricFilterClause();
  509. $MetricFilterClause->setFilters(array($regionFilter));
  510. $request = new \Google_Service_AnalyticsReporting_ReportRequest();
  511. $request->setViewId($VIEW_ID);
  512. $request->setDateRanges($dateRange);
  513. $request->setMetrics($metrics);
  514. $request->setDimensions($dimensions);
  515. $request->setDimensionFilterClauses($MetricFilterClause);
  516. // $request->setOrderBys($orderBy);
  517. $body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
  518. $body->setReportRequests( array( $request) );
  519. $reports= $analytics->reports->batchGet( $body );
  520. $result=$this->getResults($reports);
  521. return $result;
  522. }
  523. /**
  524. * Queries the Analytics Reporting API V4.
  525. *
  526. * @param service An authorized Analytics Reporting API V4 service object.
  527. * @return The Analytics Reporting API V4 response.
  528. */
  529. function getContentReport($VIEW_ID,$analytics,$metricArr,$dimensionArr) {
  530. }
  531. function getResults($reports) {
  532. $data=[];
  533. for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
  534. $item=[];
  535. $report = $reports[ $reportIndex ];
  536. $header = $report->getColumnHeader();
  537. $dimensionHeaders = $header->getDimensions();
  538. $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
  539. $rows = $report->getData()->getRows();
  540. for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
  541. $row = $rows[ $rowIndex ];
  542. $dimensions = $row->getDimensions();
  543. $metrics = $row->getMetrics();
  544. if($dimensionHeaders&&$dimensions){
  545. for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
  546. $header = $dimensionHeaders[$i];
  547. $header=str_replace('ga:','',$header);
  548. $item[$header]=$dimensions[$i];
  549. // print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n\r");
  550. // echo"<br/>";
  551. }
  552. // $date=$dimensions[0];
  553. // $date=date('Y-m-d',strtotime($date));
  554. }else{
  555. continue;
  556. }
  557. if($metricHeaders&&$metrics){
  558. for ($j = 0; $j < count($metrics); $j++) {
  559. $values = $metrics[$j]->getValues();
  560. for ($k = 0; $k < count($values); $k++) {
  561. $entry = $metricHeaders[$k];
  562. $item[$entry->getName()]=$values[$k];
  563. // print($entry->getName() . ": " . $values[$k] . "\n\r");
  564. // echo"<br/>";
  565. }
  566. }
  567. }
  568. $data[]=$item;
  569. }
  570. }
  571. return $data;
  572. }
  573. }