Berikut ini merupakan Function dari Mendapatkan Keywords otomatis dari Adwords
- Code:
public static void GetKeywords()
{
WriteLog(DateTime.Now.ToString() + ":Start Keywords.");
AdWordsUser user = new AdWordsUser();
Config(user);
DataSet ds = adw_Get_adwAdGroupActive();
if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count <= 0)
return;
string xml = string.Empty;
int j = 0;
//int flag = 1;
adw_Delete_adwKeywordActiveImport();
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["AdGroupID"] != null && !string.IsNullOrEmpty(dr["AdGroupID"].ToString()))
{
// Get the AdGroupCriterionService.
AdGroupCriterionService adGroupCriterionService =
(AdGroupCriterionService)user.GetService(AdWordsService.v201309.AdGroupCriterionService);
// Create a selector.
Selector selector = new Selector();
selector.fields = new string[] { "Id", "AdGroupId", "KeywordText" };
// Select only keywords.
Predicate predicate = new Predicate();
predicate.field = "CriteriaType";
predicate.@operator = PredicateOperator.EQUALS;
predicate.values = new string[] { "KEYWORD" };
Predicate predicate2 = new Predicate();
predicate2.field = "AdGroupId";
predicate2.@operator = PredicateOperator.EQUALS;
predicate2.values = new string[] { dr["AdGroupID"].ToString() };
selector.predicates = new Predicate[] { predicate, predicate2 };
// Set the selector paging.
selector.paging = new Paging();
int offset = 0;
int pageSize = 500;
AdGroupCriterionPage page = new AdGroupCriterionPage();
try
{
do
{
selector.paging.startIndex = offset;
selector.paging.numberResults = pageSize;
// Get the keywords.
page = adGroupCriterionService.get(selector);
// Display the results.
if (page != null && page.entries != null)
{
//if (flag.Equals(1))
//{
// flag = 0;
//}
int i = offset;
string destinationURL = string.Empty;
foreach (AdGroupCriterion adGroupCriterion in page.entries)
{
bool isNegative = (adGroupCriterion is NegativeAdGroupCriterion);
// If you are retrieving multiple type of criteria, then you may
// need to check for
//
// if (adGroupCriterion is Keyword) { ... }
//
// to identify the criterion type.
Keyword keyword = (Keyword)adGroupCriterion.criterion;
BiddableAdGroupCriterion BiddableadGroupCriterion = null;
if (isNegative)
{
xml += string.Format("<Keyword><adGroupId>{0}</adGroupId><id>{1}</id><text>{2}</text><matchType>{3}</matchType><destinationUrl>{4}</destinationUrl><Type>{5}</Type></Keyword>",
adGroupCriterion.adGroupId, keyword.id, Microsoft.Security.Application.Encoder.XmlEncode(keyword.text), string.Empty,string.Empty,string.Empty);
Console.WriteLine("{0}) Negative keyword with ad group ID = '{1}', keyword ID " +
"= '{2}', and text = '{3}' was found.", i + 1, adGroupCriterion.adGroupId,
keyword.id, keyword.text);
}
else
{
destinationURL = string.Empty;
BiddableadGroupCriterion = (BiddableAdGroupCriterion)adGroupCriterion;
if (BiddableadGroupCriterion.criterion is Keyword)
{
destinationURL = BiddableadGroupCriterion.destinationUrl;
}
xml += string.Format("<Keyword><adGroupId>{0}</adGroupId><id>{1}</id><text>{2}</text><matchType>{3}</matchType><destinationUrl>{4}</destinationUrl><Type>{5}</Type></Keyword>",
adGroupCriterion.adGroupId, keyword.id, Microsoft.Security.Application.Encoder.XmlEncode(keyword.text), keyword.matchType, destinationURL, BiddableadGroupCriterion != null ? BiddableadGroupCriterion.userStatus.ToString(): UserStatus.ACTIVE.ToString());
Console.WriteLine("{0}) Keyword with ad group ID = '{1}', keyword ID = '{2}', " +
"text = '{3}' and matchType = '{4} was found.", i + 1,
adGroupCriterion.adGroupId, keyword.id, keyword.text, keyword.matchType);
}
i++;
j++;
if (j >= 200 && !string.IsNullOrEmpty(xml))
{
adw_Update_adwKeywordActiveImport(string.Format("<Keywords>{0}</Keywords>", xml));
j = 0;
xml = string.Empty;
}
}
}
offset += pageSize;
} while (offset < page.totalNumEntries);
Console.WriteLine("Number of keywords found: {0}", page.totalNumEntries);
}
catch (Exception ex)
{
WriteLog(DateTime.Now.ToString() + "Failed to retrieve keywords." + ex);
}
}
}
if (!string.IsNullOrEmpty(xml))
{
adw_Update_adwKeywordActiveImport(string.Format("<Keywords>{0}</Keywords>", xml));
}
Thread.Sleep(60000);
adw_Update_adwKeywordActive();
WriteLog(DateTime.Now.ToString() + ":End Keywords.");
}