|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const GoogleAdsAuth = require("./get-auth"); |
| 4 | +const { NodeFetch } = require("./library/node-fetch"); |
| 5 | + |
| 6 | +class GoogleAdsReport { |
| 7 | + /** |
| 8 | + * @param {object} configs |
| 9 | + * @property {string} developerToken |
| 10 | + * @property {string} userAgent |
| 11 | + * @property {string} clientId |
| 12 | + * @property {string} clientSecret |
| 13 | + * @property {string} refreshToken |
| 14 | + * @property {string} loginCustomerId |
| 15 | + * @param {string} accessToken |
| 16 | + */ |
| 17 | + constructor(configs, accessToken) { |
| 18 | + this.configs = configs; |
| 19 | + this.accessToken = accessToken |
| 20 | + this.version = "v2"; |
| 21 | + this.auth = new GoogleAdsAuth(configs); |
| 22 | + } |
| 23 | + |
| 24 | + setOauthHeader() { |
| 25 | + return { |
| 26 | + "User-Agent": this.configs.userAgent, |
| 27 | + "Content-Type": "application/json", |
| 28 | + charset: "utf-8", |
| 29 | + Accept: "application/json", |
| 30 | + Authorization: `Bearer ${this.accessToken}`, |
| 31 | + "developer-token": this.configs.developerToken, |
| 32 | + "login-customer-id": this.configs.loginCustomerId |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * |
| 38 | + * @param {*} cid |
| 39 | + * @param {*} operations |
| 40 | + */ |
| 41 | + async customerCreateAccount(cid, operations) { |
| 42 | + if (!cid) throw new Error("CID Not Found"); |
| 43 | + if (!operations) throw new Error("Must Provide Operations"); |
| 44 | + cid = cid.replace(/-/g, ""); |
| 45 | + |
| 46 | + try { |
| 47 | + const header = this.setOauthHeader(); |
| 48 | + // Docs : https://developers.google.com/google-ads/api/reference/rpc/google.ads.googleads.v2.services#google.ads.googleads.v2.services.CreateCustomerClientRequest |
| 49 | + const endpoint = `https://googleads.googleapis.com/${this.version}/customers/${cid}:createCustomerClient`; |
| 50 | + const option = { |
| 51 | + method: "POST", |
| 52 | + body: JSON.stringify(operations), |
| 53 | + headers: header |
| 54 | + }; |
| 55 | + const response = await NodeFetch(endpoint, option); |
| 56 | + const json = await response.json(); |
| 57 | + |
| 58 | + return json; |
| 59 | + } catch (err) { |
| 60 | + console.log("Error ::>", err); |
| 61 | + throw new Error(err.message); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +module.exports = GoogleAdsReport; |
0 commit comments