-
Notifications
You must be signed in to change notification settings - Fork 199
/
GetProductCategoryTaxonomy.vb
executable file
·181 lines (158 loc) · 7.08 KB
/
GetProductCategoryTaxonomy.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
' Copyright 2018 Google LLC
'
' Licensed under the Apache License, Version 2.0 (the "License")
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'
' http:'www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.
Imports Google.Api.Ads.AdWords.Lib
Imports Google.Api.Ads.AdWords.v201809
Imports System
Imports System.Collections.Generic
Imports System.IO
Namespace Google.Api.Ads.AdWords.Examples.VB.v201809
''' <summary>
''' This code example fetches the set of valid ProductBiddingCategories.
''' </summary>
Public Class GetProductCategoryTaxonomy
Inherits ExampleBase
''' <summary>
''' Stores details about a product category and its hierarchy.
''' </summary>
Class ProductCategory
''' <summary>
''' The product category id.
''' </summary>
Private idField As Long
''' <summary>
''' The product category name.
''' </summary>
Private nameField As String
''' <summary>
''' The product category children.
''' </summary>
Private childrenField As New List(Of ProductCategory)
''' <summary>
''' Gets or sets the product category id.
''' </summary>
Public Property Id() As Long
Get
Return idField
End Get
Set(ByVal value As Long)
idField = value
End Set
End Property
''' <summary>
''' Gets or sets the product category name.
''' </summary>
Public Property Name() As String
Get
Return nameField
End Get
Set(ByVal value As String)
nameField = value
End Set
End Property
''' <summary>
''' Gets or sets the product category children.
''' </summary>
Public ReadOnly Property Children() As List(Of ProductCategory)
Get
Return childrenField
End Get
End Property
End Class
''' <summary>
''' Returns a description about the code example.
''' </summary>
Public Overrides ReadOnly Property Description() As String
Get
Return "This code example fetches the set of valid ProductBiddingCategories."
End Get
End Property
''' <summary>
''' Main method, to run this code example as a standalone application.
''' </summary>
''' <param name="args">The command line arguments.</param>
Public Shared Sub Main(ByVal args As String())
Dim codeExample As New GetProductCategoryTaxonomy
Console.WriteLine(codeExample.Description)
Try
codeExample.Run(New AdWordsUser)
Catch e As Exception
Console.WriteLine("An exception occurred while running this code example. {0}",
ExampleUtilities.FormatException(e))
End Try
End Sub
''' <summary>
''' Runs the code example.
''' </summary>
''' <param name="user">The AdWords user.</param>
Public Sub Run(ByVal user As AdWordsUser)
Using constantDataService As ConstantDataService = CType(
user.GetService(
AdWordsService.v201809.ConstantDataService),
ConstantDataService)
Dim selector As New Selector()
selector.predicates = New Predicate() { _
Predicate.In(
ProductBiddingCategoryData.Fields.
Country,
New String() {"US"})
}
Try
Dim results As ProductBiddingCategoryData() =
constantDataService.getProductBiddingCategoryData(selector)
Dim biddingCategories As New Dictionary(Of Long, ProductCategory)()
Dim rootCategories As New List(Of ProductCategory)()
For Each productBiddingCategory As ProductBiddingCategoryData In results
Dim id As Long = productBiddingCategory.dimensionValue.value
Dim parentId As Long = 0
Dim name As String = productBiddingCategory.displayValue(0).value
If Not (productBiddingCategory.parentDimensionValue Is Nothing) Then
parentId = productBiddingCategory.parentDimensionValue.value
End If
If Not biddingCategories.ContainsKey(id) Then
biddingCategories.Add(id, New ProductCategory())
End If
Dim category As ProductCategory = biddingCategories(id)
If (parentId <> 0) Then
If Not biddingCategories.ContainsKey(parentId) Then
biddingCategories.Add(parentId, New ProductCategory())
End If
Dim parent As ProductCategory = biddingCategories(parentId)
parent.Children.Add(category)
Else
rootCategories.Add(category)
End If
category.Id = id
category.Name = name
Next
DisplayProductCategories(rootCategories, "")
Catch e As Exception
Throw New System.ApplicationException("Failed to create shopping campaign.", e)
End Try
End Using
End Sub
''' <summary>
''' Displays the product categories.
''' </summary>
''' <param name="categories">The product categories.</param>
''' <param name="prefix">The prefix for display purposes.</param>
Sub DisplayProductCategories(ByVal categories As List(Of ProductCategory),
ByVal prefix As String)
For Each category As ProductCategory In categories
Console.WriteLine("{0}{1} [{2}]", prefix, category.Name, category.Id)
DisplayProductCategories(category.Children, String.Format("{0}{1} > ",
prefix, category.Name))
Next
End Sub
End Class
End Namespace