2

I'm writing this Console Application to get some basic "Google Analytics Api-data" logic going. Everythings works fine but i wan't to add some more metrics to query to google analytics API. I wan't to add ga:newvisitors, ga:reaccuringvisitors etc. How do i implement this in the code below?

Here's a relevant piece of the code:

var gas = new AnalyticsService(new BaseClientService.Initializer()
      {
       HttpClientInitializer = credential,
       ApplicationName = "TestGoogleAnalytics",
       });

var r = gas.Data.Ga.Get("ga:ProfileID", "2010-02-24", "2014-02-24", "ga:visitors");

//Specify some addition query parameters
r.Dimensions = "ga:pagePath";
r.Sort = "-ga:visitors";
r.MaxResults = 10000;

//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
foreach (KeyValuePair<string, string> kvp in d.TotalsForAllResults)
    {
    Console.WriteLine("Total Visitors:" +" " + kvp.Value);
    }

Console.WriteLine(d.TotalsForAllResults.Keys + " = " + d.TotalsForAllResults.Values);
Console.ReadLine();

Thanks

WhoAmI
  • 1,188
  • 6
  • 17
  • 47

1 Answers1

3

Metrics are the last item in the get request just separate them with a comma.

var r = gas.Data.Ga.Get("ga:ProfileID", "2010-02-24", "2014-02-24", "ga:visitors,ga:newvisitors,ga:reaccuringvisitors");

Might be easer to read this way:

string Metrics = "ga:visitors,ga:newvisitors,ga:reaccuringvisitors";
var r = gas.Data.Ga.Get("ga:ProfileID", "2010-02-24", "2014-02-24", Metrics);

make sure you have the names right those don't look right to me but I didn't check: Dimension and metric reference .

Update: ga:newvisitors was removed back in 2009. I cant find any reference to ga:reaccuringvisitors. Make sure your requesting the correct metrics.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Hmm alright, i did something similar but formated the code the wrong way i guess. Thanks i'll try this =) – WhoAmI Feb 26 '14 at 12:40
  • Oh i didn't know they removed it, i need some more statistics to draw a chart. Reacurringvisitors dosn't excist but i think ga:percentNewVisits can be used. However i get this error when runing the code, the error accurs when i execute "r" like this Google.Apis.Analytics.v3.Data.GaData d = r.Execute(); The error says - "Parameter validation failed for "ids" – WhoAmI Feb 26 '14 at 12:53
  • 1
    ga:ProfileID is the Id of the profile in GA that you want to access. Go to Admin of the view its under View Settings - View Id its a number you have to put ga: in fount of it (ga:78110423) – Linda Lawton - DaImTo Feb 26 '14 at 13:00
  • 1
    It works now thank you! I got the id-problem when using the "string metrics" variant but the other alternative you provided works. I now get Total visitors and PercentNewVisits =) – WhoAmI Feb 26 '14 at 13:03