Google Universal Analytics はサービスを終了したため、現在はこの記事で紹介しているコードは動作しません。
新しい Google Analytics 4 (GA4) プロパティを利用する必要があります。
GA4プロパティからのデータ取得はこちらの記事を参照してください。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Analytics;
using Google.GData.Extensions;
namespace GoogleAnalyticsPVGet
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string userName = textBox_User.Text;
string passWord = textBox_Password.Text;
string profileId = textBox_ProfileID.Text;
//Google Data Export API 2.4の場合
string apikey = "AIxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string dataFeedUrl = "https://www.googleapis.com/analytics/v2.4/data?key=" + apikey;
AnalyticsService service = new AnalyticsService("AnalyticsSampleApp(iPentec)");
if (!string.IsNullOrEmpty(userName)) {
service.setUserCredentials(userName, passWord);
}
DataQuery query = new DataQuery(dataFeedUrl);
query.Ids = profileId;
query.Metrics = "ga:pageviews";
query.Dimensions = "ga:pagePath";
query.Sort = "-ga:pageviews";
query.GAStartDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
query.GAEndDate = dateTimePicker_End.Value.ToString("yyyy-MM-dd");
DataFeed dataFeed = service.Query(query);
foreach (DataEntry entry in dataFeed.Entries) {
ListViewItem item = new ListViewItem(entry.Title.Text);
item.SubItems.Add(entry.Metrics[0].Value);
listView_PageView.Items.Add(item);
}
}
}
}
AnalyticsService service = new AnalyticsService("AnalyticsSampleApp(iPentec)");
if (!string.IsNullOrEmpty(userName)) {
service.setUserCredentials(userName, passWord);
}
AnalyticsServiceのインスタンスを作成します。その後setUserCredentials()メソッドを呼び出し認証情報を設定します。 DataQuery query = new DataQuery(dataFeedUrl);
query.Ids = profileId;
query.Metrics = "ga:pageviews";
query.Dimensions = "ga:pagePath";
query.Sort = "-ga:pageviews";
query.GAStartDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
query.GAEndDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
DataQueryクラスのインスタンスを作成し、取得するAnalyticsデータの条件を設定します。
DataFeed dataFeed = service.Query(query);
foreach (DataEntry entry in dataFeed.Entries) {
ListViewItem item = new ListViewItem(entry.Title.Text);
item.SubItems.Add(entry.Metrics[0].Value);
listView_PageView.Items.Add(item);
}
AnalyticsServiceのインスタンスのQuery()メソッドを呼び出し、PV数を取得します。foreachループを用いて取得したPVをListViewに追加します。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Analytics;
using Google.GData.Extensions;
namespace GoogleAnalyticsDataGet
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button_GetProfile_Click(object sender, EventArgs e)
{
string userName = textBox_User.Text;
string passWord = textBox_Pass.Text;
AccountQuery query = new AccountQuery();
AnalyticsService service = new AnalyticsService("AnalyticsSampleApp(iPentec)");
if (!string.IsNullOrEmpty(userName)) {
service.setUserCredentials(userName, passWord);
}
AccountFeed accountFeed = service.Query(query);
foreach (AccountEntry entry in accountFeed.Entries) {
ListViewItem item = new ListViewItem(entry.Title.Text);
//item.SubItems.Add(entry.Title.Text);
item.SubItems.Add(entry.ProfileId.Value);
listView_Profile.Items.Add(item);
}
}
private void button_GetPV_Click(object sender, EventArgs e)
{
string userName = textBox_User.Text;
string passWord = textBox_Pass.Text;
string profileId = listView_Profile.SelectedItems[0].SubItems[1].Text;
Google Data Export API 2.3の場合
const string dataFeedUrl = "https://www.google.com/analytics/feeds/data";
//Google Data Export API 2.4の場合
//string apikey = "AIxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//string feedUrl = "https://www.googleapis.com/analytics/v2.4/data?key=" + apikey;
AnalyticsService service = new AnalyticsService("AnalyticsSampleApp(iPentec)");
if (!string.IsNullOrEmpty(userName)) {
service.setUserCredentials(userName, passWord);
}
DataQuery query = new DataQuery(dataFeedUrl);
//query.Ids = profileId; //以前のコード
query.Ids = "ga:12345678"; // ← Google Analyticsの管理画面で調べたプロファイルIDをここに記載する
query.Metrics = "ga:pageviews";
query.Dimensions = "ga:pagePath";
query.Sort = "-ga:pageviews";
query.GAStartDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
query.GAEndDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
DataFeed dataFeed = service.Query(query);
foreach (DataEntry entry in dataFeed.Entries) {
ListViewItem item = new ListViewItem(entry.Title.Text);
item.SubItems.Add(entry.Metrics[0].Value);
listView_PageView.Items.Add(item);
}
}
}
}
AnalyticsService service = new AnalyticsService("AnalyticsSampleApp(iPentec)");
if (!string.IsNullOrEmpty(userName)) {
service.setUserCredentials(userName, passWord);
}
AnalyticsServiceのインスタンスを作成します。その後setUserCredentials()メソッドを呼び出し認証情報を設定します。 DataQuery query = new DataQuery(dataFeedUrl);
query.Ids = profileId;
query.Metrics = "ga:pageviews";
query.Dimensions = "ga:pagePath";
query.Sort = "-ga:pageviews";
query.GAStartDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
query.GAEndDate = dateTimePicker_Start.Value.ToString("yyyy-MM-dd");
DataQueryクラスのインスタンスを作成し、取得するAnalyticsデータの条件を設定します。
DataFeed dataFeed = service.Query(query);
foreach (DataEntry entry in dataFeed.Entries) {
ListViewItem item = new ListViewItem(entry.Title.Text);
item.SubItems.Add(entry.Metrics[0].Value);
listView_PageView.Items.Add(item);
}
AnalyticsServiceのインスタンスのQuery()メソッドを呼び出し、PV数を取得します。foreachループを用いて取得したPVをListViewに追加します。