Le origini dati automatiche semplificano l'invio dei dati di prodotto a Google. Queste origini dati assicurano che le informazioni più aggiornate sui prodotti pertinenti del tuo sito web raggiungano Google.
Questa pagina mostra come creare e aggiornare in modo programmatico le origini dati che ti consentono di inserire i prodotti.
L'API Content for Shopping ti consente di creare origini dati principali. Con l'API Origini dati del commerciante, puoi anche creare i seguenti tipi di origini dati:
- Origini dati di prodotto principali
- Origini dati di prodotto supplementari
- Origini dati dell'inventario locale
- Origini dati dell'inventario regionale
- Origini dati delle promozioni
- Origini dati delle recensioni prodotto
- Origini dati delle recensioni del commerciante
L'API Content for Shopping ti consente di gestire solo le origini dati con input file. L'API Merchant ti consente di gestire le origini dati con input sia di file che di API.
Per ulteriori informazioni sul confronto con l'API Content for Shopping, consulta Eseguire la migrazione della gestione delle origini dati.
Con l'API Merchant Data Sources, puoi:
- Crea un'origine dati principale con
feedLabel
econtentLanguage
specifici. - Crea un'origine dati in cui non sono impostati i campi
feedLabel
econtentLanguage
. Con questo tipo di origine dati puoi scegliere come target più paesi per i tuoi prodotti, in quanto puoi inserire prodotti con diverse combinazioni difeedLabel
econtentLanguage
in un'unica origine dati. - Crea un'origine dati supplementare da collegare a un'origine dati principale esistente.
- Configura una pianificazione per un'origine dati file.
- Registra il tuo account per la gestione automatica delle origini dati.
- Gestisci le origini dati dell'API.
- Gestisci la regola predefinita delle origini dati utilizzando le origini dati di prodotto principali.
- Utilizza altri tipi di origini dati, come le promozioni.
Per saperne di più sui canali delle origini dati, consulta Canali.
Prerequisiti
- Per il tuo account deve essere stata eseguita la migrazione ai feed per singole lingue.
Per verificare che la migrazione dell'account alla suddivisione dei target di dati sia già stata eseguita, utilizza l'elenco delle origini dati o i metodi get. Se non soddisfi i requisiti di idoneità, riceverai il seguente messaggio di eccezione e dovrai contattare l'assistenza.
This account is in the data sources migration process and can't be used with this API yet. Contact support for more info on when this account will be able to use the data sources endpoint.
Creare un'origine dati
Le origini dati principali sono le origini dati principali per il tuo inventario Merchant Center. Puoi aggiungere o rimuovere prodotti solo utilizzando un'origine dati principale. Se ogni prodotto aggiunto all'origine dati principale soddisfa i dati e i requisiti di idoneità di Merchant Center, non dovrai creare altre origini dati.
Per creare un'origine dati principale con feedLabel
e
contentLanguage
specifici, imposta i campi feedLabel
e contentLanguage
nella
configurazione specifica per tipo. Per ulteriori informazioni su questi campi, consulta PrimaryProductDataSource
.
La seguente richiesta di esempio mostra come creare un'origine dati di prodotto principale:
POST https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID} /dataSources
{
"displayName": "{DISPLAY_NAME} ",
"primaryProductDataSource": {
"contentLanguage": "{CONTENT_LANGUAGE} ",
"feedLabel": "{FEED_LABEL} ",
"countries": [
"{COUNTRY} "
],
"channel": "ONLINE_PRODUCTS"
}
}
Sostituisci quanto segue:
- {ACCOUNT_ID}: l'identificatore univoco del tuo account Merchant Center.
- {DISPLAY_NAME}: il nome visualizzato dell'origine dati.
- {CONTENT_LANGUAGE}: il codice lingua ISO 639-1 a due lettere dei prodotti nell'origine dati.
- {FEED_LABEL}: l'etichetta del feed dell'origine dati.
- {COUNTRY}: il codice di territorio CLDR del paese di destinazione dei prodotti che verranno caricati utilizzando l'origine dati.
Una volta eseguita correttamente la richiesta, viene visualizzata la seguente risposta:
{
"name": "accounts/{ACCOUNT_ID} /dataSources/{DATASOURCE_ID} ",
"dataSourceId": "{DATASOURCE_ID} ",
"displayName": "{DISPLAY_NAME} ",
"primaryProductDataSource": {
"channel": "ONLINE_PRODUCTS",
"feedLabel": "{FEED_LABEL} ",
"contentLanguage": "{CONTENT_LANGUAGE} ",
"countries": [
"{COUNTRY} "
],
"defaultRule": {
"takeFromDataSources": [
{
"self": true
}
]
}
},
"input": "API"
}
Per ulteriori informazioni sulla creazione di un'origine dati, consulta il metodo accounts.dataSources.create.
Per visualizzare l'origine dati appena creata, utilizza il metodo accounts.dataSources.get o accounts.dataSources.list.
L'esempio seguente mostra come creare un'origine dati di prodotto principale per la combinazione GB
e en
feedLabel
e contentLanguage
.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create a primary product datasource for all `feedLabel` and
* `contentLanguage` combinations. Note that rules functionality is limited for wildcard feeds.
*/
public class CreatePrimaryProductDataSourceWildCardSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
public static String createDataSource(Config config, String displayName) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String parent = getParent(config.getAccountId().toString());
// The type of data that this datasource will receive.
PrimaryProductDataSource primaryProductDataSource =
PrimaryProductDataSource.newBuilder()
// Channel can be "ONLINE_PRODUCTS" or "LOCAL_PRODUCTS" or "PRODUCTS" .
// While accepted, datasources with channel "products" representing unified products
// currently cannot be used with the Products bundle.
.setChannel(PrimaryProductDataSource.Channel.ONLINE_PRODUCTS)
.addCountries("GB")
.build();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
CreateDataSourceRequest request =
CreateDataSourceRequest.newBuilder()
.setParent(parent)
.setDataSource(
DataSource.newBuilder()
.setDisplayName(displayName)
.setPrimaryProductDataSource(primaryProductDataSource)
.build())
.build();
System.out.println("Sending Create PrimaryProduct DataSource request");
DataSource response = dataSourcesServiceClient.createDataSource(request);
System.out.println("Created DataSource Name below");
System.out.println(response.getName());
return response.getName();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
// Null is necessary to satisfy the compiler as we're not returning a String on failure.
return null;
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The displayed datasource name in the Merchant Center UI.
String displayName = "Primary Product Data Wildcard";
createDataSource(config, displayName);
}
}
Creare una nuova origine dati principale che ti aiuti a scegliere come target più paesi
Per creare un nuovo feed principale che ti aiuti a scegliere come target più paesi, configura
la tua origine dati utilizzando
PrimaryProductDataSource
e non impostare i campi feedLabel
e contentLanguage
.
Se utilizzi l'API Content for Shopping, viene creata per te una sola origine dati dell'API. Con l'API Merchant Data Sources, puoi avere più origini dati API, alcune delle quali possono non avere i campi feedLabel
e contentLanguage
impostati.
Solo le origini dati con input API possono non avere i campi feedLabel
e
contentLanguage
impostati. Questo tipo di origini dati non è supportato per gli input
di file.
Crea un'origine dati supplementare e collegala all'origine dati principale
Le origini dati supplementari vengono utilizzate solo per aggiornare i dati di prodotto già esistenti in uno o più origini dati principali. Puoi avere più origini dati supplementari e ciascuna può integrare i dati in qualsiasi numero di origini dati principali.
Puoi utilizzare le origini dati supplementari per eseguire aggiornamenti parziali dei dati di prodotto aggiungendo l'identificatore univoco dell'origine dati come parametro di query quando effettui chiamate ai metodi accounts.productInputs.insert
e accounts.productInputs.delete
. Puoi utilizzare le origini dati supplementari solo per aggiornare i prodotti esistenti.
Per creare un'origine dati supplementare, configurala utilizzando
SupplementalProductDataSource
e poi collegala aggiornando il campo defaultRule
nell'origine dati principale.
Le origini dati dei file supplementari devono avere i campi feedLabel
e contentLanguage
impostati. Le origini dati dell'API supplementari devono sempre avere i campi feedLabel
e
contentLanguage
non impostati.
L'esempio seguente mostra come creare un'origine dati per i prodotti supplementari dei file per la combinazione en
e GB
contentLanguage
e feedLabel
.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.FileInput;
import com.google.shopping.merchant.datasources.v1beta.SupplementalProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create a File Supplemental product datasource for the "en" and
* "GB" `feedLabel` and `contentLanguage` combination. This supplemental feed is eligible to be
* linked to both a wildcard primary feed and/or a primary feed with the same `feedLabel` and
* `contentLanguage` combination.
*/
public class CreateFileSupplementalProductDataSourceSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
private static FileInput setFileInput() {
// If FetchSettings are not set, then this will be an `UPLOAD` file type
// that you must manually upload via the Merchant Center UI.
return FileInput.newBuilder()
// FileName is required for `UPLOAD` fileInput type.
.setFileName("British T-shirts Supplemental Data")
.build();
}
public static String createDataSource(Config config, String displayName, FileInput fileInput)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String parent = getParent(config.getAccountId().toString());
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
CreateDataSourceRequest request =
CreateDataSourceRequest.newBuilder()
.setParent(parent)
.setDataSource(
DataSource.newBuilder()
.setDisplayName(displayName)
.setSupplementalProductDataSource(
SupplementalProductDataSource.newBuilder()
.setContentLanguage("en")
.setFeedLabel("GB")
.build())
.setFileInput(fileInput)
.build())
.build();
System.out.println("Sending create SupplementalProduct DataSource request");
DataSource response = dataSourcesServiceClient.createDataSource(request);
System.out.println("Created DataSource Name below");
System.out.println(response.getName());
return response.getName();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
// Null is necessary to satisfy the compiler as we're not returning a String on failure.
return null;
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The displayed datasource name in the Merchant Center UI.
String displayName = "British File Supplemental Product Data";
// The file input data that this datasource will receive.
FileInput fileInput = setFileInput();
createDataSource(config, displayName, fileInput);
}
}
Per creare un'origine dati supplementare che funzioni per tutte le combinazioni di feedLabel
e
contentLanguage
, esegui il seguente esempio.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.SupplementalProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create a Supplemental product datasource all `feedLabel` and
* `contentLanguage` combinations. This works only for API supplemental feeds.
*/
public class CreateSupplementalProductDataSourceWildCardSample {
private static String getParent(String merchantId) {
return String.format("accounts/%s", merchantId);
}
public static String createDataSource(Config config, String displayName) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String parent = getParent(config.getAccountId().toString());
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
CreateDataSourceRequest request =
CreateDataSourceRequest.newBuilder()
.setParent(parent)
.setDataSource(
DataSource.newBuilder()
.setDisplayName(displayName)
.setSupplementalProductDataSource(
SupplementalProductDataSource.newBuilder().build())
.build())
.build();
System.out.println("Sending create SupplementalProduct DataSource request");
DataSource response = dataSourcesServiceClient.createDataSource(request);
System.out.println("Created DataSource Name below");
System.out.println(response.getName());
return response.getName();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
return null; // Necessary to satisfy the compiler as we're not returning a
// String on failure.
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The displayed datasource name in the Merchant Center UI.
String displayName = "Supplemental API Product Data Wildcard";
createDataSource(config, displayName);
}
}
Configurare una pianificazione per l'origine dati file
Per impostare una pianificazione per il feed di file, configura l'origine dati come origine dati file utilizzando il campo FileInput
e poi configura fetchsettings
utilizzando il campo FileInput.FetchSettings
.
Eliminare un'origine dati
Per eliminare un'origine dati esistente dal tuo account, utilizza il metodo
accounts.dataSources.delete
.
L'esempio seguente mostra come utilizzare il pacchetto DeleteDataSourceRequest
per eliminare un'origine dati.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.DeleteDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a datasource. */
public class DeleteDataSourceSample {
public static void deleteDataSource(Config config, String dataSourceId) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String name =
DataSourceName.newBuilder()
.setAccount(config.getAccountId().toString())
.setDatasource(dataSourceId)
.build()
.toString();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
DeleteDataSourceRequest request = DeleteDataSourceRequest.newBuilder().setName(name).build();
System.out.println("Sending deleteDataSource request");
// Delete works for any datasource type.
// If Type "Supplemental", delete will only work if it's not linked to any primary feed.
// If a link exists and the Type is "Supplemental", you will need to remove the supplemental
// feed from the default and/or custom rule(s) of any primary feed(s) that references it. Then
// retry the delete.
dataSourcesServiceClient.deleteDataSource(request); // No response returned on success.
System.out.println(
"Delete successful, note that it may take a few minutes for the delete to update in"
+ " the system.");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// An ID automatically assigned to the datasource after creation by Google.
String dataSourceId = "1111111111"; // Replace with your datasource ID.
deleteDataSource(config, dataSourceId);
}
}
Recupera l'origine dati
Per recuperare un file configurato nell'origine dati, utilizza il metodo
accounts.dataSources.fetch
. Questo metodo esegue il recupero dei dati immediatamente in un'origine dati del tuo account. Questo metodo funziona solo con le origini dati con un set di input file.
Recupera l'origine dati
Per recuperare la configurazione dell'origine dati per il tuo account, utilizza il metodo
accounts.dataSources.get
.
L'esempio seguente mostra come utilizzare il pacchetto GetDataSourceRequest
per recuperare un'origine dati specifica per un determinato account Merchant Center.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.GetDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to get a specific datasource for a given Merchant Center account. */
public class GetDataSourceSample {
public static DataSource getDataSource(Config config, String dataSourceId) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates datasource name to identify datasource.
String name =
DataSourceName.newBuilder()
.setAccount(config.getAccountId().toString())
.setDatasource(dataSourceId)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
// The name has the format: accounts/{account}/datasources/{datasource}
GetDataSourceRequest request = GetDataSourceRequest.newBuilder().setName(name).build();
System.out.println("Sending GET DataSource request:");
DataSource response = dataSourcesServiceClient.getDataSource(request);
System.out.println("Retrieved DataSource below");
System.out.println(response);
return response;
} catch (Exception e) {
System.out.println(e);
System.exit(1);
return null; // Necessary to satisfy the compiler as we're not returning a
// DataSource on failure.
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// An ID assigned to a datasource by Google.
String datasourceId = "1111111111"; // Replace with your datasource ID.
getDataSource(config, datasourceId);
}
}
Elenco origini dati
Per elencare le configurazioni delle origini dati per il tuo account, utilizza il metodo
accounts.dataSources.list
.
L'esempio seguente mostra come utilizzare il pacchetto ListDataSourceRequest
per elencare tutte le origini dati per un determinato account Merchant Center.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient.ListDataSourcesPagedResponse;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.ListDataSourcesRequest;
import java.util.ArrayList;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to list all the datasources for a given Merchant Center account */
public class ListDataSourcesSample {
private static String getParent(String accountId) {
return String.format("accounts/%s", accountId);
}
public static ArrayList<DataSource> listDataSources(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify the account from which to list all the datasources.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
// The parent has the format: accounts/{account}
ListDataSourcesRequest request =
ListDataSourcesRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list datasources request:");
ListDataSourcesPagedResponse response = dataSourcesServiceClient.listDataSources(request);
int count = 0;
ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
ArrayList<DataSource> justPrimaryDataSources = new ArrayList<DataSource>();
// Iterates over all rows in all pages and prints the datasource in each row.
// Automatically uses the `nextPageToken` if returned to fetch all pages of data.
for (DataSource element : response.iterateAll()) {
System.out.println(element);
count++;
dataSources.add(element);
// The below lines show how to filter datasources based on type.
// `element.hasSupplementalProductDataSource()` would give you supplemental
// datasources, etc.
if (element.hasPrimaryProductDataSource()) {
justPrimaryDataSources.add(element);
}
}
System.out.print("The following count of elements were returned: ");
System.out.println(count);
return dataSources;
} catch (Exception e) {
System.out.println(e);
System.exit(1);
return null; // Necessary to satisfy the compiler as we're not returning an
// ArrayList<DataSource> on failure.
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listDataSources(config);
}
}
Origine dati della patch
Per aggiornare la configurazione di un'origine dati esistente, utilizza il metodo
accounts.dataSources.patch
.
L'esempio riportato di seguito mostra come utilizzare il pacchetto UpdateDataSourceRequest
per aggiornare un'origine dati. Inoltre, mostra come aggiornare un'origine dati principale per aggiungere origini dati supplementari alla regola predefinita.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourceReference;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource.DefaultRule;
import com.google.shopping.merchant.datasources.v1beta.UpdateDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to update a datasource to change its name in the MC UI. It also
* demonstrates how to update a primary datasource to add supplemental datasources to its default
* rule (https://support.google.com/merchants/answer/7450276).
*/
public class UpdateDataSourceSample {
public static String updateDataSource(Config config, String displayName, String dataSourceId)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates datasource name to identify datasource.
String name =
DataSourceName.newBuilder()
.setAccount(config.getAccountId().toString())
.setDatasource(dataSourceId)
.build()
.toString();
DataSource dataSource =
DataSource.newBuilder()
// Update the datasource to have the new display name
.setDisplayName(displayName)
.setName(name)
.build();
FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
UpdateDataSourceRequest request =
UpdateDataSourceRequest.newBuilder()
.setDataSource(dataSource)
.setUpdateMask(fieldMask)
.build();
System.out.println("Sending Update DataSource request");
DataSource response = dataSourcesServiceClient.updateDataSource(request);
System.out.println("Updated DataSource Name below");
System.out.println(response.getName());
return response.getName();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
return null;
}
}
public String updateDataSource(
Config config,
String primaryDataSourceName,
String firstSupplementalDataSourceName,
String secondSupplementalDataSourceName)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Setting self to 'true' refers to the primary datasource itself.
DataSourceReference dataSourceReferenceSelf =
DataSourceReference.newBuilder().setSelf(true).build();
DataSourceReference firstSupplementalDataSourceReference =
DataSourceReference.newBuilder()
.setSupplementalDataSourceName(firstSupplementalDataSourceName)
.build();
DataSourceReference secondSupplementalDataSourceReference =
DataSourceReference.newBuilder()
.setSupplementalDataSourceName(secondSupplementalDataSourceName)
.build();
// The attributes will first be taken from the primary DataSource.
// Then the first supplemental DataSource if the attribute is not in the primary DataSource
// And finally the second supplemental DataSource if not in the first two DataSources.
// Note that CustomRules could change the behavior of how updates are applied.
DefaultRule defaultRule =
DefaultRule.newBuilder()
.addTakeFromDataSources(dataSourceReferenceSelf)
.addTakeFromDataSources(firstSupplementalDataSourceReference)
.addTakeFromDataSources(secondSupplementalDataSourceReference)
.build();
// The type of data that this datasource will receive.
PrimaryProductDataSource primaryProductDataSource =
PrimaryProductDataSource.newBuilder().setDefaultRule(defaultRule).build();
DataSource dataSource =
DataSource.newBuilder()
// Update the primary datasource to have the default rule datasources in the correct
// order.
.setPrimaryProductDataSource(primaryProductDataSource)
.setName(primaryDataSourceName)
.build();
// The '.' signifies a nested field.
FieldMask fieldMask =
FieldMask.newBuilder().addPaths("primary_product_data_source.default_rule").build();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
UpdateDataSourceRequest request =
UpdateDataSourceRequest.newBuilder()
.setDataSource(dataSource)
.setUpdateMask(fieldMask)
.build();
System.out.println("Sending Update DataSource request");
DataSource response = dataSourcesServiceClient.updateDataSource(request);
System.out.println("Updated DataSource Name below");
System.out.println(response.getName());
return response.getName();
} catch (Exception e) {
System.out.println(e);
System.exit(1);
return null;
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The updated displayed datasource name in the Merchant Center UI.
String displayName = "Great Britain Primary Product Data";
// The ID of the datasource to update
String dataSourceId = "11111111"; // Replace with your datasource ID.
updateDataSource(config, displayName, dataSourceId);
}
}
Collegare le origini dati
Le origini dei dati di prodotto principali ti consentono di gestire la regola predefinita delle origini dati. La regola predefinita è quella che si applica a tutti gli attributi dell'origine dati. La regola predefinita può essere impostata durante la creazione dell'origine dati o aggiornando un'origine dati esistente tramite il campo della regola predefinita.
Per ulteriori informazioni sulla configurazione delle regole, vedi Configurare le regole per le origini dati di prodotto.
La seguente configurazione di esempio garantisce che tutti gli attributi vengano prima ricavati
dall'origine dati con l'identificatore univoco 1001
. Gli attributi mancanti vengono poi aggiunti dall'origine dati principale. Alla fine, gli attributi rimanenti verranno estratti dall'origine dati supplementare con l'identificatore univoco 1002
, se non sono già forniti in un'altra origine dati. Se lo stesso attributo viene fornito in più origini dati, viene selezionato il valore più alto nell'elenco.
defaultRule {
takeFromDataSources: [
'1001', // Supplemental product data source
'self', // Self reference to the primary data source
'1002' // Supplemental product data source
]
}
Gestione automatica dei feed
Per registrare il tuo account per la gestione automatica delle origini dati, devi svolgere i seguenti passaggi:
- Controlla se il tuo account è idoneo alla registrazione chiamando il metodo
accounts.autofeedSettings.getAutofeedSettings
. - Assicurati che il tuo account non sia un account del marketplace.
Una volta che il tuo account è idoneo alla registrazione, puoi utilizzare il metodo
accounts.autofeedSettings.updateAutofeedSettings
per attivare la gestione automatica delle origini dati. L'attivazione della gestione automatica delle origini dati consente a Google di aggiungere automaticamente i tuoi prodotti dal tuo negozio online e di assicurarsi che siano sempre aggiornati sulle piattaforme di Google.
Recuperare lo stato di caricamento del file
Per ottenere lo stato di un'origine dati con file, recupero o foglio di lavoro, puoi chiamare il metodo GET
del servizio accounts.dataSources.fileUploads
. Per ottenere il risultato dell'ultimo recupero dell'origine dati calcolato
in modo asincrono al termine dell'elaborazione dell'origine dati, utilizza l'identificatore di nome
latest
.
GET https://merchantapi.googleapis.com/accounts/v1beta/{ACCOUNT_ID} /datasources/{DATASOURCE_ID} /fileUploads/latest
Lo stato del caricamento del file potrebbe contenere una visualizzazione dettagliata dei tuoi prodotti, inclusi eventuali potenziali problemi.
Tieni presente che lo stato del caricamento del file potrebbe non esistere se il file non è mai stato caricato. Lo stato del caricamento del file potrebbe essere in fase di elaborazione se richiesto subito dopo il caricamento.