I have the following code to query google analytics using the C# v2.3 api:
string username = \"SAMPLE@SAMPLE.COM\";
string pass = \"PASS\";
string gkey = \"?key=
Accessing Google Analtyics with v3 client library is really nice tbh. The only thing i havent worked out is how to send it a refreshtoken i have stored in the database. Im stuck with the one it stores for me on the Pc.
Request autentication: All the client scret stuff is stored in client_secret.json you can download the file from google apis consol. It will pop up a browser window if it doesnt have approval if it does it will just continue on.
private void Form1_Load(object sender, EventArgs e)
{
UserCredential credential;
using (var stream = new System.IO.FileStream("client_secret.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { AnalyticsService.Scope.AnalyticsReadonly },
"user", CancellationToken.None, new FileDataStore("Analytics.Auth.Store")).Result;
}
}
Now to access Google Analytics you need to make an analytics Service.
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "Analytics API sample",
});
All your calls will now be run against that.
Yours should end up being something like:
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(ga:34197921, new DateTime(2012, 1, 2).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"), ""ga:visits,ga:visitors,ga:visitBounceRate,ga:goalStartsAll,ga:goalCompletionsAll,ga:goal1Starts,ga:goal1Completions,ga:goal2Starts,ga:goal2Completions,ga:goal3Starts,ga:goal3Completions,ga:goal4Starts,ga:goal4Completions,ga:adCost,ga:totalValue");
request.Dimensions = "ga:medium,ga:campaign,ga:date,ga:isMobile,ga:isTablet,ga:searchKeyword,ga:hostname";
I have a blog post that runs though most of the diffrent calls you can make. http://daimto.com/google-analytics-api-v3-with-c/