How to use google-ads-node - 10 common examples

To help you get started, we’ve selected a few google-ads-node examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Opteo / google-ads-api / src / grpc.ts View on Github external
public buildSearchRequest(
        customer_id: string,
        query: string,
        page_size?: number,
        page_token?: string
    ): BuildSearchRequestResponse {
        const request = new SearchGoogleAdsRequest()
        request.setCustomerId(customer_id)
        request.setQuery(query)

        if (page_size) {
            request.setPageSize(page_size)
        }
        if (page_token) {
            request.setPageToken(page_token)
        }

        const has_limit = query.toLowerCase().includes(' limit ')
        let limit = 0 // The default limit is 0, which means no limit.
        if (has_limit) {
            limit = +query
                .toLowerCase()
                .split(' limit ')[1]
github Opteo / google-ads-api / src / services / customer.ts View on Github external
if (!operation_mode) {
                operation_mode = 'create'
            }

            if (operation_mode !== 'create' && operation_mode !== 'update' && operation_mode !== 'delete') {
                throw new Error(`"_operation" field must be one of "create"|"update"|"delete"`)
            }

            if (operation_mode === 'create') {
                resource_operation.setCreate(pb)
            }

            if (operation_mode === 'update') {
                resource_operation.setUpdate(pb)
                const update_mask = getFieldMask(operation)
                resource_operation.setUpdateMask(update_mask)
            }

            if (operation_mode === 'delete') {
                // @ts-ignore Types are no use here
                if (!pb.toObject().hasOwnProperty('resourceName') || !pb.toObject().resourceName) {
                    throw new Error(`Must specify "resource_name" to remove when using "delete"`)
                }
                // @ts-ignore Types are no use here
                resource_operation.setRemove(pb.toObject().resourceName)
            }

            /* Add operation of resource type to global mutate operation e.g. "MutateOperation.setCampaignBudgetOperation" */
            const op = new grpc.MutateOperation()
            const operation_set_method = `set${operation_resource_name}Operation`
            // @ts-ignore Types are no use here
github Opteo / google-ads-api / src / services / service.ts View on Github external
const operationType = (grpc as any)[options.operation]

        const operations = []

        // If the user passed in only one entity, convert it to an array of length 1
        if (!Array.isArray(options.entity[1])) {
            options.entity[1] = [options.entity[1]]
        }

        for (const entity of options.entity[1] as Array) {
            const operation = new operationType()

            const pb = this.buildResource(options.entity[0], entity)
            operation.setUpdate(pb)

            const mask = getFieldMask(entity)
            operation.setUpdateMask(mask)

            operations.push(operation)
        }

        return this.mutate(request, operations, options)
    }
github Opteo / google-ads-api / src / grpc.ts View on Github external
refresh_token: string,
        login_customer_id?: string,
        gads_node_options?: GoogleAdsNodeOptions
    ) {

        const additional_options: any = {}

        // Apply google-ads-node options if specified
        if(gads_node_options?.prevent_mutations) {
            additional_options.preventMutations = gads_node_options.prevent_mutations
        }
        if(gads_node_options?.logging) {
            additional_options.logging = gads_node_options.logging
        }

        this.client = new GoogleAdsClient({
            developer_token,
            client_id,
            client_secret,
            refresh_token,
            login_customer_id,
            parseResults: true,
            async accessTokenGetter(clientId: string, clientSecret: string, refreshToken: string) {
                return getAccessToken({
                    client_id: clientId,
                    client_secret: clientSecret,
                    refresh_token: refreshToken,
                })
            },
            ...additional_options
        })
    }
github Opteo / google-ads-api / src / services / customer.ts View on Github external
resource_operation.setUpdate(pb)
                const update_mask = getFieldMask(operation)
                resource_operation.setUpdateMask(update_mask)
            }

            if (operation_mode === 'delete') {
                // @ts-ignore Types are no use here
                if (!pb.toObject().hasOwnProperty('resourceName') || !pb.toObject().resourceName) {
                    throw new Error(`Must specify "resource_name" to remove when using "delete"`)
                }
                // @ts-ignore Types are no use here
                resource_operation.setRemove(pb.toObject().resourceName)
            }

            /* Add operation of resource type to global mutate operation e.g. "MutateOperation.setCampaignBudgetOperation" */
            const op = new grpc.MutateOperation()
            const operation_set_method = `set${operation_resource_name}Operation`
            // @ts-ignore Types are no use here
            op[operation_set_method](resource_operation)

            /* Push operation to MutateOperationsList */
            ops.push(op)
        }

        request.setMutateOperationsList(ops)
        const response = await this.globalMutate(request)

        return response
    }
}
github Opteo / google-ads-api / src / services / customer.ts View on Github external
public async update(customer: Customer, options?: ServiceCreateOptions): UpdateResponse {
        const request = new grpc.MutateCustomerRequest()
        const operation = new grpc.CustomerOperation()

        const pb = this.buildResource('Customer', customer) as grpc.Customer
        operation.setUpdate(pb)

        const mask = getFieldMask(customer)
        operation.setUpdateMask(mask)

        request.setCustomerId(this.cid)
        request.setOperation(operation)

        if (options && options.hasOwnProperty('validate_only')) {
            request.setValidateOnly(options.validate_only as boolean)
        }

        await this.service.mutateCustomer(request)
    }
github Opteo / google-ads-api / src / services / recommendation.ts View on Github external
const operations = resourceName.map(name => {
                const operation = new ApplyRecommendationOperation()
                operation.setResourceName(name)
                return operation
            })
            request.setOperationsList(operations)
github Opteo / google-ads-api / src / services / recommendation.ts View on Github external
public async applyRecommendation(
        resourceName: string | string[],
        options?: ServiceCreateOptions
    ): Promise {
        const request = new ApplyRecommendationRequest()
        request.setCustomerId(this.cid)

        if (resourceName instanceof Array) {
            const operations = resourceName.map(name => {
                const operation = new ApplyRecommendationOperation()
                operation.setResourceName(name)
                return operation
            })
            request.setOperationsList(operations)
        } else {
            const operation = new ApplyRecommendationOperation()
            operation.setResourceName(resourceName)
            request.setOperationsList([operation])
        }

        if (options && options.hasOwnProperty('partial_failure')) {
            if (!request.setPartialFailure) {
                throw new Error(`This method does not support the partial_failure option.`)
            }
            request.setPartialFailure(options.partial_failure as boolean)
        }

        const response: ApplyRecommendationResponse.AsObject = await this.service.applyRecommendation(request)
        return {
            request: request.toObject(),
            partial_failure_error: response.partialFailureError,
            results: response.resultsList.map(r => r.resourceName),
github Opteo / google-ads-api / src / services / recommendation.ts View on Github external
const operations = resourceName.map(name => {
                const operation = new DismissRecommendationRequest.DismissRecommendationOperation()
                operation.setResourceName(name)
                return operation
            })
            request.setOperationsList(operations)
github Opteo / google-ads-api / src / services / recommendation.ts View on Github external
public async dismissRecommendation(
        resourceName: string | string[],
        options?: ServiceCreateOptions
    ): Promise {
        const request = new DismissRecommendationRequest()
        request.setCustomerId(this.cid)

        if (resourceName instanceof Array) {
            const operations = resourceName.map(name => {
                const operation = new DismissRecommendationRequest.DismissRecommendationOperation()
                operation.setResourceName(name)
                return operation
            })
            request.setOperationsList(operations)
        } else {
            const operation = new DismissRecommendationRequest.DismissRecommendationOperation()
            operation.setResourceName(resourceName)
            request.setOperationsList([operation])
        }

        if (options && options.hasOwnProperty('partial_failure')) {
            if (!request.setPartialFailure) {
                throw new Error(`This method does not support the partial_failure option.`)
            }
            request.setPartialFailure(options.partial_failure as boolean)
        }

        const response: DismissRecommendationResponse.AsObject = await this.service.dismissRecommendation(request)
        return {
            request: request.toObject(),
            partial_failure_error: response.partialFailureError,
            results: response.resultsList.map(r => r.resourceName),