@google-cloud/nodejs-common 1.0.2 → 1.0.3-alpha

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.
@@ -1910,3 +1910,27 @@ join_string_array() {
1910
1910
  shift
1911
1911
  printf %s "$first" "${@/#/$separator}"
1912
1912
  }
1913
+
1914
+ #######################################
1915
+ # Creates or updates the BigQuery view.
1916
+ # Globals:
1917
+ # GCP_PROJECT
1918
+ # DATASET
1919
+ # Arguments:
1920
+ # The name of view.
1921
+ # The query of view.
1922
+ #######################################
1923
+ create_or_update_view() {
1924
+ local viewName viewQuery
1925
+ viewName="${1}"
1926
+ viewQuery="${2}"
1927
+ local action="mk"
1928
+ if [[ $(check_existence_in_bigquery "${DATASET}.${viewName}") -eq 0 ]]; then
1929
+ action="update"
1930
+ fi
1931
+ bq "${action}" \
1932
+ --use_legacy_sql=false \
1933
+ --view "${viewQuery}" \
1934
+ --project_id ${GCP_PROJECT} \
1935
+ "${DATASET}.${viewName}"
1936
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@google-cloud/nodejs-common",
3
- "version": "1.0.2",
3
+ "version": "1.0.3-alpha",
4
4
  "description": "A NodeJs common library for solutions based on Cloud Functions",
5
5
  "author": "Google Inc.",
6
6
  "license": "Apache-2.0",
@@ -80,6 +80,13 @@ const IDENTIFIERS = [
80
80
  'address_info',
81
81
  ];
82
82
 
83
+ /**
84
+ * Maximum number of user identifiers in single UserData.
85
+ * @see https://ads-developers.googleblog.com/2021/10/userdata-enforcement-in-google-ads-api.html
86
+ * @type {number}
87
+ */
88
+ const MAX_IDENTIFIERS_PER_USER = 20;
89
+
83
90
  /**
84
91
  * Configuration for uploading click conversions for Google Ads, includes:
85
92
  * gclid, conversion_action, conversion_date_time, conversion_value,
@@ -139,15 +146,11 @@ let CustomerMatchConfig;
139
146
  * hashed_email, hashed_phone_number, mobile_id, third_party_user_id or address_info
140
147
  * @see https://developers.google.com/google-ads/api/reference/rpc/latest/UserIdentifier
141
148
  * @typedef {{
142
- * hashed_email: string,
143
- * }|{
144
- * hashed_phone_number: string,
145
- * }|{
146
- * mobile_id: string,
147
- * }|{
148
- * third_party_user_id: string,
149
- * }|{
150
- * address_info: GoogleAdsApi.OfflineUserAddressInfo,
149
+ * hashed_email: (string|Array<string>|undefined),
150
+ * hashed_phone_number: (string|Array<string>|undefined),
151
+ * mobile_id: (string|Array<string>|undefined),
152
+ * third_party_user_id: (string|Array<string>|undefined),
153
+ * address_info: (GoogleAdsApi.OfflineUserAddressInfo|undefined),
151
154
  * }}
152
155
  */
153
156
  let CustomerMatchRecord;
@@ -530,18 +533,17 @@ class GoogleAds {
530
533
  const customerId = customerMatchConfig.customer_id.replace(/-/g, '');
531
534
  const loginCustomerId = customerMatchConfig.login_customer_id.replace(/-/g,
532
535
  '');
533
- const userListType = customerMatchConfig.list_type;
534
536
  const userListId = customerMatchConfig.list_id;
535
537
  const operation = customerMatchConfig.operation;
536
538
 
537
539
  const customer = this.getGoogleAdsApiCustomer_(loginCustomerId, customerId);
538
540
  const operationsList = this.buildOperationsList_(operation,
539
- customerMatchRecords, userListType);
541
+ customerMatchRecords);
540
542
  const metadata = this.buildCustomerMatchUserListMetadata_(customerId,
541
543
  userListId);
542
544
  const request = UploadUserDataRequest.create({
543
545
  customer_id: customerId,
544
- operations: [operationsList],
546
+ operations: operationsList,
545
547
  customer_match_user_list_metadata: metadata,
546
548
  });
547
549
  const response = await customer.userData.uploadUserData(request);
@@ -556,18 +558,36 @@ class GoogleAds {
556
558
  * @see https://developers.google.com/google-ads/api/reference/rpc/latest/UserDataOperation
557
559
  * @param {string} operationType either 'create' or 'remove'
558
560
  * @param {Array<CustomerMatchRecord>} customerMatchRecords userIds
559
- * @param {string} userListType One of the following hashed_email, hashed_phone_number,
560
- * mobile_id, third_party_user_id or address_info
561
561
  * @return {Array<UserDataOperation>}
562
562
  * @private
563
563
  */
564
- buildOperationsList_(operationType, customerMatchRecords, userListType) {
565
- const userIdentifiers = customerMatchRecords.map((customerMatchRecord) => {
566
- return UserIdentifier.create(
567
- {[userListType]: customerMatchRecord[userListType]});
564
+ buildOperationsList_(operationType, customerMatchRecords) {
565
+ return customerMatchRecords.map((customerMatchRecord) => {
566
+ const userIdentifiers = [];
567
+ IDENTIFIERS.forEach((idType) => {
568
+ const idValue = customerMatchRecord[idType];
569
+ if (idValue) {
570
+ if (Array.isArray(idValue)) {
571
+ idValue.forEach((user) => {
572
+ userIdentifiers.push(UserIdentifier.create({[idType]: user}));
573
+ });
574
+ } else {
575
+ userIdentifiers.push(UserIdentifier.create({[idType]: idValue}));
576
+ }
577
+ }
578
+ });
579
+ let userData;
580
+ if (userIdentifiers.length <= MAX_IDENTIFIERS_PER_USER) {
581
+ userData = UserData.create({user_identifiers: userIdentifiers});
582
+ } else {
583
+ this.logger.warn(
584
+ `Too many user identifiers, will only send ${MAX_IDENTIFIERS_PER_USER}:`,
585
+ JSON.stringify(customerMatchRecord));
586
+ userData = UserData.create({user_identifiers: userIdentifiers}.slice(0,
587
+ MAX_IDENTIFIERS_PER_USER));
588
+ }
589
+ return UserDataOperation.create({[operationType]: userData});
568
590
  });
569
- const userData = UserData.create({user_identifiers: userIdentifiers});
570
- return UserDataOperation.create({[operationType]: userData});
571
591
  }
572
592
 
573
593
  /**
package/src/apis/index.js CHANGED
@@ -88,7 +88,7 @@ exports.bigquery = require('./bigquery.js');
88
88
  * APIs integration class for Google Ads.
89
89
  * @const {{
90
90
  * GoogleAds:!GoogleAds,
91
- * ClickConversionConfig:!ClickConversionConfig,
91
+ * ConversionConfig:!ConversionConfig,
92
92
  * CustomerMatchConfig: !CustomerMatchConfig,
93
93
  * CustomerMatchRecord: !CustomerMatchRecord,
94
94
  * ReportQueryConfig:!ReportQueryConfig,