VB.Net - Adwords API获取域关键字,CPC和搜索量

时间:2017-11-25 11:34:02

标签: vb.net api keyword google-adwords

功能1:

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

此函数获取一个url作为输入,并以下列格式返回与该url相关的关键字作为输出:

KeywordName1 @@@ CPC @@@ SearchVolume KeywordName2 @@@ CPC @@@ SearchVolume

出于某种原因,无论我输入什么网站,它都会返回180个结果,

我知道pageSize设置为180 , 事实上,如果你将pageSize降低到179,你只能获得179个结果,问题是我无法获得更多然后180个结果..

可选帮助:也是为什么第一个函数中返回的CPC值与该函数返回的CPC值不同的原因是:

功能2:

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

如何在第一个功能中获得EXCAT CPC?

  

因为现在只有第二个函数返回好的CPC 而且   第一个函数返回错误的CPC(在以色列adwords前端检查)

如果您想知道如何使用这些功能(适合初学者): VB.Net - Trying To Increase the efficiency of adwords API requests

0 个答案:

没有答案