Вышел Модуль интеграций 2.0 для MedusaЧитать анонс
AI Commerce Search logo

AI Commerce Search

Поиск и рекомендации товаров Google AI Commerce Search (Google Cloud Retail / Vertex AI Search for Retail) для Medusa v2 в виде провайдера модуля.

npm install medusa-ai-commerce-search
Категория
Поиск
Создано
Faridghar
Версия
1.0.0
Ежемесячные загрузки
Загрузка данных
Последнее обновление

Google AI Commerce Search (Google Cloud Retail API / Vertex AI Search for Retail) module provider and client SDK for Medusa v2.

This package provides enterprise AI-powered search, product recommendations, real-time catalog synchronization, and user event tracking for Medusa v2 e-commerce platforms.


Table of Contents


Features

  • Semantic & AI Search: Natural language understanding, auto-correction, query expansion, and faceted filtering powered by Google Cloud Retail API.
  • Personalized Recommendations: Frequently-bought-together, similar items, recommended for you, and custom serving configs.
  • Hierarchical Catalog Ingestion: Full parent-category hierarchies (), breadcrumbs, and collection metadata (, , ).
  • Real-Time Streaming Sync: Automatic product indexing, debounced coalescing for high-frequency updates, and cascading variant deletion.
  • Conversion & Event Tracking: Automated server-side purchase-complete tracking and client-side browsing event analytics.
  • Admin Option Mapping: Map Medusa product options (e.g. , , ) directly to Google Retail system attributes or custom attributes via the Medusa Admin UI.

Installation

Install the backend plugin and search provider in your Medusa backend:

For your storefront (Next.js, Remix, Astro, or vanilla JS), install the companion client tracker:


Automated Onboarding (1-Command GCP Setup)

Open in Cloud Shell

You can provision and configure all required Google Cloud Retail API resources with a single automated CLI setup wizard:

npx medusa-ai-commerce-search setup

What the Setup Wizard Automates:

  1. Google Cloud Authentication: Detects your active account or guides you through logging in with .
  2. Project Selection / Creation: Selects an existing Google Cloud project from your account or provisions a new one with smart auto-generated unique project IDs and conflict retry handling.
  3. Billing Verification & Linkage: Inspects project billing status and links an active Google Cloud Billing account ().
  4. API Enablement: Automatically enables and .
  5. Dedicated Service Account: Creates a dedicated service account and grants it , with automated retries and exponential backoff to handle Google Cloud IAM eventual consistency and replication delays.
  6. Credential Export & Security: Exports the service account key JSON to with strict file permissions () and automatically adds it to .
  7. Environment Configuration: Safely appends or updates required variables in your :
    • : Your target Google Cloud project ID.
    • : Path to your exported credentials key JSON.
  8. Catalog Verification & Terms Activation: Checks whether the Google Cloud Retail Data Use Terms have been accepted. If unaccepted, displays the direct Cloud Console activation link and prompts to accept them before verifying and initializing the default catalog branch () so search, recommendations, and sync endpoints are immediately ready to serve queries.

Zero-Install via Google Cloud Shell

Click the Open in Cloud Shell button above to launch an interactive cloud environment directly in your browser. Google Cloud Shell provides a browser-based terminal with Node.js and pre-installed and pre-authenticated to your Google account. In the Cloud Shell terminal, simply run:

npx medusa-ai-commerce-search setup

Non-Interactive & CI/CD Provisioning

To automate setup inside CI/CD pipelines, Docker environments, or scripted developer bootstrapping, pass non-interactive flags:

CLI Options & Flags:

OptionAliasDefaultDescription
PromptedTarget Google Cloud Project ID (creates if non-existent)
PromptedBilling account ID to link to the project
Destination path for the service account key JSON
Destination file to update
Dedicated service account name
Google Cloud Retail API location
Catalog ID
Branch ID
Non-interactive mode (auto-accept prompts and defaults)
Simulate provisioning actions without altering GCP resources
Skip Retail API catalog branch verification
Show usage help

Configuration

Google Cloud Retail API Credentials

Ensure you have a Google Cloud Project with the Retail API (or Vertex AI Search for Retail) enabled. You can authenticate via:

  1. Automated Setup (Recommended): Run as described above to automate credentials and project setup.
  2. Application Default Credentials (ADC): When running in Google Cloud or with configured, and are automatically forwarded to Retail API clients.
  3. Service Account Key File: Specify .
  4. Explicit Credentials Object: Pass .

Manual GCP Console Configuration Reference

If you prefer configuring resources manually in the Google Cloud Console rather than running the CLI wizard:

  1. Open the Google Cloud Console.
  2. Create or select a Google Cloud project and ensure billing is enabled.
  3. Enable the Retail API under APIs & Services > Library.
  4. Navigate to IAM & Admin > Service Accounts, create a service account named , and assign the () role.
  5. Create and download a JSON key for the service account.
  6. Save the key file to your Medusa project root as (ensure it is added to ).
  7. Add and to your file.

medusa-config.ts

Configure in under both (as the search provider) and (to register API routes, event subscribers, and Medusa Admin extensions):

Module Options

OptionTypeDefaultDescription
RequiredGoogle Cloud Project ID. Sets and for ADC.
Google Cloud Retail API location (, , etc.).
Catalog ID in Google Retail API.
Branch ID for product operations ( is default).
Map of logical serving config aliases to Google Cloud placement names.
Service account credentials object ().
Absolute path to service account JSON key file.
DerivedCustom Retail API endpoint (e.g. ).
Coalesce debounce delay in milliseconds for individual product updates.
.

Initial Catalog Ingestion

To populate your Google Cloud Retail API catalog with all existing Medusa products, run the bulk import script using the Medusa CLI:

This workflow:

  • Queries Medusa products using full category hierarchies () and collection metadata (, , ).
  • Applies product option mappings defined in Medusa Admin settings.
  • Batches products inline via with incremental reconciliation.

Alternatively, trigger the import programmatically or from an admin tool via .


Store & Admin API Endpoints

Store Search ()

Searches the product catalog using Google Cloud Retail API.

Request Body:

Pagination, Sorting & Expansion Parameters:

  • : Continuation token ( from a previous search response) to fetch the next page of results (accepted at the top level or inside ).
  • / : Top-level pagination alternatives to / and / .
  • : Ordering expression string (e.g., , ; trailing such as is automatically normalized to for Google Cloud Retail API).
  • : Object specification for query expansion (e.g., ).
  • : Query expansion condition string (e.g., , ), mapped to .
  • : When , automatically enables .

Response Body:

Store Events ()

Records storefront user events in the Google Cloud Retail API for personalization, ranking, and recommendations.

Request Body:

Supported event types include:

Store Recommendations ()

Fetches personalized product recommendations for a serving configuration.

Request Body:

Response Body:

Admin Options Mapping ( & )

Manages mapping of Medusa product options to Google Retail attributes in store metadata.

  • : Returns .
  • : Saves into store metadata.

Admin Bulk Ingest ()

Runs the workflow and returns .


Storefront Client SDK ()

is a zero-dependency client that tracks user events and manages visitor cookies automatically.

Initialization

Pass the query, attribution token from the search response, and optionally products / product details and the URI:

Tracking Product Views (PDP)

Tracking Cart Actions

Tracking Browsing Pages

Tracking Purchases (Conversion)


Real-Time Event Subscribers

  1. :
    • Queries order items, customer ID, email, total, currency, and metadata.
    • Prefers (or ) before falling back to or .
    • Maps line items to with quantity and writes the event to the Google Retail API.
    • Automatically resolves using configured module options if not registered in the root container.
  2. / / :
    • Ingests or removes product documents with automatic debouncing and variant handling.

Admin UI: Option Mapping

Navigate to Settings > AI Commerce Search () in the Medusa Admin dashboard to map options like:

  • ->
  • ->
  • ->
  • ->
  • ->
  • ->
  • Custom attributes ->

Limitations

  • is not directly supported by Google Cloud Retail API without branch recreation. Deleting individual documents cascades variant deletion safely.

License

MIT © Farid Ghar