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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmm alright, i did something similar but formated the code the wrong way i guess. Thanks i'll try this =)
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"
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)
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 =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.