Get number of monthly searches for specific keywords using Google Adwords API for .NET

a 夏天 提交于 2019-12-10 23:49:09

问题


I need help how to get number of monthly searches for given keywords.

I got Google Adwords API account and also money is loaded so i can make many requests. But i can't find the code from examples, how to get this info.

I'm using C# .NET, and have downloaded Google.AdWords API dll's.

Can you give me some simple example?


回答1:


There may be a more graceful way to get keywords, but I found that I needed an ad hoc report. I could not get this to work with the AdWords .Net Client Library, but building an HTTP request the old-fashioned way is not a huge chore.

This is my code for creating the request header:

var  request = (HttpWebRequest)WebRequest.Create(Properties.Settings.Default.AdHocReportsURL);
request.ContentType = "application/x-www-form-urlencoded";
request.Method      = "POST";
request.Headers.Add("Authorization: GoogleLogin auth=" + _authToken);
request.Headers.Add("clientCustomerId: " + _customerID.ToString("000-000-0000"));
request.Headers.Add("developerToken: "   + _developerToken);

You then need to append the XML spec for your ad hoc report. Here's my method:

private void  AppendReportSpec(HttpWebRequest request, ReportType reportType, IEnumerable<string> fields, DateTime startDate, DateTime endDate)
    {
    var  reportSpec = new StringBuilder("<reportDefinition><selector>");
    foreach (string field in fields)
        {
        reportSpec.Append("\t\t<fields>");
        reportSpec.Append(field);
        reportSpec.AppendLine("</fields>");
        }
    reportSpec.Append
        (
        @"<dateRange><min>{0}</min><max>{1}</max></dateRange>
        </selector>
        <reportName>Whatever</reportName>
        <reportType>{2}</reportType>
        <dateRangeType>CUSTOM_DATE</dateRangeType>
        <downloadFormat>CSV</downloadFormat>
        </reportDefinition>"
        );

    string  reportXml = String.Format(reportSpec.ToString(), startDate.ToString("yyyyMMdd"), endDate.ToString("yyyyMMdd"), reportType);

    using (var requestContent = new StreamWriter(request.GetRequestStream()))
        requestContent.Write("__rdxml=" + HttpUtility.UrlEncode(reportXml));
    }

Finally, you can parse the value in request.GetResponse() to get your data.

See http://code.google.com/apis/adwords/docs/appendix/reports.html#search-query for the list of report types (you'll probably want KEYWORDS_PERFORMANCE_REPORT) and the fields allowed in each.



来源:https://stackoverflow.com/questions/9929309/get-number-of-monthly-searches-for-specific-keywords-using-google-adwords-api-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!