{"id":1953,"date":"2021-12-10T14:44:51","date_gmt":"2021-12-10T13:44:51","guid":{"rendered":"https:\/\/webmasterei-prange.de\/automatically-label-new-products-in-google-merchant-center\/"},"modified":"2025-10-06T20:50:57","modified_gmt":"2025-10-06T18:50:57","slug":"automatically-label-new-products-in-google-merchant-center","status":"publish","type":"post","link":"https:\/\/webmasterei-prange.de\/en\/automatically-label-new-products-in-google-merchant-center\/","title":{"rendered":"Automatically label new products in Google Merchant Center"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Requirements<\/h2>\n\n<p>For the implementation of this solution, you need:<\/p>\n\n<ul class=\"wp-block-list\">\n<li>Read access to Google Merchant Center<\/li>\n\n\n\n<li><a href=\"https:\/\/webmasterei-prange.de\/google-merchant-center-daten-zu-bigquery-uebertragen-bigquery-data-transfer-service\/\">Google Merchant Center data in BigQuery<\/a><\/li>\n\n\n\n<li>Access to Google Ads Script \/ Google Apps Scripts<\/li>\n<\/ul>\n\n<h2 class=\"wp-block-heading\">Objective<\/h2>\n\n<p>To supplement data in Google Merchant Center, there&#8217;s the option of &#8220;supplemental feeds.&#8221; These trigger product updates based on an item number and other attributes. You can create such a feed using Google Spreadsheets, BigQuery, and an AppScript that retrieves the data from BigQuery and writes it to a spreadsheet.  <\/p>\n\n<h2 class=\"wp-block-heading\">Query for new products in Google Merchant Center via BigQuery<\/h2>\n\n<p>First, you need to determine the latest products in BigQuery and create a table with an output in the format offer_id, customLabel0.<\/p>\n\n<p>We achieve this in BigQuery with the following query:<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT\n  offerId AS id,\n  'New' AS custom_label_0\nFROM (\n SELECT\n  *\n FROM\n  `mnyproject.gmc_data.SHOPPING_CONTENT`\n WHERE\n  DATE(_PARTITIONTIME) = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))\nWHERE\n  creationDate &gt; DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)\nORDER BY\n  creationDate<\/pre>\n\n<p>This query could now be executed directly in the apps script. However, to make the code more maintainable, it&#8217;s better to save the query directly in BigQuery as a view. <\/p>\n\n<p>To specify the date range when products are to be classified as new, you must change the 30 in line 12 to any value.<\/p>\n\n<h2 class=\"wp-block-heading\">Output new products in Google Spreadsheet<\/h2>\n\n<p>To make the products available in Google Spreadsheets we use a <a href=\"https:\/\/developers.google.com\/apps-script\" target=\"_blank\" rel=\"noreferrer noopener\">Google Apps Script<\/a> .<\/p>\n\n<p>The script executes a query based on the query just defined, formats the data for spreadsheets, and writes it to the spreadsheet. The script is a modified version <a href=\"https:\/\/github.com\/googleworkspace\/apps-script-samples\/blob\/master\/advanced\/bigquery.gs\" target=\"_blank\" rel=\"noreferrer noopener\">of this Google Script<\/a> . <\/p>\n\n<p>Before executing, you should correctly fill in the variables in lines 12-17. In the created spreadsheet, the sheet name must be as shown in line 17. <\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function doPost(e){\n  runQuery()\n  return HtmlService.createHtmlOutput(\"script complete\");\n}\n\n\/**\n  * This Script collects all Products in Merchant Center that have been created within the lookback window.\n  *\/\nfunction runQuery() {\n  \/\/ Replace this value with the project ID listed in the Google\n  \/\/ Cloud Platform project.\n  var projectId = 'MYPROJECT';\n  var dataset = 'gmc_data';\n  var tableid = 'SHOPPING_CONTENT';\n  var tablepath = projectId + '.' + dataset + '.' + tableid;\n  var spreadsheet = 'MYSHEETURL';\n  var sheetname = 'New Products';\n  var labelName = 'New ';\n  var lookback = 60\n\n  var request = {\n  query: 'SELECT offerId as id, \"' + labelName + '\" as custom_label_2 FROM (SELECT * FROM '+tablepath+' WHERE DATE(_PARTITIONTIME) = DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY)) WHERE creationDate &gt; DATE_ADD(CURRENT_DATE(), INTERVAL -'+lookback+' DAY) ORDER BY creationDate',\n  useLegacySql: false\n  };\n  var queryResults = BigQuery.Jobs.query(request, projectId);\n  var jobId = queryResults.jobReference.jobId;\n\n  \/\/ Check on status of the query job.\n  var sleepTimeMs = 500;\n  while (!queryResults.jobComplete) {\n  Utilities.sleep(sleepTimeMs);\n  sleepTimeMs *= 2;\n  queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);\n  }\n\n  \/\/ Get all the rows of results.\n  var rows = queryResults.rows;\n  while (queryResults.pageToken) {\n  queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId, {\n  pageToken: queryResults.pageToken\n  });\n  rows = rows.concat(queryResults.rows);\n  }\n\n  if (rows) {\n  var spreadsheet = SpreadsheetApp.openByUrl(spreadsheet);\n  var sheet = spreadsheet.getSheetByName(sheetname);\n  sheet.clear();\n  \/\/ Append the headers.\n  var headers = queryResults.schema.fields.map(function(field) {\n  return field.name;\n  });\n  sheet.appendRow(headers);\n\n  \/\/ Append the results.\n  var data = new Array(rows.length);\n  for (var i = 0; i  &lt;  rows.length; i++) {\n  var cols = rows[i].f;\n  data[i] = new Array(cols.length);\n  for (var j = 0; j  &lt;  cols.length; j++) {\n  data[i][j] = cols[j].v;\n  }\n  }\n  sheet.getRange(2, 1, rows.length, headers.length).setValues(data);\n\n  Logger.log('Results spreadsheet created: %s',\n  spreadsheet.getUrl());\n  } else {\n  Logger.log('No rows returned.');\n  }\n}<\/pre>\n\n<p><a href=\"https:\/\/gist.github.com\/Webmasterei\/e1c871e5d5a9bcdc29468bc7ad019cd8#file-neu-labels-in-spreadsheet-js\">New Labels in Spreadsheet.js<\/a> hosted with \u2764 by <a href=\"https:\/\/github.com\/\">GitHub<\/a><\/p>\n\n<h2 class=\"wp-block-heading\">Add spreadsheet as an additional feed in the Merchant Center<\/h2>\n\n<ol class=\"wp-block-list\">\n<li>Open the customer&#8217;s Merchant Center that should receive the supplemental feed.<\/li>\n\n\n\n<li>Navigate to Products &gt; Feeds<\/li>\n\n\n\n<li>Select the \u201cAdd subfeed\u201d button at the bottom of the screen.<\/li>\n\n\n\n<li>Follow the subsequent steps and select the corresponding spreadsheet.<\/li>\n\n\n\n<li>It&#8217;s important to set which main feed these changes should affect.<\/li>\n<\/ol>\n\n<h2 class=\"wp-block-heading\">Problem: Triggering the sequence time-efficiently<\/h2>\n\n<p>One problem with this solution is the different times at which data is created:<\/p>\n\n<ol class=\"wp-block-list\">\n<li>Data is transferred from Merchant Center to BigQuery<\/li>\n\n\n\n<li>The Apps Script is triggered, and data is written to the sheet<\/li>\n\n\n\n<li>The Google Sheet is retrieved by the Merchant Center.<\/li>\n<\/ol>\n\n<p>If you start setting this up using time-based triggers, the risk increases that the data will only be updated when it&#8217;s out of date. Therefore, we recommend using a procedure like Gerald Maier&#8217;s in &#8221; <a href=\"https:\/\/www.linkedin.com\/pulse\/google-analytics-rohdaten-import-als-trigger-f%C3%BCr-bigquery-maier\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google Analytics Raw Data Import as a Trigger for BigQuery Queries<\/a> .&#8221; <\/p>\n\n<p>In this case, the Apps Script can be triggered immediately after the data transfer from BigQuery is complete. This allows the time required for the Merchant Center data transfer to BigQuery plus 1 hour to be used for the update in Google Merchant Center. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirements For the implementation of this solution, you need: Objective To supplement data in Google Merchant Center, there&#8217;s the option of &#8220;supplemental feeds.&#8221; These trigger product updates based on an item number and other attributes. You can create such a feed using Google Spreadsheets, BigQuery, and an AppScript that retrieves&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","slim_seo":{"title":"Automatically label new products in Google Merchant Center","description":"Automate the labeling of new products in Merchant Center with Google Sheets & Apps Script, eliminating any manual work."},"footnotes":""},"categories":[28,24,32,31],"tags":[],"class_list":["post-1953","post","type-post","status-publish","format-standard","hentry","category-big-query","category-google-ads-optimization","category-google-ads-scripts","category-google-shopping"],"taxonomy_info":{"category":[{"value":28,"label":"Big Query"},{"value":24,"label":"Google Ads Optimization"},{"value":32,"label":"Google Ads Scripts"},{"value":31,"label":"Google Shopping"}]},"featured_image_src_large":false,"author_info":{"display_name":"admin","author_link":"https:\/\/webmasterei-prange.de\/en\/author\/admin\/"},"comment_info":0,"category_info":[{"term_id":28,"name":"Big Query","slug":"big-query","term_group":0,"term_taxonomy_id":28,"taxonomy":"category","description":"","parent":0,"count":12,"filter":"raw","cat_ID":28,"category_count":12,"category_description":"","cat_name":"Big Query","category_nicename":"big-query","category_parent":0},{"term_id":24,"name":"Google Ads Optimization","slug":"google-ads-optimization","term_group":0,"term_taxonomy_id":24,"taxonomy":"category","description":"","parent":0,"count":12,"filter":"raw","cat_ID":24,"category_count":12,"category_description":"","cat_name":"Google Ads Optimization","category_nicename":"google-ads-optimization","category_parent":0},{"term_id":32,"name":"Google Ads Scripts","slug":"google-ads-scripts","term_group":0,"term_taxonomy_id":32,"taxonomy":"category","description":"","parent":0,"count":10,"filter":"raw","cat_ID":32,"category_count":10,"category_description":"","cat_name":"Google Ads Scripts","category_nicename":"google-ads-scripts","category_parent":0},{"term_id":31,"name":"Google Shopping","slug":"google-shopping","term_group":0,"term_taxonomy_id":31,"taxonomy":"category","description":"","parent":0,"count":7,"filter":"raw","cat_ID":31,"category_count":7,"category_description":"","cat_name":"Google Shopping","category_nicename":"google-shopping","category_parent":0}],"tag_info":false,"_links":{"self":[{"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/posts\/1953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/comments?post=1953"}],"version-history":[{"count":1,"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/posts\/1953\/revisions"}],"predecessor-version":[{"id":1954,"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/posts\/1953\/revisions\/1954"}],"wp:attachment":[{"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/media?parent=1953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/categories?post=1953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webmasterei-prange.de\/en\/wp-json\/wp\/v2\/tags?post=1953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}