I am trying to get all the advertiser accounts under my MCC account and 
generate an ad performance report for each.  I wanted to run the report 
once - but that isn't allowed from the MCC account.  I'm coding in vb.net. 
  I have used some of the example code as my starting point.  I have the 
list of advertiser accounts under the MCC account - and I need to be able 
to change to the new ClientCustomerId.  The ClientCustomerId value 
specified in my app.config file is that of the MCC account.  How to I do 
this?  Right now when I run it - I am getting the 
error CUSTOMER_SERVING_TYPE_REPORT_MISMATCH because it is trying to run the 
report using the customer ID of the MCC account.

Thank you!

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Web
Imports System.Text

Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.Util.Reports
Imports Google.Api.Ads.AdWords.v201402



Namespace Google.Api.Ads.AdWords.Examples.VB.v201402

    Public Class DoubleClick_Adwords_Report_Download_With_AQWL


        Public Shared Sub Main(ByVal args As String())
            Try
                Dim AcctHeirarchy As New 
DoubleClick_Adwords_Report_Download_With_AQWL
                '  Get accounts (advertisers) under the MCM account ID that 
was supplied in the app.config file

                AcctHeirarchy.GetAccts(New AdWordsUser)

            Catch ex As System.Exception
                Console.WriteLine("An exception occurred", ex)
                Environment.Exit(-1)
            End Try
        End Sub

        Public Sub GetAccts(ByVal user As AdWordsUser)
            ' Get the ManagedCustomerService.
            Dim mcService As ManagedCustomerService = 
user.GetService(AdWordsService.v201402.ManagedCustomerService)
            mcService.RequestHeader.clientCustomerId = Nothing

            ' Create selector.
            Dim selector As New Selector()
            selector.fields = New String() {"Login", "CustomerId", "Name"}

            Try
                ' Get results.
                Dim page As ManagedCustomerPage = mcService.get(selector)

                ' Display serviced account graph.
                If Not page.entries Is Nothing Then
                    ' Create map from customerId to customer node.
                    Dim customerIdToCustomerNode As Dictionary(Of Long, 
ManagedCustomerTreeNode) = New Dictionary(Of Long, 
ManagedCustomerTreeNode)()

                    ' Create account tree nodes for each customer.
                    For Each customer As ManagedCustomer In page.entries
                        Dim node As New ManagedCustomerTreeNode()
                        node.Account = customer
                        customerIdToCustomerNode.Add(customer.customerId, 
node)
                    Next

                    ' For each link, connect nodes in tree.
                    If (Not page.links Is Nothing) Then
                        For Each link As ManagedCustomerLink In page.links
                            Dim managerNode As ManagedCustomerTreeNode = 
customerIdToCustomerNode(link.managerCustomerId)
                            Dim childNode As ManagedCustomerTreeNode = 
customerIdToCustomerNode(link.clientCustomerId)
                            childNode.ParentNode = managerNode
                            If (Not managerNode Is Nothing) Then
                                managerNode.ChildAccounts.Add(childNode)
                            End If
                        Next
                    End If

                    ' Find the root account node in the tree.
                    Dim rootNode As ManagedCustomerTreeNode = Nothing
                    For Each account As ManagedCustomer In page.entries
                        If 
(customerIdToCustomerNode(account.customerId).ParentNode Is Nothing) Then
                            rootNode = 
customerIdToCustomerNode(account.customerId)
                            Exit For
                        End If
                    Next

                    ' Display account tree.
                    ' Console.WriteLine("Login, CustomerId, Name")
                    ' Console.WriteLine(rootNode.ToTreeString(0, New 
StringBuilder()))

          *          'Generate Report for Each Account*
*                    For Each childAccount As ManagedCustomerTreeNode In 
rootNode.ChildAccounts*
*                        Dim AdWordsReport As New 
DoubleClick_Adwords_Report_Download_With_AQWL*
*                        Dim fileName As String = "Adwords" & "_" & 
Format(Now(), "yyyyMMddHHmmss").ToString() & "_AdwordsDailyReporting_" & 
childAccount.Account.customerId.ToString() & ".csv"*
*                        AdWordsReport.RunReport(New AdWordsUser, fileName)*
*                    Next*

                Else
                    Console.WriteLine("No accounts were found.")
                End If
            Catch ex As Exception
                Throw New System.ApplicationException("Failed to get 
accounts.", ex)
            End Try
        End Sub

        Public Sub RunReport(ByVal user As AdWordsUser, ByVal fileName As 
String)

            Dim query As String = "SELECT AccountDescriptiveName, Date, 
CampaignName, AdGroupName, Id, " &
                   "Clicks, Impressions, Cost, ViewThroughConversions, 
Conversions, ConversionsManyPerClick, Headline " &
                   "FROM AD_PERFORMANCE_REPORT WHERE Impressions > 0 DURING 
LAST_30_DAYS "

            Dim filePath As String = "K:\BulkLoad\DoubleClick" & 
Path.DirectorySeparatorChar & fileName

            Try
                Dim utilities As New ReportUtilities(user)
                utilities.ReportVersion = "v201402"
                utilities.DownloadClientReport(query, 
DownloadFormat.CSVFOREXCEL.ToString(), filePath)
                Console.WriteLine("Report was downloaded to '{0}'.", 
filePath)

            Catch ex As Exception
                Throw New System.ApplicationException("Failed to generate 
report.", ex)
            End Try

        End Sub

        Class ManagedCustomerTreeNode
            ''' <summary>
            ''' The parent node.
            ''' </summary>
            Private _parentNode As ManagedCustomerTreeNode

            ''' <summary>
            ''' The account associated with this node.
            ''' </summary>
            Private _account As ManagedCustomer

            ''' <summary>
            ''' The list of child accounts.
            ''' </summary>
            Private _childAccounts As New List(Of ManagedCustomerTreeNode)

            ''' <summary>
            ''' Gets or sets the parent node.
            ''' </summary>
            Public Property ParentNode() As ManagedCustomerTreeNode
                Get
                    Return _parentNode
                End Get
                Set(ByVal value As ManagedCustomerTreeNode)
                    _parentNode = value
                End Set
            End Property


            ''' <summary>
            ''' Gets or sets the account.
            ''' </summary>
            Public Property Account() As ManagedCustomer
                Get
                    Return _account
                End Get
                Set(ByVal value As ManagedCustomer)
                    _account = value
                End Set
            End Property

            ''' <summary>
            ''' Gets or sets the child accounts.
            ''' </summary>
            Public Property ChildAccounts() As List(Of 
ManagedCustomerTreeNode)
                Get
                    Return _childAccounts
                End Get
                Set(ByVal value As List(Of ManagedCustomerTreeNode))
                    _childAccounts = value
                End Set
            End Property

            ''' <summary>
            ''' Returns a <see cref="System.String"/> that represents this 
instance.
            ''' </summary>
            ''' <returns>
            ''' A <see cref="System.String"/> that represents this instance.
            ''' </returns>
            Public Overrides Function ToString() As String
                Dim login As String = "(no login)"
                If (Not String.IsNullOrEmpty(_account.login)) Then
                    login = _account.login
                End If
                Return String.Format("{0}, {1}, {2}", login, 
_account.customerId, _account.name)
            End Function

            Public Function ToTreeString(ByVal depth As Integer, ByVal sb 
As StringBuilder) As StringBuilder
                For i As Integer = 0 To depth * 2
                    sb.Append("-")
                Next
                sb.Append(Me)
                sb.Append("\n")
                For Each childAccount As ManagedCustomerTreeNode In 
_childAccounts
                    childAccount.ToTreeString(depth + 1, sb)
                Next
                Return sb
            End Function
        End Class
    End Class

End Namespace


-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+:
https://googleadsdeveloper.blogspot.com/
https://plus.google.com/+GoogleAdsDevelopers/posts
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to