@google-cloud/nodejs-common 2.4.2 → 2.4.4
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 +2 -1
- package/bin/install_functions.sh +1 -1
- package/package.json +15 -16
- package/src/apis/base/ads_api_common.js +3 -2
- package/src/apis/index.js +0 -12
- package/src/apis/cloud_platform_apis.js +0 -96
- package/src/apis/measurement_protocol.js +0 -156
package/README.md
CHANGED
|
@@ -12,7 +12,8 @@ and [Data Tasks Coordinator]. This library includes:
|
|
|
12
12
|
for [GMP and Google Ads Connector]:
|
|
13
13
|
|
|
14
14
|
- Google Analytics data import
|
|
15
|
-
- Google Analytics measurement protocol
|
|
15
|
+
- ~~Google Analytics measurement protocol~~ (This has
|
|
16
|
+
been deprecated since v2.4.4.)
|
|
16
17
|
- Campaign Manager offline conversion upload
|
|
17
18
|
- Search Ads 360 conversions upload
|
|
18
19
|
- Google Ads click conversions upload
|
package/bin/install_functions.sh
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google-cloud/nodejs-common",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.4",
|
|
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,26 +16,25 @@
|
|
|
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": "
|
|
20
|
-
"@google-cloud/automl": "
|
|
21
|
-
"@google-cloud/bigquery": "^7.9.
|
|
19
|
+
"@google-cloud/aiplatform": "3.35.0",
|
|
20
|
+
"@google-cloud/automl": "4.0.1",
|
|
21
|
+
"@google-cloud/bigquery": "^7.9.4",
|
|
22
22
|
"@google-cloud/datastore": "^9.2.1",
|
|
23
|
-
"@google-cloud/firestore": "^7.
|
|
23
|
+
"@google-cloud/firestore": "^7.11.0",
|
|
24
24
|
"@google-cloud/logging-winston": "^6.0.0",
|
|
25
|
-
"@google-cloud/pubsub": "
|
|
26
|
-
"@google-cloud/
|
|
27
|
-
"@google-cloud/scheduler": "
|
|
28
|
-
"@google-cloud/
|
|
25
|
+
"@google-cloud/pubsub": "4.11.0",
|
|
26
|
+
"@google-cloud/secret-manager": "5.6.0",
|
|
27
|
+
"@google-cloud/scheduler": "4.3.0",
|
|
28
|
+
"@google-cloud/storage": "^7.16.0",
|
|
29
|
+
"@grpc/grpc-js": "^1.13.3",
|
|
29
30
|
"gaxios": "^6.7.1",
|
|
30
|
-
"google-ads-nodejs-client": "
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"winston": "^3.17.0"
|
|
34
|
-
"@grpc/grpc-js": "^1.12.2",
|
|
35
|
-
"lodash": "^4.17.21"
|
|
31
|
+
"google-ads-nodejs-client": "18.0.1",
|
|
32
|
+
"googleapis": "^148.0.0",
|
|
33
|
+
"lodash": "^4.17.21",
|
|
34
|
+
"winston": "^3.17.0"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"jasmine": "^5.
|
|
37
|
+
"jasmine": "^5.7.0"
|
|
39
38
|
},
|
|
40
39
|
"scripts": {
|
|
41
40
|
"test": "node node_modules/jasmine/bin/jasmine"
|
|
@@ -81,17 +81,18 @@ class RestSearchStreamTransform extends Transform {
|
|
|
81
81
|
this.processFn = processFn;
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
-
let rows;
|
|
84
|
+
let rows, data;
|
|
85
85
|
if (rawString.indexOf(START_TAG) === -1) { // no 'results'
|
|
86
86
|
rows = [];
|
|
87
|
+
data = '';
|
|
87
88
|
} else {
|
|
88
89
|
const startIndex = rawString.indexOf(START_TAG) + START_TAG.length;
|
|
89
90
|
const resultsWithTailing = rawString.substring(startIndex, maskIndex);
|
|
90
91
|
const results = resultsWithTailing.substring(
|
|
91
92
|
0, resultsWithTailing.lastIndexOf(','));
|
|
92
93
|
rows = JSON.parse(results);
|
|
94
|
+
data = rows.map(this.processFn).join('\n') + '\n';
|
|
93
95
|
}
|
|
94
|
-
const data = rows.map(this.processFn).join('\n') + '\n';
|
|
95
96
|
// Clear cached chunks.
|
|
96
97
|
this.chunks = [latest.subarray(latest.indexOf(END_TAG) + END_TAG.length)];
|
|
97
98
|
|
package/src/apis/index.js
CHANGED
|
@@ -47,18 +47,6 @@ exports.displayvideo = require('./display_video.js');
|
|
|
47
47
|
*/
|
|
48
48
|
exports.analytics = require('./analytics.js');
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
* APIs integration class for Google Analytics Measurement Protocol.
|
|
52
|
-
* @const {{MeasurementProtocol:!MeasurementProtocol}}
|
|
53
|
-
*/
|
|
54
|
-
exports.measurementprotocol = require('./measurement_protocol.js');
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Cloud Resource Manager for checking the permissions.
|
|
58
|
-
* @const {{CloudPlatformApis:!CloudPlatformApis}}
|
|
59
|
-
*/
|
|
60
|
-
exports.cloudplatform = require('./cloud_platform_apis.js');
|
|
61
|
-
|
|
62
50
|
/**
|
|
63
51
|
* APIs integration class for Google Spreadsheets.
|
|
64
52
|
* @const {{
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
// Copyright 2019 Google Inc.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @fileoverview API handler for Google Cloud Platform APIs.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
'use strict';
|
|
20
|
-
|
|
21
|
-
const {google} = require('googleapis');
|
|
22
|
-
const AuthClient = require('./auth_client.js');
|
|
23
|
-
|
|
24
|
-
const API_SCOPES = Object.freeze([
|
|
25
|
-
'https://www.googleapis.com/auth/cloud-platform',
|
|
26
|
-
]);
|
|
27
|
-
const API_VERSION = 'v1';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Google Cloud Platform API stubs. This class offers convenient request
|
|
31
|
-
* wrappers for some of Google Cloud APIs that are not supported by
|
|
32
|
-
* Google cloud client libraries.
|
|
33
|
-
*/
|
|
34
|
-
class CloudPlatformApis {
|
|
35
|
-
constructor(env = process.env) {
|
|
36
|
-
/** @const {!AuthClient} */
|
|
37
|
-
this.authClient = new AuthClient(API_SCOPES, env);
|
|
38
|
-
this.projectId = env['GCP_PROJECT'];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Gets the auth object.
|
|
43
|
-
* @return {!Promise<{!OAuth2Client|!JWT|!Compute}>}
|
|
44
|
-
*/
|
|
45
|
-
async getAuth_() {
|
|
46
|
-
if (this.auth) return this.auth;
|
|
47
|
-
await this.authClient.prepareCredentials();
|
|
48
|
-
this.auth = this.authClient.getDefaultAuth();
|
|
49
|
-
return this.auth;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Gets the GCP project Id. In Cloud Functions, it *should* be passed in
|
|
54
|
-
* through environment variable during the deployment. But if it doesn't exist
|
|
55
|
-
* (for example, in local unit tests), this function will fallback to ADC
|
|
56
|
-
* (Application Default Credential) auth's asynchronous function to get the
|
|
57
|
-
* project Id.
|
|
58
|
-
* @return {string}
|
|
59
|
-
* @private
|
|
60
|
-
*/
|
|
61
|
-
async getProjectId_() {
|
|
62
|
-
if (!this.projectId) this.projectId = await this.auth.getProjectId();
|
|
63
|
-
return this.projectId;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Returns the available permissions for the given project and permissions
|
|
68
|
-
* list. See:
|
|
69
|
-
* https://cloud.google.com/resource-manager/reference/rest/v1/projects/testIamPermissions.
|
|
70
|
-
* For more information of Permission, see:
|
|
71
|
-
* https://cloud.google.com/iam/docs/overview#permissions.
|
|
72
|
-
* @param {!Array<string>} permissions Permissions array.
|
|
73
|
-
* @return {!Promise<!Array<string>>} The available permissions that current
|
|
74
|
-
* operator (defined by ADC authentication information) has for the given
|
|
75
|
-
* permission list.
|
|
76
|
-
*/
|
|
77
|
-
async testIamPermissions(permissions) {
|
|
78
|
-
const resourceManager = google.cloudresourcemanager({
|
|
79
|
-
version: API_VERSION,
|
|
80
|
-
auth: await this.getAuth_(),
|
|
81
|
-
});
|
|
82
|
-
const projectId = await this.getProjectId_();
|
|
83
|
-
const request = {
|
|
84
|
-
resource_: projectId,
|
|
85
|
-
resource: {permissions: permissions},
|
|
86
|
-
};
|
|
87
|
-
return resourceManager.projects.testIamPermissions(request)
|
|
88
|
-
.then((response) => response.data.permissions);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = {
|
|
93
|
-
CloudPlatformApis,
|
|
94
|
-
API_VERSION,
|
|
95
|
-
API_SCOPES,
|
|
96
|
-
};
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// Copyright 2019 Google Inc.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @fileoverview This is Google Analytics Measurement Protocol based on nodejs
|
|
17
|
-
* 'request' library.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
'use strict';
|
|
21
|
-
|
|
22
|
-
const {request} = require('gaxios');
|
|
23
|
-
const {
|
|
24
|
-
getLogger,
|
|
25
|
-
SendSingleBatch,
|
|
26
|
-
BatchResult,
|
|
27
|
-
} = require('../components/utils.js');
|
|
28
|
-
/** Base URL for Google Analytics service. */
|
|
29
|
-
const BASE_URL = 'https://www.google-analytics.com';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Measurement Protocol hits are HTTP request. Reference:
|
|
33
|
-
* 1. Measurement Protocol Parameter Reference
|
|
34
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
|
|
35
|
-
* 2. Google Analytics Collection Limits and Quotas
|
|
36
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/limits-quotas
|
|
37
|
-
* 3. Validating Hits - Measurement Protocol
|
|
38
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits
|
|
39
|
-
* 4. Batching multiple hits in a single request
|
|
40
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch
|
|
41
|
-
*/
|
|
42
|
-
class MeasurementProtocol {
|
|
43
|
-
/**
|
|
44
|
-
* Measurement Protocol has a debug endpoint which will return the results of
|
|
45
|
-
* each hits. By given this initial parameter with 'true' value, this class
|
|
46
|
-
* can send hits to the debug endpoint.
|
|
47
|
-
* @param {boolean=} debugMode
|
|
48
|
-
*/
|
|
49
|
-
constructor(debugMode = false) {
|
|
50
|
-
this.debugMode = debugMode;
|
|
51
|
-
this.logger = getLogger('API.MP');
|
|
52
|
-
this.logger.debug(`Init ${this.constructor.name} with Debug Mode.`);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Returns the function to send out a Measurement Protocol request with
|
|
57
|
-
* multiple hits.
|
|
58
|
-
* @param {!Object<string,string>} config Measurement Protocol common
|
|
59
|
-
* configuration, e.g. web property ID.
|
|
60
|
-
* @return {!SendSingleBatch} Function which can send a batch of hits to
|
|
61
|
-
* Measurement Protocol.
|
|
62
|
-
*/
|
|
63
|
-
getSinglePingFn(config) {
|
|
64
|
-
/**
|
|
65
|
-
* Sends a batch of hits to Measurement Protocol.
|
|
66
|
-
* @param {!Array<string>} lines Data for single request. It should be
|
|
67
|
-
* guaranteed that it doesn't exceed quota limitation.
|
|
68
|
-
* @param {string} batchId The tag for log.
|
|
69
|
-
* @return {!Promise<BatchResult>}
|
|
70
|
-
*/
|
|
71
|
-
return async (lines, batchId) => {
|
|
72
|
-
const payload = lines.map((line) => {
|
|
73
|
-
const record = JSON.parse(line);
|
|
74
|
-
const hit = Object.assign({}, config, record);
|
|
75
|
-
return Object.keys(hit).map(
|
|
76
|
-
(key) => `${key}=${encodeURIComponent(hit[key])}`)
|
|
77
|
-
.join('&');
|
|
78
|
-
})
|
|
79
|
-
.join('\n');
|
|
80
|
-
// In debug mode, the path is fixed to '/debug/collect'.
|
|
81
|
-
const path = (this.debugMode) ? '/debug/collect' : '/batch';
|
|
82
|
-
const requestOptions = {
|
|
83
|
-
method: 'POST',
|
|
84
|
-
responseType: 'json',
|
|
85
|
-
url: `${BASE_URL}${path}`,
|
|
86
|
-
body: payload,
|
|
87
|
-
headers: {'User-Agent': 'Tentacles/MeasurementProtocol-v1'}
|
|
88
|
-
};
|
|
89
|
-
const response = await request(requestOptions);
|
|
90
|
-
/** @type {BatchResult} */ const batchResult = {
|
|
91
|
-
numberOfLines: lines.length,
|
|
92
|
-
};
|
|
93
|
-
if (response.status < 200 || response.status >= 300) {
|
|
94
|
-
const errorMessages = [
|
|
95
|
-
`Measurement Protocol [${batchId}] didn't succeed.`,
|
|
96
|
-
`Get response code: ${response.status}`,
|
|
97
|
-
`response: ${response.data}`,
|
|
98
|
-
];
|
|
99
|
-
this.logger.error(errorMessages.join('\n'));
|
|
100
|
-
batchResult.errors = errorMessages;
|
|
101
|
-
batchResult.result = false;
|
|
102
|
-
return batchResult;
|
|
103
|
-
}
|
|
104
|
-
this.logger.debug(`Configuration:`, config);
|
|
105
|
-
this.logger.debug(`Input Data: `, lines);
|
|
106
|
-
this.logger.debug(`Batch[${batchId}] status: ${response.status}`);
|
|
107
|
-
this.logger.debug(response.data);
|
|
108
|
-
// There is not enough information from the non-debug mode.
|
|
109
|
-
if (!this.debugMode) {
|
|
110
|
-
batchResult.result = true;
|
|
111
|
-
} else {
|
|
112
|
-
this.extraFailedLines_(batchResult, response.data.hitParsingResult,
|
|
113
|
-
lines);
|
|
114
|
-
}
|
|
115
|
-
return batchResult;
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Extras failed lines based on the hitParsingResult, see:
|
|
121
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits
|
|
122
|
-
*
|
|
123
|
-
* Note, only in 'debug' mode, Google Analytics will return this part of data.
|
|
124
|
-
*
|
|
125
|
-
* @param {!BatchResult} batchResult
|
|
126
|
-
* @param {!Array<!Object>} hitParsingResults
|
|
127
|
-
* @param {!Array<string>} lines The original input data.
|
|
128
|
-
* @private
|
|
129
|
-
*/
|
|
130
|
-
extraFailedLines_(batchResult, hitParsingResults, lines) {
|
|
131
|
-
batchResult.failedLines = [];
|
|
132
|
-
batchResult.groupedFailed = {};
|
|
133
|
-
const errors = new Set();
|
|
134
|
-
hitParsingResults.forEach((result, index) => {
|
|
135
|
-
if (!result.valid) {
|
|
136
|
-
const failedLine = lines[index];
|
|
137
|
-
batchResult.failedLines.push(failedLine);
|
|
138
|
-
result.parserMessage.forEach(({description: error, messageType}) => {
|
|
139
|
-
this.logger.info(`[${messageType}]: ${error} for ${failedLine}`);
|
|
140
|
-
if (messageType === 'ERROR') {
|
|
141
|
-
errors.add(error);
|
|
142
|
-
const groupedFailed = batchResult.groupedFailed[error] || [];
|
|
143
|
-
groupedFailed.push(failedLine);
|
|
144
|
-
if (groupedFailed.length === 1) {
|
|
145
|
-
batchResult.groupedFailed[error] = groupedFailed;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
batchResult.result = batchResult.failedLines.length === 0;
|
|
152
|
-
batchResult.errors = Array.from(errors);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
module.exports = {MeasurementProtocol};
|