This guide explains how to use the
EmailPreferences
resource to manage your Merchant Center email
notifications, and see which
types of emails your account's
users
receive.
For the full list of email notification types, see the EmailPreferences
reference.
You can only use the Merchant API to manage email notifications for accounts where you're added as a user.
To view or update email notifications, you need the following values:
- The email address of the user that receives the emails. You can find their
email address in the
name
field of theUser
resource. - The merchant ID of the account to start or stop receiving emails about. If the merchant account is an advanced account, and the user has access to the advanced account, use the merchant ID of the advanced account.
Opt in or out of notifications
To update the email notification settings for a specific merchant account, call
accounts.v1beta.accounts.users.updateEmailPreferences
with the ACCOUNT_ID
of the account and the email address of the user.
Use the following values to control the email notifications the user receives:
OPTED_IN
: Receive email notifications for the account.OPTED_OUT
: Don't receive email notifications for the account.
Here's a sample request to opt a user out of email notifications for a specific merchant account:
PATCH https://merchantapi.googleapis.com/accounts/v1beta/accounts/ACCOUNT_ID/users/EMAIL_ADDRESS/emailPreferences
{
"newsAndTips": "OPTED_OUT"
}
To retrieve the email preferences for a particular user, you can use the
google.shopping.merchant.accounts.v1beta.UpdateEmailPreferencesRequest
method, as shown in the following sample.
Java
public static void updateEmailPreferences(Config config, String email) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
EmailPreferencesServiceSettings emailPreferencesServiceSettings =
EmailPreferencesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates EmailPreferences name to identify EmailPreferences.
String name =
EmailPreferencesName.newBuilder()
.setAccount(config.getAccountId().toString())
.setEmail(email)
.build()
.toString();
// Create a EmailPreferences with the updated fields.
EmailPreferences emailPreferences =
EmailPreferences.newBuilder().setName(name).setNewsAndTips(OptInState.OPTED_IN).build();
FieldMask fieldMask = FieldMask.newBuilder().addPaths("news_and_tips").build();
try (EmailPreferencesServiceClient emailPreferencesServiceClient =
EmailPreferencesServiceClient.create(emailPreferencesServiceSettings)) {
UpdateEmailPreferencesRequest request =
UpdateEmailPreferencesRequest.newBuilder()
.setEmailPreferences(emailPreferences)
.setUpdateMask(fieldMask)
.build();
System.out.println("Sending Update EmailPreferences request");
EmailPreferences response = emailPreferencesServiceClient.updateEmailPreferences(request);
System.out.println("Updated EmailPreferences Name below");
System.out.println(response.getName());
} catch (Exception e) {
System.out.println(e);
}
}
View existing notifications
To see the current email notification settings for a specific user, call
accounts.v1beta.accounts.users.getEmailPreferences
.
Here's a sample request, where the ACCOUNT_ID
is the merchant ID of the
account the email notifications are about, and the EMAIL_ADDRESS
is the email
address of the user that receives the emails.
GET https://merchantapi.googleapis.com/accounts/v1beta/accounts/ACCOUNT_ID/users/EMAIL_ADDRESS/emailPreferences
Here's a sample response from a successful call:
{
"name": "accounts/ACCOUNT_ID/users/EMAIL_ADDRESS/emailPreferences",
"newsAndTips": "OPTED_IN"
}
To retrieve the email preferences for a particular user, you can use the
google.shopping.merchant.accounts.v1beta.GetEmailPreferencesRequest
package, as shown in the following sample.
Java
public static void getEmailPreferences(Config config, String email) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
EmailPreferencesServiceSettings emailPreferencesServiceSettings =
EmailPreferencesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates EmailPreferences name to identify the EmailPreferences.
String name =
EmailPreferencesName.newBuilder()
.setAccount(config.getAccountId().toString())
.setEmail(email)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (EmailPreferencesServiceClient emailPreferencesServiceClient =
EmailPreferencesServiceClient.create(emailPreferencesServiceSettings)) {
// The name has the format: accounts/{account}/users/{user}/emailPreferences
GetEmailPreferencesRequest request =
GetEmailPreferencesRequest.newBuilder().setName(name).build();
System.out.println("Sending get EmailPreferences request:");
EmailPreferences response = emailPreferencesServiceClient.getEmailPreferences(request);
System.out.println("Retrieved EmailPreferences below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
For more information about email notifications, see Change your Merchant Center email preferences.