PHP Google Analytics API - Simple example

前端 未结 3 819
既然无缘
既然无缘 2021-02-04 16:49

I am trying to set some basic example of using Google Analytics with this library: https://github.com/google/google-api-php-client

For starter I have:

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-04 16:54

    what you could do is create a new function...

    function ga_campaign_transactions($gaEmail, $gaPass, $gProfile, $limit)
    {
        require_once('classes/google-analytics/gapi.class.php');
    
        $gDimensions = array('campaign');
        $gMetrics = array('transactions');
        $gSortMetric = NULL;
        $gFilter = '';
        $gSegment = '';
        $gStartDate = '2013-12-25';
        $gEndDate = '2014-01-08';
        $gStartIndex = 1;
        $gMaxResults = $limit;
    
        $ga = new gapi($gaEmail, $gaPass);
        $ga->requestReportData($gProfile, $gDimensions, $gMetrics, $gSortMetric, $gFilter, $gSegment, $gStartDate, $gEndDate, $gStartIndex, $gMaxResults);
    
        $gAnalytics_results = $ga->getResults();
    
        //RETURN RESULTS
        return $gAnalytics_results;
    
    }
    
    
    $gProfile = '123456';              // The Profile ID for the account, NOT GA:
    $gaEmail = 'YOUR GOOGLE EMAIL';    // Google Email address.
    $gaPass  = 'YOUR GOOGLE PASSWORD'; // Google Password.
    // NOTE: if 2 step login is turned on, create an application password.
    
    $limit   = 50;
    $ga_campaign_transactions = ga_campaign_transactions($gaEmail, $gaPass, $gProfile, $limit)
    
    //OUTPUT
    if(!empty($ga_campaign_transactions))
    {
        $counter=0;
        $gaCampResults= array(); // CREATE ARRAY TO STORE ALL RESULTS
        foreach($ga_campaign_transactions as $row)
        {
    
            $dim_list = $row->getDimesions();
            $met_list = $row->getMetrics();
    
            $gaCampResults[$counter]['campaign'] = $dim_list['campaign'];
            $gaCampResults[$counter]['transactions'] = $met_list['transactions'];
        $counter++;
        }
     }
    
    
     if(!empty($gaCampResults))
     {
         $totalCampTransactions = count($gaCampResults);
         ?>
         

    We Found ( ) Results

      Campaign:".$gaRow['campaign']." | Transactions: ".$gaRow['transactions'].""; } ?>

    find Analytics Profile ID

    Create Google Application password

    Hopefully that puts you on the right track :) untested this, but similar to what I've been using...

    Marty

提交回复
热议问题