dongtiao2105 2016-07-18 23:55
浏览 12
已采纳

在Google Analytics数据获取调用中应用if语句时出错

I get the following error when applying an if statement to my Google Analytics Data Get Call. Below is my code:

$results = $analytics->data_ga->get(
  'ga:' . $google_account[ 'profile' ],
  $fromDate,
  $toDate, 'ga:sessions,ga:percentNewSessions,ga:newUsers,ga:bounceRate,ga:pageviewsPerSession,ga:avgSessionDuration,ga:goalConversionRateAll,ga:goalCompletionsAll,ga:goalValueAll',
if($gapi_primary_dimension <> ""){
  array(
    'dimensions'  => 'ga:'.$gapi_primary_dimension,
    'sort'        => 'ga:sessions',
    'max-results' => 5
  )
} else if($gapi_primary_dimension <> "" && $gapi_secondary_dimension <> ""){
  array(
    'dimensions'  => 'ga:'.$gapi_primary_dimension,'ga:'.$gapi_secondary_dimension,
    'sort'        => 'ga:sessions',
    'max-results' => 5
  )     
} else {
    array(
    'dimensions'  => 'ga:channelGrouping',
    'sort'        => 'ga:sessions',
    'max-results' => 5
    )
}

);

The error is coming from the first if statement (line 5). Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • doufei2007 2016-07-19 00:06
    关注

    You can't use if/else statements in the arguments, please try this:

    if($gapi_primary_dimension <> ""){
      $array = array(
            'dimensions'  => 'ga:'.$gapi_primary_dimension,
            'sort'        => 'ga:sessions',
            'max-results' => 5
      );
    } elseif($gapi_primary_dimension <> "" && $gapi_secondary_dimension <> ""){
      $array = array(
            'dimensions'  => 'ga:'.$gapi_primary_dimension,'ga:'.$gapi_secondary_dimension,
            'sort'        => 'ga:sessions',
            'max-results' => 5
      );    
    } else {
        $array = array(
            'dimensions'  => 'ga:channelGrouping',
            'sort'        => 'ga:sessions',
            'max-results' => 5      
        );   
    }   
    
    $results = $analytics->data_ga->get(
      'ga:' . $google_account[ 'profile' ],
      $fromDate,
      $toDate, 'ga:sessions,ga:percentNewSessions,ga:newUsers,ga:bounceRate,ga:pageviewsPerSession,ga:avgSessionDuration,ga:goalConversionRateAll,ga:goalCompletionsAll,ga:goalValueAll', $array);
    

    I hope this will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计
  • ¥15 用联想小新14Pro
  • ¥15 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)
  • ¥15 基于8255的交通灯设计
  • ¥15 w3wp,exe 中发生未处理的 Microsoft ,NETFramework 异常。
  • ¥20 C51单片机程序及仿真(加减器)
  • ¥15 AQWA | 水动力分析 二阶波浪力
  • ¥15 c语言题目:成绩管理系统