@google-cloud/nodejs-common 0.9.5-beta → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -60
- package/bin/install_functions.sh +67 -37
- package/package.json +15 -15
- package/src/apis/google_ads.js +114 -7
- package/src/apis/youtube.js +89 -1
package/README.md
CHANGED
|
@@ -1,69 +1,78 @@
|
|
|
1
1
|
# NodeJS Common Library
|
|
2
2
|
|
|
3
|
-
<!--* freshness: { owner: 'lushu' reviewed: '2021-
|
|
4
|
-
|
|
5
|
-
A NodeJs common library for other projects, e.g. [GMP and Google Ads
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
1.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
1.
|
|
12
|
-
Connector]:
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
1.
|
|
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
|
-
|
|
3
|
+
<!--* freshness: { owner: 'lushu' reviewed: '2021-12-02' } *-->
|
|
4
|
+
|
|
5
|
+
A NodeJs common library for other projects, e.g. [GMP and Google Ads Connector]
|
|
6
|
+
and [Data Tasks Coordinator]. This library includes:
|
|
7
|
+
|
|
8
|
+
1. Authentication wrapper based on google auth library to support OAuth, JWT and
|
|
9
|
+
ADC authentication;
|
|
10
|
+
|
|
11
|
+
1. Wrapper for some Google APIs for integration, mainly
|
|
12
|
+
for [GMP and Google Ads Connector]:
|
|
13
|
+
|
|
14
|
+
* Google Analytics data import
|
|
15
|
+
* Google Analytics measurement protocol
|
|
16
|
+
* Campaign Manager offline conversion upload
|
|
17
|
+
* Search Ads 360 conversions upload
|
|
18
|
+
* Google Ads click conversions upload
|
|
19
|
+
* Google Ads customer match upload
|
|
20
|
+
* Google Ads conversions scheduled uploads based on Google Sheets
|
|
21
|
+
* Measurement Protocol Google Analytics 4
|
|
22
|
+
|
|
23
|
+
1. Wrapper for some Google APIs for reporting, mainly
|
|
24
|
+
for [Data Tasks Coordinator]:
|
|
25
|
+
|
|
26
|
+
* Google Ads reporting
|
|
27
|
+
* Campaign Manager reporting
|
|
28
|
+
* Search Ads 360 reporting
|
|
29
|
+
* Display and Video 360 reporting
|
|
30
|
+
* YouTube Data API
|
|
31
|
+
* Ads Data Hub querying
|
|
32
|
+
|
|
33
|
+
1. Utilities wrapper class for Google Cloud Products:
|
|
34
|
+
|
|
35
|
+
* **Firestore Access Object**: Firestore has two modes which are excluded to
|
|
36
|
+
each other and can't be changed once selected in a Cloud Project[[2]].
|
|
37
|
+
This class, with its two successors offer a unified interface to operate
|
|
38
|
+
data objects on either Firestore or Datastore.
|
|
39
|
+
|
|
40
|
+
* **AutoMl Tables API**: Offers a unified entry to use this API based on
|
|
41
|
+
Google Cloud client library combined with REST requests to service
|
|
42
|
+
directly due to some functionalities missed in the client library.
|
|
43
|
+
|
|
44
|
+
* **Vertex AI API**: Offers a unified entry to use this API based on Google
|
|
45
|
+
Cloud client library.
|
|
46
|
+
|
|
47
|
+
* **Pub/Sub Utilities**: Offers utilities functions to create topics and
|
|
48
|
+
subscriptions for Pub/Sub, as well as the convenient way to publish a
|
|
49
|
+
message.
|
|
50
|
+
|
|
51
|
+
* **Storage Utilities**: Offers functions to manipulate the files on Cloud
|
|
52
|
+
Storage. The main functions are:
|
|
53
|
+
|
|
54
|
+
* Reading a given length (or slightly less) content without breaking a
|
|
55
|
+
line;
|
|
56
|
+
* Splitting a file into multiple files with the given length (or
|
|
57
|
+
slightly less) without breaking a line;
|
|
58
|
+
* Merging files into one file.
|
|
59
|
+
|
|
60
|
+
* **Cloud Scheduler Adapter**: A wrapper to pause and resume Cloud Scheduler
|
|
61
|
+
jobs.
|
|
62
|
+
|
|
63
|
+
* **Cloud Functions Adapter**: Cloud Functions have different parameters in
|
|
64
|
+
different environments, e.g. Node6 vs Node8[[1]]. This utility file offers
|
|
65
|
+
an adapter to wrap a Node8 Cloud Functions into Node6 and Node8 compatible
|
|
66
|
+
functions.
|
|
67
|
+
|
|
63
68
|
1. A share library for [Bash] to facilitate installation tasks.
|
|
64
69
|
|
|
65
70
|
[GMP and Google Ads Connector]:https://github.com/GoogleCloudPlatform/cloud-for-marketing/tree/master/marketing-analytics/activation/gmp-googleads-connector
|
|
71
|
+
|
|
66
72
|
[Data Tasks Coordinator]:https://github.com/GoogleCloudPlatform/cloud-for-marketing/tree/master/marketing-analytics/activation/data-tasks-coordinator
|
|
73
|
+
|
|
67
74
|
[1]:https://cloud.google.com/functions/docs/writing/background#functions-writing-background-hello-pubsub-node8-10
|
|
75
|
+
|
|
68
76
|
[2]:https://cloud.google.com/datastore/docs/concepts/overview#comparison_with_traditional_databases
|
|
77
|
+
|
|
69
78
|
[Bash]:https://www.gnu.org/software/bash/
|
package/bin/install_functions.sh
CHANGED
|
@@ -443,7 +443,7 @@ select_functions_location() {
|
|
|
443
443
|
"name~${PROJECT_NAMESPACE}" --format="csv[no-heading](name,REGION)"))
|
|
444
444
|
if [[ ${#exist_functions[@]} -gt 0 ]]; then
|
|
445
445
|
local exist_region
|
|
446
|
-
exist_region=$(printf "${exist_functions[0]}" | cut -d
|
|
446
|
+
exist_region=$(printf "${exist_functions[0]}" | cut -d\, -f2 | uniq)
|
|
447
447
|
printf '%s\n' "Current application has already been installed in region: \
|
|
448
448
|
${exist_region}."
|
|
449
449
|
local i
|
|
@@ -466,9 +466,9 @@ Functions for that region. Do you want to continue? [N/y]: "
|
|
|
466
466
|
if [[ ${confirm_delete} == "Y" || ${confirm_delete} == "y" ]]; then
|
|
467
467
|
for i in "${!exist_functions[@]}"; do
|
|
468
468
|
local exist_function
|
|
469
|
-
exist_function=$(printf "${exist_functions[$i]}" | cut -d
|
|
469
|
+
exist_function=$(printf "${exist_functions[$i]}" | cut -d\, -f1)
|
|
470
470
|
local function_region
|
|
471
|
-
function_region=$(printf "${exist_functions[$i]}" | cut -d
|
|
471
|
+
function_region=$(printf "${exist_functions[$i]}" | cut -d\, -f2)
|
|
472
472
|
gcloud functions delete --region="${function_region}" \
|
|
473
473
|
"${exist_function}"
|
|
474
474
|
done
|
|
@@ -743,8 +743,8 @@ select_dataset_location() {
|
|
|
743
743
|
"ASIA_PACIFIC"
|
|
744
744
|
)
|
|
745
745
|
local MULTI_REGIONAL=(
|
|
746
|
-
"Data centers within member states of the European Union (
|
|
747
|
-
"Data centers in the United States (
|
|
746
|
+
"Data centers within member states of the European Union (eu)"
|
|
747
|
+
"Data centers in the United States (us)"
|
|
748
748
|
)
|
|
749
749
|
local AMERICAS=(
|
|
750
750
|
"${NORTH_AMERICA[@]}"
|
|
@@ -1199,21 +1199,17 @@ create_or_update_sink() {
|
|
|
1199
1199
|
local existingFilter
|
|
1200
1200
|
existingFilter=$(gcloud logging sinks list --filter="name:${sinkName}" \
|
|
1201
1201
|
--format="value(filter)")
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
printf '%s\n' " Logging Export [${sinkName}] doesn't exist. Creating..."
|
|
1207
|
-
else
|
|
1208
|
-
action="update"
|
|
1209
|
-
printf '%s\n' " Logging Export [${sinkName}] exists with a different \
|
|
1210
|
-
filter. Updating..."
|
|
1211
|
-
fi
|
|
1212
|
-
gcloud -q logging sinks ${action} "${sinkName}" "${sinkDestAndFlags[@]}" \
|
|
1213
|
-
--log-filter="${logFilter}"
|
|
1202
|
+
local action
|
|
1203
|
+
if [[ -z "${existingFilter}" ]]; then
|
|
1204
|
+
action="create"
|
|
1205
|
+
printf '%s\n' " Logging Export [${sinkName}] doesn't exist. Creating..."
|
|
1214
1206
|
else
|
|
1215
|
-
|
|
1207
|
+
action="update"
|
|
1208
|
+
printf '%s\n' " Logging Export [${sinkName}] exists with a different \
|
|
1209
|
+
filter. Updating..."
|
|
1216
1210
|
fi
|
|
1211
|
+
gcloud -q logging sinks ${action} "${sinkName}" "${sinkDestAndFlags[@]}" \
|
|
1212
|
+
--log-filter="${logFilter}"
|
|
1217
1213
|
if [[ $? -gt 0 ]];then
|
|
1218
1214
|
printf '%s\n' "Failed to create or update Logs router sink."
|
|
1219
1215
|
return 1
|
|
@@ -1822,32 +1818,66 @@ get_cloud_functions_service_account() {
|
|
|
1822
1818
|
}
|
|
1823
1819
|
|
|
1824
1820
|
#######################################
|
|
1825
|
-
# Make sure Firestore
|
|
1826
|
-
#
|
|
1827
|
-
#
|
|
1828
|
-
# datastore.locations.list - sample role: Cloud Datastore Owner
|
|
1829
|
-
# servicemanagement.services.bind - sample role: Editor
|
|
1821
|
+
# Make sure the Firestore database is in the current project. If there is no
|
|
1822
|
+
# Firestore datastore, it will help to create one.
|
|
1823
|
+
# To create the Firestore, the operator need to be the Owner.
|
|
1830
1824
|
# Globals:
|
|
1831
1825
|
# GCP_PROJECT
|
|
1832
1826
|
# Arguments:
|
|
1833
|
-
#
|
|
1827
|
+
# Firestore mode, 'native' or 'datastore'.
|
|
1828
|
+
# Firestore region, it's not the same list as Cloud Functions regions and it
|
|
1829
|
+
# will be bonded to this Cloud project after created.
|
|
1834
1830
|
#######################################
|
|
1835
1831
|
check_firestore_existence() {
|
|
1836
|
-
local
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1832
|
+
local firestore mode appRegion
|
|
1833
|
+
mode="${1}"
|
|
1834
|
+
appRegion="${2}"
|
|
1835
|
+
firestore=$(gcloud app describe --format="csv[no-heading](databaseType)")
|
|
1836
|
+
if [[ -z "${firestore}" ]]; then
|
|
1837
|
+
printf '%s\n' "Firestore is not ready. Creating a new Firestore database\
|
|
1838
|
+
is an irreversible operation, so read carefully before continue:"
|
|
1839
|
+
printf '%s\n' " 1. You need to be the owner of ${GCP_PROJECT} to continue."
|
|
1840
|
+
printf '%s\n' " 2. Once you select the region and mode, you cannot change it."
|
|
1841
|
+
printf '%s\n' "Press any key to continue..."
|
|
1846
1842
|
local any
|
|
1847
1843
|
read -n1 -s any
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1844
|
+
if [[ -z "${mode}" ]]; then
|
|
1845
|
+
printf '%s\n' " For more information about mode, see \
|
|
1846
|
+
https://cloud.google.com/firestore/docs/firestore-or-datastore#choosing_a_database_mode"
|
|
1847
|
+
fi
|
|
1848
|
+
while [[ -z "${mode}" ]]; do
|
|
1849
|
+
printf '%s' " Enter the mode of your dataset [Native]: "
|
|
1850
|
+
local selectMode
|
|
1851
|
+
read -r selectMode
|
|
1852
|
+
selectMode=$(printf '%s' "${selectMode:-"Native"}" | \
|
|
1853
|
+
tr '[:upper:]' '[:lower:]')
|
|
1854
|
+
if [[ ${selectMode} == 'native' || ${selectMode} == 'datastore' ]]; then
|
|
1855
|
+
mode=${selectMode}
|
|
1856
|
+
fi
|
|
1857
|
+
done
|
|
1858
|
+
printf '%s\n' "Creating Firestore database in ${mode} mode..."
|
|
1859
|
+
gcloud app create --region=${appRegion}
|
|
1860
|
+
if [[ $? -eq 0 ]]; then
|
|
1861
|
+
if [[ "${mode}" == "native" ]]; then
|
|
1862
|
+
appRegion=$(gcloud app describe --format="csv[no-heading](locationId)")
|
|
1863
|
+
gcloud firestore databases create --region="${appRegion}"
|
|
1864
|
+
fi
|
|
1865
|
+
else
|
|
1866
|
+
return 1
|
|
1867
|
+
fi
|
|
1868
|
+
else
|
|
1869
|
+
printf '%s\n' "OK. Firestore is ready in mode ${firestore}."
|
|
1870
|
+
fi
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
#######################################
|
|
1874
|
+
# Installation step for confirming Firestore is ready.
|
|
1875
|
+
# See function check_firestore_existence.
|
|
1876
|
+
#######################################
|
|
1877
|
+
confirm_firestore() {
|
|
1878
|
+
(( STEP += 1 ))
|
|
1879
|
+
printf '%s\n' "Step ${STEP}: Checking the status of Firestore..."
|
|
1880
|
+
check_firestore_existence
|
|
1851
1881
|
}
|
|
1852
1882
|
|
|
1853
1883
|
#######################################
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google-cloud/nodejs-common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A NodeJs common library for solutions based on Cloud Functions",
|
|
5
5
|
"author": "Google Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -16,20 +16,20 @@
|
|
|
16
16
|
},
|
|
17
17
|
"homepage": "https://github.com/GoogleCloudPlatform/cloud-for-marketing/blob/master/marketing-analytics/activation/common-libs/nodejs-common/README.md",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@google-cloud/aiplatform": "^1.
|
|
20
|
-
"@google-cloud/automl": "^2.
|
|
21
|
-
"@google-cloud/bigquery": "^5.
|
|
22
|
-
"@google-cloud/datastore": "^6.
|
|
23
|
-
"@google-cloud/firestore": "^4.
|
|
24
|
-
"@google-cloud/logging-winston": "^4.
|
|
25
|
-
"@google-cloud/pubsub": "^2.
|
|
26
|
-
"@google-cloud/storage": "^5.
|
|
27
|
-
"gaxios": "^4.3.
|
|
28
|
-
"google-ads-api": "^
|
|
29
|
-
"google-ads-node": "^
|
|
30
|
-
"google-auth-library": "^7.
|
|
31
|
-
"googleapis": "^
|
|
32
|
-
"soap": "^0.
|
|
19
|
+
"@google-cloud/aiplatform": "^1.13.0",
|
|
20
|
+
"@google-cloud/automl": "^2.5.1",
|
|
21
|
+
"@google-cloud/bigquery": "^5.9.2",
|
|
22
|
+
"@google-cloud/datastore": "^6.6.2",
|
|
23
|
+
"@google-cloud/firestore": "^4.15.1",
|
|
24
|
+
"@google-cloud/logging-winston": "^4.1.1",
|
|
25
|
+
"@google-cloud/pubsub": "^2.18.2",
|
|
26
|
+
"@google-cloud/storage": "^5.16.0",
|
|
27
|
+
"gaxios": "^4.3.2",
|
|
28
|
+
"google-ads-api": "^9.0.0",
|
|
29
|
+
"google-ads-node": "^7.0.0",
|
|
30
|
+
"google-auth-library": "^7.10.2",
|
|
31
|
+
"googleapis": "^91.0.0",
|
|
32
|
+
"soap": "^0.43.0",
|
|
33
33
|
"winston": "^3.3.3",
|
|
34
34
|
"lodash": "^4.17.21"
|
|
35
35
|
},
|
package/src/apis/google_ads.js
CHANGED
|
@@ -41,25 +41,77 @@ const {
|
|
|
41
41
|
},
|
|
42
42
|
} = googleAdsLib;
|
|
43
43
|
const {GoogleAdsApi} = require('google-ads-api');
|
|
44
|
+
const lodash = require('lodash');
|
|
45
|
+
|
|
44
46
|
const AuthClient = require('./auth_client.js');
|
|
45
47
|
const {getLogger, BatchResult,} = require('../components/utils.js');
|
|
46
48
|
|
|
47
49
|
/** @type {!ReadonlyArray<string>} */
|
|
48
50
|
const API_SCOPES = Object.freeze(['https://www.googleapis.com/auth/adwords',]);
|
|
49
51
|
|
|
52
|
+
/**
|
|
53
|
+
* List of properties that will be taken from the data file as elements of a
|
|
54
|
+
* conversion.
|
|
55
|
+
* @see https://developers.google.com/google-ads/api/reference/rpc/latest/ClickConversion
|
|
56
|
+
* @type {Array<string>}
|
|
57
|
+
*/
|
|
58
|
+
const PICKED_PROPERTIES = [
|
|
59
|
+
'external_attribution_data',
|
|
60
|
+
'cart_data',
|
|
61
|
+
'user_identifiers',
|
|
62
|
+
'gclid',
|
|
63
|
+
'conversion_action',
|
|
64
|
+
'conversion_date_time',
|
|
65
|
+
'conversion_value',
|
|
66
|
+
'currency_code',
|
|
67
|
+
'order_id',
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Kinds of UserIdentifier.
|
|
72
|
+
* @see https://developers.google.com/google-ads/api/reference/rpc/latest/UserIdentifier
|
|
73
|
+
* @type {Array<string>}
|
|
74
|
+
*/
|
|
75
|
+
const IDENTIFIERS = [
|
|
76
|
+
'hashed_email',
|
|
77
|
+
'hashed_phone_number',
|
|
78
|
+
'mobile_id',
|
|
79
|
+
'third_party_user_id',
|
|
80
|
+
'address_info',
|
|
81
|
+
];
|
|
82
|
+
|
|
50
83
|
/**
|
|
51
84
|
* Configuration for uploading click conversions for Google Ads, includes:
|
|
52
85
|
* gclid, conversion_action, conversion_date_time, conversion_value,
|
|
53
|
-
* currency_code, order_id, external_attribution_data
|
|
86
|
+
* currency_code, order_id, external_attribution_data, etc.
|
|
87
|
+
* @see PICKED_PROPERTIES
|
|
88
|
+
*
|
|
89
|
+
* Other properties that will be used to build the conversions but not picked by
|
|
90
|
+
* the value directly including:
|
|
91
|
+
* 1. 'user_identifier_source', source of the user identifier. If there is user
|
|
92
|
+
* identifiers information in the conversion, this property should be set as
|
|
93
|
+
* 'FIRST_PARTY'.
|
|
94
|
+
* @see IDENTIFIERS
|
|
95
|
+
* @see https://developers.google.com/google-ads/api/reference/rpc/latest/UserIdentifier?hl=en
|
|
96
|
+
* 2. 'custom_variable_tags', the tags of conversion custom variables. To upload
|
|
97
|
+
* custom variables, 'conversion_custom_variable_id' is required rather than the
|
|
98
|
+
* 'tag'. So the invoker is expected to use the function
|
|
99
|
+
* 'getConversionCustomVariableId' to get the ids and pass in as a
|
|
100
|
+
* map(customVariables) of <tag, id> pairs before uploading conversions.
|
|
101
|
+
*
|
|
54
102
|
* @see https://developers.google.com/google-ads/api/reference/rpc/latest/ClickConversion
|
|
55
103
|
* @typedef {{
|
|
104
|
+
* external_attribution_data: (GoogleAdsApi.ExternalAttributionData|undefined),
|
|
105
|
+
* cart_data: (object|undefined),
|
|
56
106
|
* gclid: string,
|
|
57
107
|
* conversion_action: string,
|
|
58
108
|
* conversion_date_time: string,
|
|
59
109
|
* conversion_value: number,
|
|
60
110
|
* currency_code:(string|undefined),
|
|
61
111
|
* order_id: (string|undefined),
|
|
62
|
-
*
|
|
112
|
+
* user_identifier_source:(UserIdentifierSource|undefined),
|
|
113
|
+
* custom_variable_tags:(!Array<string>|undefined),
|
|
114
|
+
* customVariables:(!object<string,string>|undefined),
|
|
63
115
|
* }}
|
|
64
116
|
*/
|
|
65
117
|
let ClickConversionConfig;
|
|
@@ -219,10 +271,8 @@ class GoogleAds {
|
|
|
219
271
|
*/
|
|
220
272
|
return async (lines, batchId) => {
|
|
221
273
|
/** @type {!Array<ClickConversionConfig>} */
|
|
222
|
-
const conversions = lines.map(
|
|
223
|
-
|
|
224
|
-
return Object.assign({}, adsConfig, record);
|
|
225
|
-
});
|
|
274
|
+
const conversions = lines.map(
|
|
275
|
+
(line) => buildClickConversionFromLine(line, adsConfig, customerId));
|
|
226
276
|
/** @const {BatchResult} */
|
|
227
277
|
const batchResult = {
|
|
228
278
|
result: true,
|
|
@@ -346,7 +396,6 @@ class GoogleAds {
|
|
|
346
396
|
* failure.
|
|
347
397
|
* groupedFailed - a hashmap of failed the lines. The key is the reason, the
|
|
348
398
|
* value is the array of failed lines due to this reason.
|
|
349
|
-
* TODO: Confirm how to surface and handle groupedFailed.
|
|
350
399
|
* @param {!BatchResult} batchResult
|
|
351
400
|
* @param {!Array<!GoogleAdsFailure>} failures
|
|
352
401
|
* @param {!Array<string>} lines The original input data.
|
|
@@ -404,6 +453,26 @@ class GoogleAds {
|
|
|
404
453
|
return customer.conversionUploads.uploadClickConversions(request);
|
|
405
454
|
}
|
|
406
455
|
|
|
456
|
+
/**
|
|
457
|
+
* Returns the id of Conversion Custom Variable with the given tag.
|
|
458
|
+
* @param {string} tag Custom Variable tag.
|
|
459
|
+
* @param {string} customerId
|
|
460
|
+
* @param {string} loginCustomerId Login customer account ID (Mcc Account id).
|
|
461
|
+
* @return {Promise<number|undefined>} Returns undefined if can't find tag.
|
|
462
|
+
*/
|
|
463
|
+
async getConversionCustomVariableId(tag, customerId, loginCustomerId) {
|
|
464
|
+
const customer = this.getGoogleAdsApiCustomer_(loginCustomerId, customerId);
|
|
465
|
+
const customVariables = await customer.query(`
|
|
466
|
+
SELECT conversion_custom_variable.id,
|
|
467
|
+
conversion_custom_variable.tag
|
|
468
|
+
FROM conversion_custom_variable
|
|
469
|
+
WHERE conversion_custom_variable.tag = "${tag}" LIMIT 1
|
|
470
|
+
`);
|
|
471
|
+
if (customVariables.length > 0) {
|
|
472
|
+
return customVariables[0].conversion_custom_variable.id;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
407
476
|
/**
|
|
408
477
|
* Returns the function to send out a request to Google Ads API with
|
|
409
478
|
* user ids for Customer Match upload
|
|
@@ -535,6 +604,43 @@ class GoogleAds {
|
|
|
535
604
|
|
|
536
605
|
}
|
|
537
606
|
|
|
607
|
+
/**
|
|
608
|
+
* Returns a conversion object based the given config and line data.
|
|
609
|
+
* @param {string} line A JSON string of a conversion data.
|
|
610
|
+
* @param {ClickConversionConfig} config Default click conversion params
|
|
611
|
+
* @param {string} customerId
|
|
612
|
+
* @return {object} A conversion
|
|
613
|
+
*/
|
|
614
|
+
const buildClickConversionFromLine = (line, config, customerId) => {
|
|
615
|
+
const {customVariables, user_identifier_source} = config;
|
|
616
|
+
const record = JSON.parse(line);
|
|
617
|
+
const conversion = lodash.merge(lodash.pick(config, PICKED_PROPERTIES),
|
|
618
|
+
lodash.pick(record, PICKED_PROPERTIES));
|
|
619
|
+
if (customVariables) {
|
|
620
|
+
const tags = Object.keys(customVariables);
|
|
621
|
+
conversion.custom_variables = tags.map((tag) => {
|
|
622
|
+
return {
|
|
623
|
+
conversion_custom_variable:
|
|
624
|
+
`customers/${customerId}/conversionCustomVariables/${customVariables[tag]}`,
|
|
625
|
+
value: record[tag],
|
|
626
|
+
};
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
const user_identifiers = [];
|
|
630
|
+
IDENTIFIERS.forEach((identifier) => {
|
|
631
|
+
if (record[identifier]) {
|
|
632
|
+
user_identifiers.push({
|
|
633
|
+
user_identifier_source,
|
|
634
|
+
[identifier]: record[identifier],
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
if (user_identifiers.length > 0) {
|
|
639
|
+
conversion.user_identifiers = user_identifiers;
|
|
640
|
+
}
|
|
641
|
+
return conversion;
|
|
642
|
+
}
|
|
643
|
+
|
|
538
644
|
module.exports = {
|
|
539
645
|
ClickConversionConfig,
|
|
540
646
|
CustomerMatchRecord,
|
|
@@ -542,4 +648,5 @@ module.exports = {
|
|
|
542
648
|
GoogleAds,
|
|
543
649
|
ReportQueryConfig,
|
|
544
650
|
GoogleAdsField,
|
|
651
|
+
buildClickConversionFromLine,
|
|
545
652
|
};
|
package/src/apis/youtube.js
CHANGED
|
@@ -24,6 +24,7 @@ const {
|
|
|
24
24
|
Schema$Video,
|
|
25
25
|
Schema$CommentThread,
|
|
26
26
|
Schema$Playlist,
|
|
27
|
+
Schema$Search,
|
|
27
28
|
} = google.youtube;
|
|
28
29
|
const AuthClient = require('./auth_client.js');
|
|
29
30
|
const {getLogger} = require('../components/utils.js');
|
|
@@ -105,6 +106,45 @@ let ListCommentThreadsConfig;
|
|
|
105
106
|
*/
|
|
106
107
|
let ListPlaylistConfig;
|
|
107
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Configuration for listing youtube search results.
|
|
111
|
+
* @see https://developers.google.com/youtube/v3/docs/search/list
|
|
112
|
+
* @typedef {{
|
|
113
|
+
* part: Array<string>,
|
|
114
|
+
* forContentOwner: (boolean|undefined),
|
|
115
|
+
* forDeveloper: (boolean|undefined),
|
|
116
|
+
* forMine: (boolean|undefined),
|
|
117
|
+
* relatedToVideoId: (string|undefined),
|
|
118
|
+
* channelId: (string|undefined),
|
|
119
|
+
* channelType: (string|undefined),
|
|
120
|
+
* eventType: (string|undefined),
|
|
121
|
+
* location: (string|undefined),
|
|
122
|
+
* locationRadius: (string|undefined),
|
|
123
|
+
* maxResults: (unsigned integer|undefined),
|
|
124
|
+
* onBehalfOfContentOwner: (string|undefined),
|
|
125
|
+
* order: (string|undefined),
|
|
126
|
+
* pageToken: (string|undefined),
|
|
127
|
+
* publishedAfter: (datetime|undefined),
|
|
128
|
+
* publishedBefore: (datetime|undefined),
|
|
129
|
+
* q: (string|undefined),
|
|
130
|
+
* regionCode: (string|undefined),
|
|
131
|
+
* relevanceLanguage: (string|undefined),
|
|
132
|
+
* safeSearch: (string|undefined),
|
|
133
|
+
* topicId: (string|undefined),
|
|
134
|
+
* type: (string|undefined),
|
|
135
|
+
* videoCaption: (string|undefined),
|
|
136
|
+
* videoCategoryId: (string|undefined),
|
|
137
|
+
* videoDefinition: (string|undefined),
|
|
138
|
+
* videoDimension: (string|undefined),
|
|
139
|
+
* videoDuration: (string|undefined),
|
|
140
|
+
* videoEmbeddable: (string|undefined),
|
|
141
|
+
* videoLicense: (string|undefined),
|
|
142
|
+
* videoSyndicated: (string|undefined),
|
|
143
|
+
* videoType: (string|undefined)
|
|
144
|
+
* }}
|
|
145
|
+
*/
|
|
146
|
+
let ListSearchConfig;
|
|
147
|
+
|
|
108
148
|
/**
|
|
109
149
|
* Youtube API v3 stub.
|
|
110
150
|
* See: https://developers.google.com/youtube/v3/docs
|
|
@@ -116,6 +156,8 @@ let ListPlaylistConfig;
|
|
|
116
156
|
* https://developers.google.com/youtube/v3/docs/commentThreads/list
|
|
117
157
|
* Playlist list type definition, see:
|
|
118
158
|
* https://developers.google.com/youtube/v3/docs/playlists/list
|
|
159
|
+
* Search list type definition, see:
|
|
160
|
+
* https://developers.google.com/youtube/v3/docs/search/list
|
|
119
161
|
*/
|
|
120
162
|
class YouTube {
|
|
121
163
|
/**
|
|
@@ -220,7 +262,7 @@ class YouTube {
|
|
|
220
262
|
* @return {!Promise<Array<Schema$Playlist>>}
|
|
221
263
|
*/
|
|
222
264
|
async listPlaylists(config, resultLimit = 1000, pageToken = null) {
|
|
223
|
-
if (resultLimit <= 0)
|
|
265
|
+
if (resultLimit <= 0) return [];
|
|
224
266
|
|
|
225
267
|
const playlistsRequest = Object.assign({
|
|
226
268
|
auth: this.auth,
|
|
@@ -255,6 +297,51 @@ class YouTube {
|
|
|
255
297
|
throw new Error(errorMsg);
|
|
256
298
|
}
|
|
257
299
|
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Returns a collection of search results that match the query parameters
|
|
303
|
+
* specified in the API request.
|
|
304
|
+
* @see https://developers.google.com/youtube/v3/docs/search/list
|
|
305
|
+
* @param {!ListSearchConfig} config List search result configuration.
|
|
306
|
+
* @param {number} resultLimit The limit of the number of results.
|
|
307
|
+
* @param {string} pageToken Token to identify a specific page in the result.
|
|
308
|
+
* @return {!Promise<Array<Schema$Search>>}
|
|
309
|
+
*/
|
|
310
|
+
async listSearchResults(config, resultLimit = 1000, pageToken = null) {
|
|
311
|
+
if (resultLimit <= 0) return [];
|
|
312
|
+
|
|
313
|
+
const searchRequest = Object.assign({
|
|
314
|
+
auth: this.auth,
|
|
315
|
+
}, config, {
|
|
316
|
+
pageToken
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
if (Array.isArray(searchRequest.part)) {
|
|
320
|
+
searchRequest.part = searchRequest.part.join(',');
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
const response = await this.instance.search.list(searchRequest);
|
|
325
|
+
this.logger.debug('Response: ', response.data);
|
|
326
|
+
if (response.data.nextPageToken) {
|
|
327
|
+
this.logger.debug(
|
|
328
|
+
'Call youtube search:list API agian with Token: ',
|
|
329
|
+
response.data.nextPageToken);
|
|
330
|
+
const nextResponse = await this.listSearchResults(
|
|
331
|
+
config,
|
|
332
|
+
resultLimit - response.data.items.length,
|
|
333
|
+
response.data.nextPageToken);
|
|
334
|
+
return response.data.items.concat(nextResponse);
|
|
335
|
+
}
|
|
336
|
+
return response.data.items;
|
|
337
|
+
} catch (error) {
|
|
338
|
+
const errorMsg =
|
|
339
|
+
`Fail to list search results: ${JSON.stringify(searchRequest)}`;
|
|
340
|
+
this.logger.error('YouTube list search results failed.', error.message);
|
|
341
|
+
this.logger.debug('Errors in response:', error);
|
|
342
|
+
throw new Error(errorMsg);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
258
345
|
}
|
|
259
346
|
|
|
260
347
|
module.exports = {
|
|
@@ -263,6 +350,7 @@ module.exports = {
|
|
|
263
350
|
ListVideosConfig,
|
|
264
351
|
ListCommentThreadsConfig,
|
|
265
352
|
ListPlaylistConfig,
|
|
353
|
+
ListSearchConfig,
|
|
266
354
|
API_VERSION,
|
|
267
355
|
API_SCOPES,
|
|
268
356
|
};
|