From b2331a0c1855c7be06e1474855d3cec51d1f1c66 Mon Sep 17 00:00:00 2001 From: soheilv Date: Fri, 11 Mar 2022 17:32:54 +0000 Subject: [PATCH] Updated to ES6, added exception handling --- advanced/shoppingContent.gs | 128 ++++++++++++++---------- advanced/tagManager.gs | 190 ++++++++++++++++++++---------------- 2 files changed, 181 insertions(+), 137 deletions(-) diff --git a/advanced/shoppingContent.gs b/advanced/shoppingContent.gs index 69f1ad0d4..d27b41994 100644 --- a/advanced/shoppingContent.gs +++ b/advanced/shoppingContent.gs @@ -18,9 +18,9 @@ * Inserts a product into the products list. Logs the API response. */ function productInsert() { - var merchantId = 123456; // Replace this with your Merchant Center ID. + const merchantId = 123456; // Replace this with your Merchant Center ID. // Create a product resource and insert it - var productResource = { + const productResource = { 'offerId': 'book123', 'title': 'A Tale of Two Cities', 'description': 'A classic novel about the French Revolution', @@ -52,8 +52,14 @@ function productInsert() { } }; - response = ShoppingContent.Products.insert(productResource, merchantId); - Logger.log(response); // RESTful insert returns the JSON object as a response. + try { + response = ShoppingContent.Products.insert(productResource, merchantId); + // RESTful insert returns the JSON object as a response. + Logger.log(response); + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); + } } // [END apps_script_shopping_product_insert] @@ -62,26 +68,31 @@ function productInsert() { * Lists the products for a given merchant. */ function productList() { - var merchantId = 123456; // Replace this with your Merchant Center ID. - var pageToken; - var pageNum = 1; - var maxResults = 10; - do { - var products = ShoppingContent.Products.list(merchantId, { - pageToken: pageToken, - maxResults: maxResults - }); - Logger.log('Page ' + pageNum); - if (products.resources) { - for (var i = 0; i < products.resources.length; i++) { - Logger.log('Item [' + i + '] ==> ' + products.resources[i]); + const merchantId = 123456; // Replace this with your Merchant Center ID. + let pageToken; + let pageNum = 1; + const maxResults = 10; + try { + do { + const products = ShoppingContent.Products.list(merchantId, { + pageToken: pageToken, + maxResults: maxResults + }); + Logger.log('Page ' + pageNum); + if (products.resources) { + for (let i = 0; i < products.resources.length; i++) { + Logger.log('Item [' + i + '] ==> ' + products.resources[i]); + } + } else { + Logger.log('No more products in account ' + merchantId); } - } else { - Logger.log('No more products in account ' + merchantId); - } - pageToken = products.nextPageToken; - pageNum++; - } while (pageToken); + pageToken = products.nextPageToken; + pageNum++; + } while (pageToken); + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); + } } // [END apps_script_shopping_product_list] @@ -93,7 +104,7 @@ function productList() { * @param {object} productResource3 The third product resource. */ function custombatch(productResource1, productResource2, productResource3) { - var merchantId = 123456; // Replace this with your Merchant Center ID. + const merchantId = 123456; // Replace this with your Merchant Center ID. custombatchResource = { 'entries': [ { @@ -119,8 +130,13 @@ function custombatch(productResource1, productResource2, productResource3) { } ] }; - var response = ShoppingContent.Products.custombatch(custombatchResource); - Logger.log(response); + try { + const response = ShoppingContent.Products.custombatch(custombatchResource); + Logger.log(response); + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); + } } // [END apps_script_shopping_product_batch_insert] @@ -131,37 +147,43 @@ function custombatch(productResource1, productResource2, productResource3) { */ function updateAccountTax() { // Replace this with your Merchant Center ID. - var merchantId = 123456; + const merchantId = 123456; // Replace this with the account that you are updating taxes for. - var accountId = 123456; + const accountId = 123456; - var accounttax = ShoppingContent.Accounttax.get(merchantId, accountId); - Logger.log(accounttax); + try { + const accounttax = ShoppingContent.Accounttax.get(merchantId, accountId); + Logger.log(accounttax); - var taxInfo = { - accountId: accountId, - rules: [ - { - 'useGlobalRate': true, - 'locationId': 21135, - 'shippingTaxed': true, - 'country': 'US' - }, - { - 'ratePercent': 3, - 'locationId': 21136, - 'country': 'US' - }, - { - 'ratePercent': 2, - 'locationId': 21160, - 'shippingTaxed': true, - 'country': 'US' - } - ] - }; + const taxInfo = { + accountId: accountId, + rules: [ + { + 'useGlobalRate': true, + 'locationId': 21135, + 'shippingTaxed': true, + 'country': 'US' + }, + { + 'ratePercent': 3, + 'locationId': 21136, + 'country': 'US' + }, + { + 'ratePercent': 2, + 'locationId': 21160, + 'shippingTaxed': true, + 'country': 'US' + } + ] + }; - Logger.log(ShoppingContent.Accounttax.update(taxInfo, merchantId, accountId)); + Logger.log(ShoppingContent.Accounttax + .update(taxInfo, merchantId, accountId)); + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); + } } // [END apps_script_shopping_account_info] diff --git a/advanced/tagManager.gs b/advanced/tagManager.gs index 852b97c62..f088e9f06 100644 --- a/advanced/tagManager.gs +++ b/advanced/tagManager.gs @@ -15,58 +15,64 @@ */ // [START apps_script_tag_manager_create_version] /** - * Creates a container version for a particular account with the input accountPath. + * Creates a container version for a particular account + * with the input accountPath. * @param {string} accountPath The account path. * @return {string} The tag manager container version. */ function createContainerVersion(accountPath) { - var date = new Date(); + const date = new Date(); // Creates a container in the account, using the current timestamp to make // sure the container is unique. - var container = TagManager.Accounts.Containers.create( - { - 'name': 'appscript tagmanager container ' + date.getTime(), - 'usageContext': ['WEB'] - }, - accountPath); - var containerPath = container.path; - // Creates a workspace in the container to track entity changes. - var workspace = TagManager.Accounts.Containers.Workspaces.create( - {'name': 'appscript workspace', 'description': 'appscript workspace'}, - containerPath); - var workspacePath = workspace.path; - // Creates a random value variable. - var variable = TagManager.Accounts.Containers.Workspaces.Variables.create( - {'name': 'apps script variable', 'type': 'r'}, - workspacePath); - // Creates a trigger that fires on any page view. - var trigger = TagManager.Accounts.Containers.Workspaces.Triggers.create( - {'name': 'apps script trigger', 'type': 'PAGEVIEW'}, - workspacePath); - // Creates a arbitary pixel that fires the tag on all page views. - var tag = TagManager.Accounts.Containers.Workspaces.Tags.create( - { - 'name': 'apps script tag', - 'type': 'img', - 'liveOnly': false, - 'parameter': [ - {'type': 'boolean', 'key': 'useCacheBuster', 'value': 'true'}, { - 'type': 'template', - 'key': 'cacheBusterQueryParam', - 'value': 'gtmcb' - }, - {'type': 'template', 'key': 'url', 'value': '//example.com'} - ], - 'firingTriggerId': [trigger.triggerId] - }, - workspacePath); - // Creates a container version with the variabe, trigger, and tag. - var version = TagManager.Accounts.Containers.Workspaces - .create_version( - {'name': 'apps script version'}, workspacePath) - .containerVersion; - Logger.log(version); - return version; + try { + const container = TagManager.Accounts.Containers.create( + { + 'name': 'appscript tagmanager container ' + date.getTime(), + 'usageContext': ['WEB'] + }, + accountPath); + const containerPath = container.path; + // Creates a workspace in the container to track entity changes. + const workspace = TagManager.Accounts.Containers.Workspaces.create( + {'name': 'appscript workspace', 'description': 'appscript workspace'}, + containerPath); + const workspacePath = workspace.path; + // Creates a random value variable. + const variable = TagManager.Accounts.Containers.Workspaces.Variables.create( + {'name': 'apps script variable', 'type': 'r'}, + workspacePath); + // Creates a trigger that fires on any page view. + const trigger = TagManager.Accounts.Containers.Workspaces.Triggers.create( + {'name': 'apps script trigger', 'type': 'PAGEVIEW'}, + workspacePath); + // Creates a arbitary pixel that fires the tag on all page views. + const tag = TagManager.Accounts.Containers.Workspaces.Tags.create( + { + 'name': 'apps script tag', + 'type': 'img', + 'liveOnly': false, + 'parameter': [ + {'type': 'boolean', 'key': 'useCacheBuster', 'value': 'true'}, { + 'type': 'template', + 'key': 'cacheBusterQueryParam', + 'value': 'gtmcb' + }, + {'type': 'template', 'key': 'url', 'value': '//example.com'} + ], + 'firingTriggerId': [trigger.triggerId] + }, + workspacePath); + // Creates a container version with the variabe, trigger, and tag. + const version = TagManager.Accounts.Containers.Workspaces + .create_version( + {'name': 'apps script version'}, workspacePath) + .containerVersion; + Logger.log(version); + return version; + } catch (e) { + // TODO (Developer) - Handle exception + Logger.log('Failed with error: %s', e.error); + } } // [END apps_script_tag_manager_create_version] @@ -77,7 +83,7 @@ function createContainerVersion(accountPath) { * @return {string} The container path. */ function grabContainerPath(versionPath) { - var pathParts = versionPath.split('/'); + const pathParts = versionPath.split('/'); return pathParts.slice(0, 4).join('/'); } @@ -87,17 +93,22 @@ function grabContainerPath(versionPath) { * @param {object} version The container version. */ function publishVersionAndQuickPreviewDraft(version) { - var containerPath = grabContainerPath(version.path); - // Publish the input container version. - TagManager.Accounts.Containers.Versions.publish(version.path); - var workspace = TagManager.Accounts.Containers.Workspaces.create( - {'name': 'appscript workspace', 'description': 'appscript workspace'}, - containerPath); - var workspaceId = workspace.path; - // Quick previews the current container draft. - var quickPreview = TagManager.Accounts.Containers.Workspaces.quick_preview( - workspace.path); - Logger.log(quickPreview); + try { + const containerPath = grabContainerPath(version.path); + // Publish the input container version. + TagManager.Accounts.Containers.Versions.publish(version.path); + const workspace = TagManager.Accounts.Containers.Workspaces.create( + {'name': 'appscript workspace', 'description': 'appscript workspace'}, + containerPath); + const workspaceId = workspace.path; + // Quick previews the current container draft. + const quickPreview = TagManager.Accounts.Containers.Workspaces + .quick_preview(workspace.path); + Logger.log(quickPreview); + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); + } } // [END apps_script_tag_manager_publish_version] @@ -108,7 +119,7 @@ function publishVersionAndQuickPreviewDraft(version) { * @return {string} The container path. */ function grabContainerPath(versionPath) { - var pathParts = versionPath.split('/'); + const pathParts = versionPath.split('/'); return pathParts.slice(0, 4).join('/'); } @@ -118,20 +129,26 @@ function grabContainerPath(versionPath) { * @param {object} version The container version object. */ function createAndReauthorizeUserEnvironment(version) { - // Creates a container version. - var containerPath = grabContainerPath(version.path); - // Creates a user environment that points to a container version. - var environment = TagManager.Accounts.Containers.Environments.create( - { - 'name': 'test_environment', - 'type': 'user', - 'containerVersionId': version.containerVersionId - }, - containerPath); - Logger.log('Original user environment: ' + environment); - // Reauthorizes the user environment that points to a container version. - TagManager.Accounts.Containers.Environments.reauthorize({}, environment.path); - Logger.log('Reauthorized user environment: ' + environment); + try { + // Creates a container version. + const containerPath = grabContainerPath(version.path); + // Creates a user environment that points to a container version. + const environment = TagManager.Accounts.Containers.Environments.create( + { + 'name': 'test_environment', + 'type': 'user', + 'containerVersionId': version.containerVersionId + }, + containerPath); + Logger.log('Original user environment: ' + environment); + // Reauthorizes the user environment that points to a container version. + TagManager.Accounts.Containers.Environments.reauthorize( + {}, environment.path); + Logger.log('Reauthorized user environment: ' + environment); + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); + } } // [END apps_script_tag_manager_create_user_environment] @@ -141,20 +158,25 @@ function createAndReauthorizeUserEnvironment(version) { * @param {string} accountPath The account path. */ function logAllAccountUserPermissionsWithContainerAccess(accountPath) { - var userPermissions = + try { + const userPermissions = TagManager.Accounts.User_permissions.list(accountPath).userPermission; - for (var i = 0; i < userPermissions.length; i++) { - var userPermission = userPermissions[i]; - if ('emailAddress' in userPermission) { - var containerAccesses = userPermission.containerAccess; - for (var j = 0; j < containerAccesses.length; j++) { - var containerAccess = containerAccesses[j]; - Logger.log( - 'emailAddress:' + userPermission.emailAddress + ' containerId:' + - containerAccess.containerId + ' containerAccess:' + - containerAccess.permission); + for (let i = 0; i < userPermissions.length; i++) { + const userPermission = userPermissions[i]; + if ('emailAddress' in userPermission) { + const containerAccesses = userPermission.containerAccess; + for (let j = 0; j < containerAccesses.length; j++) { + const containerAccess = containerAccesses[j]; + Logger.log( + 'emailAddress:' + userPermission.emailAddress + + ' containerId:' + containerAccess.containerId + + ' containerAccess:' + containerAccess.permission); + } } } + } catch (e) { + // TODO (Developer) - Handle exceptions + Logger.log('Failed with error: $s', e.error); } } // [END apps_script_tag_manager_log]