จัดการการแจ้งเตือนทางอีเมลของ Merchant Center

คู่มือนี้จะอธิบายวิธีใช้แหล่งข้อมูล EmailPreferences เพื่อจัดการการแจ้งเตือนทางอีเมลของ Merchant Center และดูประเภทอีเมลที่users ของบัญชีได้รับ ดูรายการประเภทการแจ้งเตือนทางอีเมลทั้งหมดได้ที่ข้อมูลอ้างอิง EmailPreferences

คุณใช้ Merchant API เพื่อจัดการการแจ้งเตือนทางอีเมลได้เฉพาะในบัญชีที่เพิ่มคุณเป็นผู้ใช้

หากต้องการดูหรืออัปเดตการแจ้งเตือนทางอีเมล คุณต้องมีค่าต่อไปนี้

เลือกใช้หรือไม่ใช้การแจ้งเตือน

หากต้องการอัปเดตการตั้งค่าการแจ้งเตือนทางอีเมลสำหรับบัญชีผู้ขายที่เฉพาะเจาะจง ให้โทรไปที่ accounts.v1beta.accounts.users.updateEmailPreferences พร้อมระบุ ACCOUNT_ID ของบัญชีและ EMAIL_ADDRESS ของผู้ใช้

ใช้ค่าต่อไปนี้เพื่อควบคุมการแจ้งเตือนทางอีเมลที่ผู้ใช้ได้รับ

  • OPTED_IN: รับการแจ้งเตือนทางอีเมลสําหรับบัญชี
  • OPTED_OUT: ไม่ได้รับการแจ้งเตือนทางอีเมลสำหรับบัญชี

ตัวอย่างคำขอเลือกไม่รับการแจ้งเตือนทางอีเมลสำหรับบัญชีผู้ขายที่เฉพาะเจาะจงมีดังนี้

PATCH https://merchantapi.googleapis.com/accounts/v1beta/accounts/MERCHANT_ID/users/EMAIL_ADDRESS/emailPreferences

{
  "newsAndTips": "OPTED_OUT"
}

ตัวอย่างการตอบกลับจากการเรียกใช้ที่สำเร็จมีดังนี้

{
  "name": "accounts/MERCHANT_ID/users/EMAIL_ADDRESS/emailPreferences",
  "newsAndTips": "OPTED_OUT"
}

หากต้องการเรียกข้อมูลค่ากําหนดของอีเมลสําหรับผู้ใช้ที่เฉพาะเจาะจง ให้ใช้เมธอด google.shopping.merchant.accounts.v1beta.UpdateEmailPreferencesRequest ดังที่แสดงในตัวอย่างต่อไปนี้

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferences;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferences.OptInState;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferencesName;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferencesServiceClient;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferencesServiceSettings;
import com.google.shopping.merchant.accounts.v1beta.UpdateEmailPreferencesRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to update a EmailPreferences to OPT_IN to News and Tips. This service
 * only permits retrieving and updating email preferences for the authenticated user.
 */
public class UpdateEmailPreferencesSample {

  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);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The email address of this user. If you want to get the user information
    // Of the user making the Content API request, you can also use "me" instead
    // Of an email address.
    // String email = "testUser@gmail.com";
    String email = "me";

    updateEmailPreferences(config, email);
  }
}

ดูการแจ้งเตือนที่มีอยู่

หากต้องการดูการตั้งค่าการแจ้งเตือนทางอีเมลปัจจุบันของผู้ใช้ที่เฉพาะเจาะจง ให้เรียกใช้ accounts.v1beta.accounts.users.getEmailPreferences

ตัวอย่างคำขอมีดังนี้ โดยที่ ACCOUNT_ID คือรหัสผู้ขายของบัญชีที่มีการแจ้งเตือนทางอีเมล และ EMAIL_ADDRESS คืออีเมลของผู้ใช้ที่ได้รับอีเมล

GET https://merchantapi.googleapis.com/accounts/v1beta/accounts/MERCHANT_ID/users/EMAIL_ADDRESS/emailPreferences

ตัวอย่างการตอบกลับจากการเรียกใช้ที่สำเร็จมีดังนี้

{
  "name": "accounts/ACCOUNT_ID/users/EMAIL_ADDRESS/emailPreferences",
  "newsAndTips": "OPTED_IN"
}

หากต้องการเรียกข้อมูลค่ากําหนดของอีเมลสําหรับผู้ใช้ที่เฉพาะเจาะจง ให้ใช้แพ็กเกจ google.shopping.merchant.accounts.v1beta.GetEmailPreferencesRequest ดังที่แสดงในตัวอย่างต่อไปนี้

Java

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferences;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferencesName;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferencesServiceClient;
import com.google.shopping.merchant.accounts.v1beta.EmailPreferencesServiceSettings;
import com.google.shopping.merchant.accounts.v1beta.GetEmailPreferencesRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to get the email preferences of a Merchant Center account. */
public class GetEmailPreferencesSample {

  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);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The email address of this user. If you want to get the user information
    // Of the user making the Content API request, you can also use "me" instead
    // Of an email address.
    String email = "testUser@gmail.com";
    // String email = "me";

    getEmailPreferences(config, email);
  }
}

ดูข้อมูลเพิ่มเติมเกี่ยวกับการแจ้งเตือนทางอีเมลได้ที่เปลี่ยนค่ากำหนดของอีเมล Merchant Center