Miscellaneous Samples

The code samples below provide examples of miscellaneous management functions available in the AdWords API. Client Library.

Get all image assets

' 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

Namespace Google.Api.Ads.AdWords.Examples.VB.v201809
    ''' <summary>
    ''' This code example gets all image assets. To upload an image asset, run UploadImageAsset.vb.
    ''' </summary>
    Public Class GetAllImageAssets
        Inherits ExampleBase

        ''' <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 GetAllImageAssets
            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>
        ''' Returns a description about the code example.
        ''' </summary>
        Public Overrides ReadOnly Property Description() As String
            Get
                Return "This code example gets all image assets. To upload an image asset, run " +
                       "UploadImageAsset.vb."
            End Get
        End Property

        ''' <summary>
        ''' Runs the code example.
        ''' </summary>
        ''' <param name="user">The AdWords user.</param>
        Public Sub Run(ByVal user As AdWordsUser)
            Using assetService As AssetService = CType(
                user.GetService(
                    AdWordsService.v201809.AssetService),
                AssetService)

                ' Create the selector.
                Dim selector As New Selector()
                selector.fields = New String() { _
                                                   Asset.Fields.AssetName, Asset.Fields.AssetStatus,
                                                   ImageAsset.Fields.ImageFileSize,
                                                   ImageDimensionInfo.Fields.ImageWidth,
                                                   ImageDimensionInfo.Fields.ImageHeight,
                                                   ImageDimensionInfo.Fields.ImageFullSizeUrl
                                               }
                ' Filter for image assets only.
                selector.predicates = New Predicate() { _
                                                          Predicate.Equals(Asset.Fields.AssetSubtype,
                                                                           AssetType.IMAGE.ToString())
                                                      }
                selector.paging = Paging.Default

                Dim page As New AssetPage()

                Try
                    Do
                        ' Get the image assets.
                        page = assetService.get(selector)

                        ' Display the results.
                        If Not (page Is Nothing) AndAlso Not (page.entries Is Nothing) Then
                            Dim i As Integer = selector.paging.startIndex
                            For Each imageAsset As ImageAsset In page.entries
                                Console.WriteLine(
                                    "{0}) Image asset with id = '{1}', name = '{2}' and " +
                                    "status = '{3}' was found.", i + 1, imageAsset.assetId,
                                    imageAsset.assetName, imageAsset.assetStatus)
                                Console.WriteLine("  Size is {0}x{1} and asset URL is {2}.",
                                                  imageAsset.fullSizeInfo.imageWidth,
                                                  imageAsset.fullSizeInfo.imageHeight,
                                                  imageAsset.fullSizeInfo.imageUrl)
                                i = i + 1
                            Next
                        End If
                        selector.paging.IncreaseOffset()
                    Loop While (selector.paging.startIndex < page.totalNumEntries)
                    Console.WriteLine("Number of image assets found: {0}", page.totalNumEntries)
                Catch e As Exception
                    Throw New System.ApplicationException("Failed to retrieve image assets.", e)
                End Try
            End Using
        End Sub
    End Class
End Namespace

Get all images and videos

' 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

Namespace Google.Api.Ads.AdWords.Examples.VB.v201809
    ''' <summary>
    ''' This code example gets all videos And images. Use the Google Ads website to upload New
    ''' videos. To upload image, run UploadImage.vb.
    ''' </summary>
    Public Class GetAllVideosAndImages
        Inherits ExampleBase

        ''' <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 GetAllVideosAndImages
            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>
        ''' Returns a description about the code example.
        ''' </summary>
        Public Overrides ReadOnly Property Description() As String
            Get
        Return _
                  "This code example gets all videos and images. Use the Google Ads " &
                      "website to upload new videos. To upload image, run UploadImage.vb."
      End Get
    End Property

        ''' <summary>
        ''' Runs the code example.
        ''' </summary>
        ''' <param name="user">The AdWords user.</param>
        Public Sub Run(ByVal user As AdWordsUser)
            Using mediaService As MediaService = CType(
                user.GetService(
                    AdWordsService.v201809.MediaService),
                MediaService)

                ' Create the video selector.
                Dim selector As New Selector
                selector.fields = New String() { _
                                                   Media.Fields.MediaId, Dimensions.Fields.Width,
                                                   Dimensions.Fields.Height, Media.Fields.MimeType
                                               }

                ' Set the filter.
                Dim mediaTypes = New String() { _
                                                  MediaMediaType.VIDEO.ToString(),
                                                  MediaMediaType.IMAGE.ToString()
                                              }
                selector.predicates = New Predicate() { _
                                                          Predicate.In(Media.Fields.Type,
                                                                       mediaTypes)
                                                      }

                selector.paging = Paging.Default

                Dim page As New MediaPage

                Try
                    Do
                        page = mediaService.get(selector)

                        If ((Not page Is Nothing) AndAlso (Not page.entries Is Nothing)) Then
                            Dim i As Integer = selector.paging.startIndex

                            For Each media As Media In page.entries
                                If TypeOf media Is Video Then
                                    Dim video As Video = CType(media, [Video])
                                    Console.WriteLine(
                                        "{0}) Video with id '{1}' and name '{2}' was found.",
                                        i + 1, video.mediaId, video.name)
                                ElseIf TypeOf media Is Image Then
                                    Dim image As Image = CType(media, [Image])

                                    ' Preferred: Use image.dimensions.ToDict() if you are not on
                                    ' Mono.
                                    Dim dimensions As Dictionary(Of MediaSize, Dimensions) =
                                            MapEntryExtensions.ToDict (Of MediaSize, Dimensions)(
                                                image.dimensions)

                                    Console.WriteLine(
                                        "{0}) Image with id '{1}', dimensions '{2}x{3}', and " &
                                        "MIME type '{4}' was found.", i + 1, image.mediaId,
                                        dimensions(MediaSize.FULL).width,
                                        dimensions(MediaSize.FULL).height, image.mimeType)
                                End If
                                i = i + 1
                            Next
                        End If
                        selector.paging.IncreaseOffset()
                    Loop While (selector.paging.startIndex < page.totalNumEntries)
                    Console.WriteLine("Number of images and videos found: {0}",
                                      page.totalNumEntries)
                Catch e As Exception
                    Throw New System.ApplicationException("Failed to get images and videos.", e)
                End Try
            End Using
        End Sub
    End Class
End Namespace

Upload an image

' 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 Google.Api.Ads.Common.Util

Namespace Google.Api.Ads.AdWords.Examples.VB.v201809
    ''' <summary>
    ''' This code example uploads an image. To get images, run GetAllVideosAndImages.vb.
    ''' </summary>
    Public Class UploadImage
        Inherits ExampleBase

        ''' <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 UploadImage
            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>
        ''' Returns a description about the code example.
        ''' </summary>
        Public Overrides ReadOnly Property Description() As String
            Get
                Return _
                    "This code example uploads an image. To get images, run " &
                    "GetAllVideosAndImages.vb."
            End Get
        End Property

        ''' <summary>
        ''' Runs the code example.
        ''' </summary>
        ''' <param name="user">The AdWords user.</param>
        Public Sub Run(ByVal user As AdWordsUser)
            Using mediaService As MediaService = CType(
                user.GetService(
                    AdWordsService.v201809.MediaService),
                MediaService)

                ' Create the image.
                Dim image As New Image
                image.data = MediaUtilities.GetAssetDataFromUrl("https://goo.gl/3b9Wfh",
                                                                user.Config)
                image.type = MediaMediaType.IMAGE

                Try
                    ' Upload the image.
                    Dim result As Media() = mediaService.upload(New Media() {image})

                    ' Display the results.
                    If ((Not result Is Nothing) AndAlso (result.Length > 0)) Then
                        Dim newImage As Media = result(0)

                        ' Preferred: Use newImage.dimensions.ToDict() if you are not on Mono.
                        Dim dimensions As Dictionary(Of MediaSize, Dimensions) =
                                MapEntryExtensions.ToDict (Of MediaSize, Dimensions)(
                                    newImage.dimensions)

                        Console.WriteLine(
                            "Image with id '{0}', dimensions '{1}x{2}', and MIME type '{3}'" &
                            " was uploaded.", newImage.mediaId,
                            dimensions.Item(MediaSize.FULL).width,
                            dimensions.Item(MediaSize.FULL).height, newImage.mimeType)
                    Else
                        Console.WriteLine("No images were uploaded.")
                    End If
                Catch e As Exception
                    Throw New System.ApplicationException("Failed to upload images.", e)
                End Try
            End Using
        End Sub
    End Class
End Namespace

Upload an image asset

' 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 Google.Api.Ads.Common.Util

Namespace Google.Api.Ads.AdWords.Examples.VB.v201809
    ''' <summary>
    ''' This code example uploads an image asset. To get images, run GetAllImageAssets.vb.
    ''' </summary>
    Public Class UploadImageAsset
        Inherits ExampleBase

        ''' <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 UploadImageAsset
            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>
        ''' Returns a description about the code example.
        ''' </summary>
        Public Overrides ReadOnly Property Description() As String
            Get
                Return "This code example uploads an image asset. To get images, run " +
                       "GetAllImageAssets.vb."
            End Get
        End Property

        ''' <summary>
        ''' Runs the code example.
        ''' </summary>
        ''' <param name="user">The AdWords user.</param>
        Public Sub Run(ByVal user As AdWordsUser)
            Using assetService As AssetService = CType(
                user.GetService(
                    AdWordsService.v201809.AssetService),
                AssetService)

                ' Create the image asset.
                Dim imageAsset As New ImageAsset()
                ' Optional: Provide a unique friendly name to identify your asset. If you specify
                ' the assetName field, then both the asset name and the image being uploaded should be
                ' unique, and should not match another ACTIVE asset in this customer account.
                ' imageAsset.assetName = "Jupiter Trip " + ExampleUtilities.GetRandomString()
                imageAsset.imageData = MediaUtilities.GetAssetDataFromUrl("https://goo.gl/3b9Wfh",
                                                                          user.Config)

                ' Create the operation.
                Dim operation As New AssetOperation()
                operation.operator = [Operator].ADD
                operation.operand = imageAsset

                Try
                    ' Create the asset.
                    Dim result As AssetReturnValue = assetService.mutate(
                        New AssetOperation() _
                                                                            {operation})

                    ' Display the results.
                    If Not (result Is Nothing) AndAlso Not (result.value Is Nothing) _
                       AndAlso result.value.Length > 0 Then
                        Dim newAsset As Asset = result.value(0)

                        Console.WriteLine("Image asset with id = '{0}' and name = {1} was created.",
                                          newAsset.assetId, newAsset.assetName)
                    Else
                        Console.WriteLine("No image asset was created.")
                    End If
                Catch e As Exception
                    Throw New System.ApplicationException("Failed to upload image assets.", e)
                End Try
            End Using
        End Sub
    End Class
End Namespace

Upload an HTML5 zip file as a MediaBundle

' 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 Google.Api.Ads.Common.Util

Namespace Google.Api.Ads.AdWords.Examples.VB.v201809
    ''' <summary>
    ''' This code example uploads an HTML5 zip file.
    ''' </summary>
    Public Class UploadMediaBundle
        Inherits ExampleBase

        ''' <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 UploadMediaBundle
            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>
        ''' Returns a description about the code example.
        ''' </summary>
        Public Overrides ReadOnly Property Description() As String
            Get
                Return "This code example uploads an HTML5 zip file."
            End Get
        End Property

        ''' <summary>
        ''' Runs the code example.
        ''' </summary>
        ''' <param name="user">The AdWords user.</param>
        Public Sub Run(ByVal user As AdWordsUser)
            Using mediaService As MediaService = CType(
                user.GetService(
                    AdWordsService.v201809.MediaService),
                MediaService)

                Try
                    ' Create HTML5 media.
                    Dim html5Zip As Byte() =
                            MediaUtilities.GetAssetDataFromUrl("https://goo.gl/9Y7qI2",
                                                               user.Config)
                    ' Create a media bundle containing the zip file with all the HTML5 components.
                    Dim mediaBundleArray(1) As Media

                    Dim mediaBundle As New MediaBundle()
                    mediaBundle.data = html5Zip
                    mediaBundle.type = MediaMediaType.MEDIA_BUNDLE

                    mediaBundleArray(0) = mediaBundle

                    ' Upload HTML5 zip.
                    mediaBundleArray = mediaService.upload(mediaBundleArray)

                    ' Display HTML5 zip.
                    If (Not mediaBundleArray Is Nothing) AndAlso (mediaBundleArray.Length > 0) Then
                        Dim newBundle As Media = mediaBundleArray(0)

                        ' Preferred: Use newBundle.dimensions.ToDict() if you are not on Mono.
                        Dim dimensions As Dictionary(Of MediaSize, Dimensions) =
                                MapEntryExtensions.ToDict (Of MediaSize, Dimensions)(
                                    newBundle.dimensions)

                        Console.WriteLine("HTML5 media with id '{0}', dimensions '{1}x{2}', and " &
                                          "MIME type '{3}' was uploaded.", newBundle.mediaId,
                                          dimensions(MediaSize.FULL).width,
                                          dimensions(MediaSize.FULL).height,
                                          newBundle.mimeType)
                    Else
                        Console.WriteLine("No HTML5 zip was uploaded.")
                    End If
                Catch e As Exception
                    Throw New System.ApplicationException("Failed to upload HTML5 zip file.", e)
                End Try
            End Using
        End Sub
    End Class
End Namespace

Send feedback about...

AdWords API
AdWords API
Need help? Visit our support page.