Click here to Skip to main content
15,746,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am getting below error while accessing the GA through my C# code

<pre lang="c#">
Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Can some one please help me out .

Thanks in advance.

Adding code

Int32 _intVisists = 0;
Int32 _intPageViewVisit = 0;
// your Google analytics username and password here
string userName = "";
string passWord = "";
string gkey = ""; //this is my google analytics aap key

string dataFeedUrl = "";

AccountQuery query = new AccountQuery();
AnalyticsService service = new AnalyticsService("Project name");
if (!string.IsNullOrEmpty(userName))
service.setUserCredentials(userName, passWord);
string str = "ga:123456789";

DataQuery query1 = new DataQuery(dataFeedUrl);

//Data come for FROM date to Date

query1.Ids = str;
query1.Metrics = "ga:socialInteractions";//visitors
query1.Dimensions = "ga:pagePath";
//query1.Sort = "ga:visits";
query1.GAStartDate = "2014-01-27";
query1.GAEndDate = "2014-02-10";
DataFeed dataFeedVisits = service.Query(query1);
foreach (DataEntry entry in dataFeedVisits.Entries)
string st = entry.Title.Text;
string ss = entry.Metrics[0].Value;
_intVisists = Int32.Parse(ss);
Response.Write("Total Visits : " + ss);

// total New visits (count + %)
query1.Ids = str;
query1.Metrics = "ga:newVisits";//visitors
query1.Sort = "ga:newVisits";

query1.GAStartDate = (DateTime.Now).AddDays(-30).ToString("yyyy-MM-dd");
query1.GAEndDate = (DateTime.Now).ToString("yyyy-MM-dd");
query1.StartIndex = 1;

DataFeed dataFeedNwVisits = service.Query(query1);
Updated 10-Feb-14 3:21am
ZurdoDev 10-Feb-14 7:52am    
No one can help you unless you post your code. Something about your request was bad, you'll have to figure it out.
suhel_khan 11-Feb-14 6:49am    
Hi Ryan,

I have attached the Code can you please have a look?
ZurdoDev 11-Feb-14 6:55am    
I see you define gkey, for example, but then never use it.
suhel_khan 11-Feb-14 7:28am    
Hi Ryan,

I added the gKey also, still I am getting the same error.
string dataFeedUrl = "" + gkey;
ZurdoDev 11-Feb-14 7:49am    
I would see if google has a forum. You'll likely get a quicker response there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900