質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Google Analytics

Google AnalyticsはGoogleが開発した無料のウェブ分析のソリューションです。複数のクライアント側のAPIとデータをエクスポートし管理するREST APIも格納されています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

プラグイン

プラグイン(plug-in)は、ソフトウェアアプリケーションの機能拡張の為に開発された、一組のソフトウェアコンポーネントのことを指します。

Q&A

0回答

2280閲覧

アナリティクスAPIを使用したWPプラグインが動きません

MyQuestioner

総合スコア57

Google Analytics

Google AnalyticsはGoogleが開発した無料のウェブ分析のソリューションです。複数のクライアント側のAPIとデータをエクスポートし管理するREST APIも格納されています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

プラグイン

プラグイン(plug-in)は、ソフトウェアアプリケーションの機能拡張の為に開発された、一組のソフトウェアコンポーネントのことを指します。

0グッド

0クリップ

投稿2018/09/06 15:39

編集2022/01/12 10:55

アナリティクス APIを使用したWPプラグインを数年前に人に作って頂いたのですが、1週間ぐらい前から動作をしなくなりました。
アナリティクスAPIと連携して、ランキング形式で表示させるものなのですが、操作をしてもページビュー数等が表示されなくなりました。
このプラグインは「Google Analyticator」というプラグインを改良されて作られたらしく、APIライブラリは全く同じものでした。
[Google Analyticator](https://ja.wordpress.org/plugins/google-analyticator/Google Analytics)
メインのphp

<?php define('GARCODE',"GoogleAnalyticsRankingCode"); define('GARTOKEN',"GoogleAnalyticsRankingToken"); define('GARWPID',"GoogleAnalyticsRankingWebPropertyID"); define('GARVID',"GoogleAnalyticsRankingViewID"); define('HIDDEN_IDS',"GoogleAnalyticsRankingHiddenIDs"); function GoogleAnalyticsRanking_Uninstall(){ delete_option(GARCODE); delete_option(GARTOKEN); delete_option(GARWPID); delete_option(GARVID); delete_option(HIDDEN_IDS); wp_clear_scheduled_hook('get_ranking_cron'); } class GoogleAnalyticsRanking { function __construct(){ if(!class_exists('Google_Client')){ require_once(dirname(__FILE__).'/google-api-php-client/src/Google_Client.php'); } if(!class_exists('Google_AnalyticsService')){ require_once(dirname(__FILE__).'/google-api-php-client/src/contrib/Google_AnalyticsService.php'); } foreach(json_decode(file_get_contents(dirname(__FILE__).'/client_id/client_id.json'),true) as $k => $v){ if(is_array($v)){ foreach($v as $k2 => $v2){ $this->$k2 = $v2; } }else{ $this->$k = $v; } } $this->DIR = @array_pop(explode("/",dirname(__FILE__))); $this->NONCE = md5(dirname(__FILE__)); if(function_exists('register_uninstall_hook'))register_uninstall_hook(__FILE__, 'GoogleAnalyticsRanking_Uninstall'); add_action('init', array($this,'init')); add_action('wp_ajax_get_posts_googleanalyticsranking',array($this,'wp_ajax_get_posts')); add_action('get_ranking_cron', array($this, 'get_ranking')); } function init(){ $this->client = new Google_Client(); $this->client->setApprovalPrompt('force'); $this->client->setAccessType('offline'); $this->client->setClientId($this->client_id); $this->client->setClientSecret($this->client_secret); $this->client->setRedirectUri($this->redirect_uris[0]); $this->client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly')); $this->token = get_option(GARTOKEN); $this->WebPropertyID = get_option(GARWPID); $this->ViewID = get_option(GARVID); if(!empty($this->token)){ $this->client->setAccessToken(json_encode($this->token)); $this->analytics = new Google_AnalyticsService($this->client); } add_action('admin_menu',array($this,'admin_menu')); } function wp_enqueue(){ wp_enqueue_script('googleclient','https://apis.google.com/js/client.js?onload=googleApiClientReady',array('jquery'),'',true); wp_enqueue_script('option_page',plugins_url($this->DIR).'/assets/js/option_page.js',array('jquery','googleclient'),'',true); } function admin_menu(){ $hook = add_menu_page('ランキング','ランキング','edit_others_posts',"{$this->DIR}_options_page",array($this,'options_page')); add_submenu_page("{$this->DIR}_options_page",'初期設定','初期設定','edit_others_posts',"{$this->DIR}_options_page",array($this,'options_page')); add_action('load-'.$hook,array($this,'options_page_update')); add_action('load-'.$hook,array($this,'wp_enqueue')); if(empty($this->token)){ add_action('load-'.$hook,array($this,'redirect_client_setting_page')); $hook = add_submenu_page("{$this->DIR}_options_page",'認証コード設定','認証コード設定','edit_others_posts',"{$this->DIR}_client_setting_page",array($this,'client_setting_page')); add_action('load-'.$hook,array($this,'client_setting_page_update')); }else{ remove_action('load-'.$hook,array($this,'redirect_client_setting_page')); } if($_GET['page'] === "{$this->DIR}_reset_page"){ delete_option('GoogleAnalyticsRankingData'); delete_option(GARCODE); delete_option(GARTOKEN); delete_option(GARWPID); delete_option(GARVID); wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=reset")); exit; } function options_page(){ require_once(dirname(__FILE__).'/assets/views/options_page.php'); } function options_page_update(){ if($_SERVER["REQUEST_METHOD"] == "POST"){ if(!empty($this->token)){ $my_nonce = filter_input(INPUT_POST,$this->NONCE)?filter_input(INPUT_POST,$this->NONCE):null; if(wp_verify_nonce($my_nonce, wp_create_nonce($this->DIR))){ $webproperty_id = filter_input(INPUT_POST,'webproperty_id'); if(!empty($webproperty_id)){ update_option(GARWPID,$webproperty_id); if($view_id = filter_input(INPUT_POST,'view_id')){ update_option(GARVID,$view_id); }else{ list($pre, $account_id, $post) = explode('-',$webproperty_id); $profiles = $this->analytics->management_profiles->listManagementProfiles($account_id,$webproperty_id); update_option(GARVID,$profiles['items'][0]['id']); } delete_option('GoogleAnalyticsRankingData'); exec("nohup php -c '' '".dirname(__FILE__)."/background.php' > /dev/null &"); wp_safe_redirect(admin_url("admin.php?page={$_GET['page']}&update=ok")); exit; }else{ wp_safe_redirect(admin_url("admin.php?page={$_GET['page']}&update=error")); exit; } }else{ wp_safe_redirect(admin_url("admin.php?page={$_GET['page']}&update=error")); exit; } }else{ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page")); exit; } } } function client_setting_page(){ require_once(dirname(__FILE__).'/assets/views/client_setting_page.php'); } function redirect_client_setting_page(){ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page")); exit; } function client_setting_page_update(){ if($_SERVER["REQUEST_METHOD"] == "POST"){ $my_nonce = filter_input(INPUT_POST,$this->NONCE)?filter_input(INPUT_POST,$this->NONCE):null; if(wp_verify_nonce($my_nonce, wp_create_nonce($this->DIR))){ $code = filter_input(INPUT_POST,'token_key'); if(!empty($code)){ $res = file_get_contents($this->token_uri, false, stream_context_create(array('http' => array( 'method' => 'POST', 'content' => http_build_query(array( 'code' => $code, 'grant_type' => 'authorization_code', 'redirect_uri' => $this->redirect_uris[0], 'client_id' => $this->client_id, 'client_secret' => $this->client_secret, )), )))); $token = json_decode($res, true); if(isset($token['error']) || empty($token)){ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=error")); exit; } delete_option(GARCODE); delete_option(GARTOKEN); if(!add_option(GARCODE,$code,'','no')){ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=error")); exit; } if(!add_option(GARTOKEN,$token,'','no')){ wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_client_setting_page&update=error")); exit; } } wp_safe_redirect(admin_url("admin.php?page={$this->DIR}_options_page")); exit; } } }

※追記です。
デバッグモードでプラグインを有効化すると
「Notice: Undefined index: page in」で次の箇所にエラーが表示されます。

if($_GET['page'] === "{$this->DIR}_reset_page")

Notice: Undefined indexのエラー対策として3パターン編集してみました。

if (isset($_GET['page'] && $_GET['page'] == "{$this->DIR}_reset_page")){

の場合、エラー内容
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

if (isset($_GET['page'] === "{$this->DIR}_reset_page")){

の場合、エラー内容
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

if (!empty($_GET['page'] === "{$this->DIR}_reset_page")){

の場合、「Notice: Undefined index: page in」エラーのまま

その他、edumpというデバッグプラグインでは
「Only variables should be passed by reference」というエラーが

$this->DIR = @array_pop(explode("/",dirname(__FILE__)));

に出ます。
こちらの対処方法も分からない状態です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

CHERRY

2018/09/07 06:26

作られた方に相談するわけにはいかないのでしょうか?
CHERRY

2018/09/07 06:28

WordPress をデバッグモードにして動かすとプラグインのファイルでエラーが出ていたりしないでしょうか?
MyQuestioner

2018/09/07 06:29

本来そうするべきなのですが、現在は音信不通となっておりまして…
MyQuestioner

2018/09/07 06:34

デバッグモードにしますと、Notice: Undefined index: created in /(略)/wp-content/plugins/GoogleAnalyticsRanking/google-api-php-client/src/auth/Google_OAuth2.php on line 324というエラー表示が出ます。
MyQuestioner

2018/09/07 06:35

このエラーは同じライブラリを使うGoogle Analyticatorでは表示されません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問