<?php
putenv("GOOGLE_APPLICATION_CREDENTIALS=" . __DIR__ . '/key.json');
require_once __DIR__.'/vendor/autoload.php';

use Google\Analytics\Data\V1beta\Client\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\Filter\StringFilter;
use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\RunReportRequest;
use Google\Analytics\Data\V1beta\RunReportResponse;
//testing property 383258684
class ga_wrapper {
    private $_startDate = null;
    private $_endDate = null;
    private $_client;
    public function __construct($startDate,$endDate){
        $this->_startDate = $startDate;
        $this->_endDate = $endDate;
        $this->_client = new BetaAnalyticsDataClient();
    }

    public function getGAReportData($property_id){
        $request = (new RunReportRequest())
                    ->setProperty('properties/' . $property_id)
                    ->setDateRanges([new DateRange(['start_date' => $this->_startDate,'end_date' => $this->_endDate])])
                    ->setDimensions([
                                        new Dimension(['name' => 'date']),
                                        new Dimension(['name' => 'sessionCampaignName']),
                                        // new Dimension(['name' => 'pagePath']),
                                        // new Dimension(['name' => 'region']),
                                        // new Dimension(['name' => 'deviceCategory']),
                                        new Dimension(['name' => 'countryId']),
                                    ])
                    ->setDimensionFilter(
                                        new FilterExpression([
                                            'filter' => new Filter([
                                            'field_name' => 'sessionCampaignName',
                                            'string_filter' => new Filter\StringFilter([
                                                'match_type' => Filter\StringFilter\MatchType::BEGINS_WITH,
                                                'value' => 'search',
                                            ])
                                            ])
                                        ])
                    )
                    ->setMetrics([
                        new Metric(['name' => 'sessions']),
                        new Metric(['name' => 'screenPageViews']),
                    ])
                    ->setLimit(100000)
                    ->setOffset(0);
        $response = $this->_client->runReport($request);
        $finalData = [];
        foreach ($response->getRows() as $row) {
            $temp = [];
            $date = $row->getDimensionValues()[0]->getValue();
            $dateTime = DateTime::createFromFormat('Ymd', $date);
            $temp['date'] = $dateTime->format('Y-m-d');
            $temp['campaign'] = $row->getDimensionValues()[1]->getValue();
            // $temp['pagepath'] = $row->getDimensionValues()[2]->getValue();
            // $temp['region'] = $row->getDimensionValues()[3]->getValue();
            // $temp['device'] = $row->getDimensionValues()[4]->getValue();
            $temp['country'] = $row->getDimensionValues()[2]->getValue();
            $temp['session'] = $row->getMetricValues()[0]->getValue();
            $temp['pageview'] = $row->getMetricValues()[1]->getValue();
            $finalData[] = $temp;
        }
        return $finalData;
    }
}