Trying To Increase The Efficiency Of Adwords API VB.Net Code

50 views
Skip to first unread message

orrb...@gmail.com

unread,
Nov 27, 2017, 5:27:32 AM11/27/17
to AdWords API Forum
Lets Start With The Questions:
--
1) Why Function 1(DomainKeywords) Returns Only 180 Keywords?
(Please Note That pageSize Does Nothing When Increased But For Example If You Set pageSize To 179 It Works)
--
2) Function 1(DomainKeywords) Returns Different CPC From Function 3(KeywordCPC)
(Please Note That Function 3(KeywordCPC) Returns The Correct Number So Function 1(DomainKeywords) Is Has The Problem)
(The goal here is to minimize the traffic and operations on adwords)
(currently I have to call function 3(KeywordCPC) inside function 1(DomainKeywords) to make it work(get the correct CPC) but it slow down the process)
(so lets sum it up the question is there a way to get the EXACT cpc directly inside function 1 without calling function 3?)
(As you can see in function 1(DomainKeywords) I do not pass the Keyword Match Type)
(As you can see in function 1(DomainKeywords) I do not pass the Micro Amount)
(As you can see in function 1(DomainKeywords) I do not calculte the Max and Min CPC (function 3 does(KeywordCPC)))
(But Still in function 1(DomainKeywords) I get a number that suppose to be the CPC, looks like the CPC, but its not)
--
3) Basiclly what I need is two functions(im aware I have 3 at the moment)
so 2 funcs that do not spam google
1. First Function: Get URL -> Return Keyword,CPC,Volume | This one already exists(func 1) and 99% working but the CPC is wrong compared to the Adwords FrontEnd and to Function 3(KeywordCPC)
2. Second Function: Get Keyword -> Return CPC,Volume | Currently I have to request both of them with 2 seprate functions which triggers the QPS error constantly..
--
Final Notes:
Function 1: Gets The Correct CPC: NO
Function 2: Gets The Correct Search Volume: YES
Function 3: Gets The Correct CPC: YES
Problem: Too Many Requests
Goal: Minimize Requests
Vision: Find a Better Way
--
Meet My Functions:

Function 1: "DomainKeywords"
Input: "Random URL String"
Output: "180 Related Keywords And Their Search Volume"
Usage:
'Step 1:'Calling The API From a Windows Form Application..
Dim Find As New GetKeywordIdeas 'Where I Store The Functions..

'Step 2:'Gets 180 Related Keywords And Their Search Volume
Dim DomainKeywords As String = Find.DomainKeywords("www.PUT-DOMAIN-URL-HERE.com")

'Step 3:'For Each Line Equals To New Keyword
Dim spliter As String() = DomainKeywords.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

For Each keydat In spliter 'For Each Keyword
    If keydat <> String.Empty And keydat.Contains("ERROR") = False Then 'Just Making Sure No Suprizes..
        Application.DoEvents() 'Refreshing UI..
        Dim keyid As Integer = 0 'Counter That Helps The Split Proccess
        For Each dats In keydat.Split(CType("@@@", Char())) 'Keywords Are Returned Like This: KeyName@@@CPC@@@VOLUME
            If dats <> String.Empty Then
                keyid += 1
                If keyid = 1 Then
                    ListBox1.Items.Add(dats) 'Keyword Name
                End If
                If keyid = 2 Then
                    ListBox2.Items.Add(dats) 'Cost Per Click Aka CPC 'This Returns a Wrong Number Compared To Adwords Real CPC
                End If
                If keyid = 3 Then
                    ListBox3.Items.Add(dats) 'Search Volume
                End If
            End If
            Application.DoEvents() 'Refreshing UI..
        Next
    End If
Next
Function Code:
Public Function DomainKeywords(ByVal url As String) As String
    Dim output As String = ""
    Dim user As AdWordsUser = New AdWordsUser
    Using targetingIdeaService As TargetingIdeaService = CType(user.GetService(AdWordsService.v201710.TargetingIdeaService), TargetingIdeaService)
        Dim selector As New TargetingIdeaSelector()
        selector.requestType = RequestType.IDEAS
        selector.ideaType = IdeaType.KEYWORD
        selector.requestedAttributeTypes = New AttributeType() {AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME, AttributeType.AVERAGE_CPC, AttributeType.CATEGORY_PRODUCTS_AND_SERVICES}
        Dim searchParameters As New List(Of SearchParameter)
        Dim relatedToUrlSearchParameter As New RelatedToUrlSearchParameter
        relatedToUrlSearchParameter.urls = New String() {url}
        relatedToUrlSearchParameter.includeSubUrls = False
        searchParameters.Add(relatedToUrlSearchParameter)
        Dim languageParameter As New LanguageSearchParameter()
        Dim hebrew As New Language()
        hebrew.id = 1027
        languageParameter.languages = New Language() {hebrew}
        searchParameters.Add(languageParameter)
        Dim locationParameter As New LocationSearchParameter()
        Dim israel As New Location
        israel.id = 2376
        locationParameter.locations = New Location() {israel}
        searchParameters.Add(locationParameter)
        selector.searchParameters = searchParameters.ToArray()
        selector.paging = New Paging
        Dim page As New TargetingIdeaPage()
        Dim offset As Integer = 0
        Dim pageSize As Integer = 180
        Try
            Dim i As Integer = 0
            Do
                selector.paging.startIndex = offset
                selector.paging.numberResults = pageSize
                page = targetingIdeaService.get(selector)
                Dim keywordCheck As List(Of String) = New List(Of String)
                If Not page.entries Is Nothing AndAlso page.entries.Length > 0 Then
                    For Each targetingIdea As TargetingIdea In page.entries
                        For Each entry As Type_AttributeMapEntry In targetingIdea.data
                            Dim ideas As Dictionary(Of AttributeType, AdWords.v201710.Attribute) = MapEntryExtensions.ToDict(Of AttributeType, AdWords.v201710.Attribute)(targetingIdea.data)
                            Dim keyword As String = DirectCast(ideas(AttributeType.KEYWORD_TEXT), StringAttribute).value
                            Dim averageMonthlySearches As Long = DirectCast(ideas(AttributeType.SEARCH_VOLUME), LongAttribute).value
                            '''''''''''''''''''This Returns a Wrong Number
                            Dim cpc As Money = DirectCast(ideas(AttributeType.AVERAGE_CPC), MoneyAttribute).value
                            Dim microedit As String = Math.Round(cpc.microAmount / 1000000, 2).ToString + "$"
                            ''''''''''''''''''
                            Dim isExist As Boolean = False
                            For Each keycheck In keywordCheck
                                If keyword = keycheck Then
                                    isExist = True
                                End If
                            Next
                            If isExist = False Then
                                keywordCheck.Add(keyword)
                                If output = String.Empty Then
                                    output = keyword + "@@@" + microedit + "@@@" + averageMonthlySearches.ToString
                                Else
                                    output = output + Environment.NewLine + keyword + "@@@" + microedit + "@@@" + averageMonthlySearches.ToString
                                End If
                            End If
                        Next
                        i = i + 1
                    Next
                End If
                offset = offset + pageSize
            Loop While (offset < page.totalNumEntries)
        Catch e As Exception
            If output = String.Empty Then
                output = "ERROR"
                If e.Message.Contains("Rate exceeded") Then
                    MsgBox("rate exceeded")
                Else
                    MsgBox(e.Message.ToString)
                End If
            End If
        End Try
    End Using
    Return output
End Function

Function 2: "KeywordsVolume"
Input: "Random Keyword"
Output: "Keyword Volume"
Usage:
Dim Find As New GetKeywordIdeas 'Where I Store The Functions..
Find.KeywordsVolume(TextBoxWithKeywordName.Text) ' Returns Keyword Volume

'This Usage Method Get As Input "TextBoxWithKeywordName" And Return As Output To Listbox's The CPC and VOLUME of that Keyword
ListBoxOfKeywords.Items.Add(TextBoxWithKeywordName.Text) 'Add Keyword
ListBoxOfCPC.Items.Add(Find.KeywordCPC(TextBoxWithKeywordName.Text)) 'Using The KeywordCPC Function
ListBoxOfVolume.Items.Add(Find.KeywordsVolume(TextBoxWithKeywordName.Text)) 'Using The KeywordsVolume Function
Function Code:
Public Function KeywordsVolume(ByVal keywordName As String, Optional Tries As Integer = 0) As String
    Dim output As String = ""
    Dim user As AdWordsUser = New AdWordsUser
    Using targetingIdeaService As TargetingIdeaService = CType(user.GetService(AdWordsService.v201710.TargetingIdeaService), TargetingIdeaService)
        Dim selector As New TargetingIdeaSelector()
        selector.requestType = RequestType.STATS
        selector.ideaType = IdeaType.KEYWORD
        selector.requestedAttributeTypes = New AttributeType() {AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME, AttributeType.CATEGORY_PRODUCTS_AND_SERVICES}
        Dim searchParameters As New List(Of SearchParameter)
        Dim relatedToQuerySearchParameter As New RelatedToQuerySearchParameter()
        relatedToQuerySearchParameter.queries = New String() {keywordName}
        searchParameters.Add(relatedToQuerySearchParameter)
        Dim languageParameter As New LanguageSearchParameter()
        Dim hebrew As New Language()
        hebrew.id = 1027
        languageParameter.languages = New Language() {hebrew}
        searchParameters.Add(languageParameter)
        Dim locationParameter As New LocationSearchParameter()
        Dim israel As New Location
        israel.id = 2376
        locationParameter.locations = New Location() {israel}
        searchParameters.Add(locationParameter)
        selector.searchParameters = searchParameters.ToArray()
        selector.paging = New Paging
        Dim page As New TargetingIdeaPage()
        Dim offset As Integer = 0
        Dim pageSize As Integer = 500
        Try
            Dim i As Integer = 0
            Do
                selector.paging.startIndex = offset
                selector.paging.numberResults = pageSize
                page = targetingIdeaService.get(selector)
                Dim keywordCheck As List(Of String) = New List(Of String)
                If Not page.entries Is Nothing AndAlso page.entries.Length > 0 Then
                    For Each targetingIdea As TargetingIdea In page.entries
                        For Each entry As Type_AttributeMapEntry In targetingIdea.data
                            Dim ideas As Dictionary(Of AttributeType, AdWords.v201710.Attribute) = MapEntryExtensions.ToDict(Of AttributeType, AdWords.v201710.Attribute)(targetingIdea.data)
                            Dim keyword As String = DirectCast(ideas(AttributeType.KEYWORD_TEXT), StringAttribute).value
                            Dim averageMonthlySearches As Long = DirectCast(ideas(AttributeType.SEARCH_VOLUME), LongAttribute).value
                            If output = String.Empty Then
                                output = averageMonthlySearches.ToString
                                Exit For
                            End If
                        Next
                        If output <> String.Empty Then
                            Exit For
                        End If
                        i = i + 1
                    Next
                End If
                offset = offset + pageSize
            Loop While (offset < page.totalNumEntries)
        Catch e As Exception
            If output = String.Empty Then
                output = "ERROR"
                If e.Message.Contains("Rate exceeded") Then
                    output = KeywordsVolume(keywordName, Tries + 1)
                End If
            End If
        End Try
    End Using
    Return output
End Function

Function 2: "KeywordCPC"
Input: "Random Keyword"
Output: "Keyword CPC - Cost Per Click"
Usage:
Dim Find As New GetKeywordIdeas 'Where I Store The Functions..
Find.KeywordCPC(TextBoxWithKeywordName.Text) ' Returns Keyword CPC

'This Usage Method Get As Input "TextBoxWithKeywordName" And Return As Output To Listbox's The CPC and VOLUME of that Keyword
ListBoxOfKeywords.Items.Add(TextBoxWithKeywordName.Text) 'Add Keyword
ListBoxOfCPC.Items.Add(Find.KeywordCPC(TextBoxWithKeywordName.Text)) 'Using The KeywordCPC Function
ListBoxOfVolume.Items.Add(Find.KeywordsVolume(TextBoxWithKeywordName.Text)) 'Using The KeywordsVolume Function
Function Code:
Public Function KeywordCPC(keyName As String, Optional Tries As Integer = 0) As String
    Dim output As String = ""
    Dim user As AdWordsUser = New AdWordsUser
    Using trafficEstimatorService As TrafficEstimatorService = CType(user.GetService(AdWordsService.v201710.TrafficEstimatorService), TrafficEstimatorService)
        Dim keyword3 As New Keyword
        keyword3.text = keyName
        keyword3.matchType = KeywordMatchType.EXACT
        Dim keywords As Keyword() = New Keyword() {keyword3}
        Dim keywordEstimateRequests As New List(Of KeywordEstimateRequest)
        For Each keyword As Keyword In keywords
            Dim keywordEstimateRequest As New KeywordEstimateRequest
            keywordEstimateRequest.keyword = keyword
            keywordEstimateRequests.Add(keywordEstimateRequest)
        Next
        Dim adGroupEstimateRequest As New AdGroupEstimateRequest
        adGroupEstimateRequest.keywordEstimateRequests = keywordEstimateRequests.ToArray
        adGroupEstimateRequest.maxCpc = New Money
        adGroupEstimateRequest.maxCpc.microAmount = 1000000
        Dim campaignEstimateRequest As New CampaignEstimateRequest
        campaignEstimateRequest.adGroupEstimateRequests = New AdGroupEstimateRequest() {adGroupEstimateRequest}
        Dim countryCriterion As New Location
        countryCriterion.id = 2376
        Dim languageCriterion As New Language
        languageCriterion.id = 1027
        campaignEstimateRequest.criteria = New Criterion() {countryCriterion, languageCriterion}
        Try
            Dim selector As New TrafficEstimatorSelector
            selector.campaignEstimateRequests = New CampaignEstimateRequest() {campaignEstimateRequest}
            selector.platformEstimateRequested = False
            Dim result As TrafficEstimatorResult = trafficEstimatorService.get(selector)
            If ((Not result Is Nothing) AndAlso (Not result.campaignEstimates Is Nothing) AndAlso (result.campaignEstimates.Length > 0)) Then
                Dim campaignEstimate As CampaignEstimate = result.campaignEstimates(0)
                If ((Not campaignEstimate.adGroupEstimates Is Nothing) AndAlso (campaignEstimate.adGroupEstimates.Length > 0)) Then
                    Dim adGroupEstimate As AdGroupEstimate = campaignEstimate.adGroupEstimates(0)
                    If (Not adGroupEstimate.keywordEstimates Is Nothing) Then
                        For i As Integer = 0 To adGroupEstimate.keywordEstimates.Length - 1
                            Dim keyword As Keyword = keywordEstimateRequests.Item(i).keyword
                            Dim keywordEstimate As KeywordEstimate = adGroupEstimate.keywordEstimates(i)
                            If keywordEstimateRequests.Item(i).isNegative Then
                                Continue For
                            End If
                            Dim meanAverageCpc As Long = 0L
                            Dim meanAveragePosition As Double = 0
                            Dim meanClicks As Single = 0
                            Dim meanTotalCost As Single = 0
                            If (Not (keywordEstimate.min Is Nothing) AndAlso Not (keywordEstimate.max Is Nothing)) Then
                                If (Not (keywordEstimate.min.averageCpc Is Nothing) AndAlso Not (keywordEstimate.max.averageCpc Is Nothing)) Then
                                    meanAverageCpc = CLng((keywordEstimate.min.averageCpc.microAmount + keywordEstimate.max.averageCpc.microAmount) / 2)
                                End If
                            End If
                            output = Math.Round(meanAverageCpc / 1000000, 2).ToString + "$"
                        Next i
                    End If
                End If
            Else
                output = "ZERO"
            End If
        Catch e As Exception
            If output = String.Empty Then
                output = "ERROR"
                If e.Message.Contains("Rate exceeded") Then
                    output = KeywordCPC(keyName, Tries + 1)
                End If
            End If
        End Try
    End Using
    Return output
End Function

This code is just pure gold for me and anyone using the api in vb.net,
feel free to use this as its your own
Thank you for reading and for your help!

Sreelakshmi Sasidharan (AdWords API Team)

unread,
Nov 27, 2017, 3:44:54 PM11/27/17
to AdWords API Forum
Hi, 

All the three functions are different either in terms of the API service used or the type of request being made, which is why you are not getting consistent results from the API. DomainKeywords function is using TargetingIdeaService and checking for keyword ideas, KeywordsVolume is using TargetingIdeaService and getting the keyword stats, KeywordCPC is using TrafficEstimatorService. You can see that results for the corresponding operations on the AdWords UI are different as well. Ideally, results from the API and the corresponding UI view should be matching. Based on your use case, you may need to make 3 API calls to get all the details. You can check the guide on mapping of the Keyword planner to the API service for better understanding.

Are you getting different data from the equivalent AdWords API Service and the AdWords UI? If so, could you please share the AdWords UI screenshot with the targeting parameters and the SOAP logs for that API operation? Please use reply privately to author while sharing the details. 

Thanks,
Sreelakshmi, AdWords API Team
Reply all
Reply to author
Forward
0 new messages