ShopBase
Elton lấy dữ liệu cửa hàng ShopBase qua ShopBase Admin REST API: đơn hàng, khách hàng và sản phẩm. Elton xác thực bằng API key và password của một private app trên ShopBase, và chỉ gửi yêu cầu đọc (GET), không tạo hay sửa dữ liệu trên ShopBase.
1. Lấy API key và password trên ShopBase
API key và password là của một private app tạo trong trang quản trị ShopBase:
- Đăng nhập trang quản trị (admin) của cửa hàng ShopBase.
- Vào Apps.
- Bấm Manage private apps (gần cuối trang).
- Bấm Create a new private app.
- Ở mục App details, nhập tên app (ví dụ
Elton Data) và email liên hệ. - Ở mục Admin API, cho app quyền truy cập đơn hàng (Orders), khách hàng (Customers) và sản phẩm (Products). Elton chỉ đọc, nên quyền đọc là đủ.
- Lưu app. Mục Admin API hiện API key và Password của app. Sao chép cả hai.
Ghi lại địa chỉ cửa hàng, dạng https://<tên shop>.onshopbase.com, ví dụ https://eltondata.onshopbase.com.
Hướng dẫn của ShopBase: Private apps. Ai có API key và password đều đọc được dữ liệu cửa hàng qua API, nên hãy giữ chúng như mật khẩu. Xoá private app trên ShopBase thì Elton không lấy được dữ liệu nữa.
2. Tạo data source
- Vào Data Sources → Create data source, ở ô Type chọn ShopBase.
- Điền form:
| Trường | Giá trị |
|---|---|
| API key | API key của private app ở bước 1 |
| Password | Password của private app ở bước 1 |
| Base Url | Địa chỉ cửa hàng, ví dụ https://eltondata.onshopbase.com. Nhập kèm https://, không có / ở cuối |
- Bấm Create data source.
Khi sửa data source, ô Password hiện dạng **********. Để nguyên thì Elton dùng password đã lưu.
Tên bảng ở destination lấy theo Base Url (xem mục 4). Đổi Base Url của một source đã chạy thì các lần chạy sau ghi vào bảng mới; dữ liệu đã lấy vẫn nằm ở bảng cũ.
3. Tạo connection
Khi tạo connection với source là ShopBase:
- Select 1 or many type of data: chọn một hoặc nhiều loại Order, Customer, Product. Mỗi loại là một bảng ở destination.
- First run loads from (bắt buộc): ngày bắt đầu lấy đơn hàng ở lần chạy đầu tiên. Elton lấy đơn có
updated_attừ ngày này. Customer và Product không theo ngày này: lần chạy đầu luôn lấy toàn bộ. - Later runs rescan: các lần chạy sau, Elton lấy bản ghi có
updated_attừ ngày của lần chạy thành công gần nhất, lùi lại số ngày này (1, 3, 7, 30, 60 hoặc 90 ngày; để trống là 1 ngày). Áp dụng cho cả ba loại. Nhờ vậy đơn đổi trạng thái (thanh toán, giao hàng, huỷ, hoàn tiền…), khách hàng hay sản phẩm vừa sửa vẫn được cập nhật.
Khi reload connection, Customer và Product cũng lấy lại toàn bộ; Order lấy lại từ First run loads from.
Bản ghi được cập nhật theo khoá của bảng (id): lấy lại cùng một bản ghi thì dòng cũ được thay, không bị nhân đôi.
4. ShopBase: dữ liệu được đồng bộ
Bảng có tên dạng shopbase_<loại>_<tên shop>, với tên shop là phần trước .onshopbase.com trong Base Url. Ví dụ Base Url https://eltondata.onshopbase.com cho bảng shopbase_order_eltondata. Nếu Base Url là tên miền riêng (không có .onshopbase.com), tên shop là tên miền với dấu . đổi thành _, ví dụ https://shop.example.com cho shopbase_order_shop_example_com.
| Loại dữ liệu | Bảng | Khoá | Cách lấy | Phân vùng (partition) theo ngày |
|---|---|---|---|---|
| Order | shopbase_order_<tên shop> | id | Đơn có updated_at từ First run loads from, sau đó theo Later runs rescan | created_at |
| Customer | shopbase_customer_<tên shop> | id | Toàn bộ ở lần chạy đầu, sau đó khách hàng có updated_at theo Later runs rescan | |
| Product | shopbase_product_<tên shop> | id | Toàn bộ ở lần chạy đầu, sau đó sản phẩm có updated_at theo Later runs rescan |
Cách lưu dữ liệu chung cho cả ba bảng:
- Cột dạng object (ví dụ
customer,shipping_address,default_address) được lưu dạng chuỗi JSON. - Cột dạng danh sách (ví dụ
line_items,variants,addresses) được lưu dạng mảng chuỗi (trên BigQuery làARRAY<STRING>), mỗi phần tử là một object JSON. DùngUNNESTđể tách từng phần tử, vàJSON_VALUEđể đọc trường bên trong (xem mục 5). created_at,updated_atvà các cột thời điểm dạng chuỗi ngày giờ là giờ UTC.- Cột
elton_created_at(thời điểm Elton lấy bản ghi) chỉ được ghi vào BigQuery khi connection dùng kiểu ghi Append. Với Merge (cập nhật theo khoá) và Overwrite, bảng ở BigQuery không có cột này.
Order
Bảng shopbase_order_<tên shop>: mỗi dòng là một đơn hàng.
| Cột | Ý nghĩa |
|---|---|
id | Mã đơn (số), dùng để nối bảng |
name, order_number | Tên đơn hiển thị trong ShopBase admin, và số thứ tự đơn |
created_at, updated_at, processed_at, closed_at, cancelled_at | Thời điểm tạo, cập nhật, xử lý, đóng, huỷ đơn |
created_at_in_timezone | Thời điểm tạo theo múi giờ cửa hàng. Trên BigQuery cột này là TIMESTAMP nên được quy về UTC, giá trị trùng với created_at |
paid_at, invoice_sent_at | Thời điểm thanh toán, gửi hoá đơn, dạng Unix timestamp (số giây). Có thể là 0 |
financial_status | Trạng thái thanh toán, ví dụ paid |
fulfillment_status, shipment_status | Trạng thái xử lý giao hàng, trạng thái vận chuyển |
cancel_reason | Lý do huỷ |
currency | Tiền tệ của đơn |
subtotal_price, total_line_items_price, total_discounts, total_line_items_discount, total_tax, total_tipping, total_price | Tạm tính, tổng tiền hàng, tổng giảm giá, giảm giá theo sản phẩm, thuế, tiền tip, tổng tiền đơn |
total_quantity, total_weight, total_weight_in_gram | Tổng số lượng sản phẩm, tổng cân nặng |
email, phone | Email, số điện thoại người mua |
customer | Khách hàng (chuỗi JSON): id, email, first_name, last_name, phone, orders_count, total_spent… |
billing_address, shipping_address | Địa chỉ thanh toán, địa chỉ giao hàng (chuỗi JSON) |
line_items | Sản phẩm trong đơn (mảng JSON): id, product_id, variant_id, sku, title, variant_title, quantity, price, total_discount, vendor, fulfillment_status… |
shipping_lines | Phương thức và phí vận chuyển (mảng JSON): title, code, price, discounted_price… |
discount_code, discount_applications | Mã giảm giá và các khuyến mãi áp dụng (mảng JSON) |
refunds, fulfillments | Các lần hoàn tiền, các lần giao hàng (mảng JSON) |
payment_gateway, payment_gateway_names, transaction_id | Cổng thanh toán và mã giao dịch |
is_test_order | Đơn thử nghiệm. Lọc is_test_order = false khi tính doanh thu |
tags, note | Thẻ và ghi chú của đơn |
shop_id | Mã cửa hàng ShopBase |
Tất cả cột của shopbase_order_<tên shop>: id, created_at, updated_at, access_key, account_id, additional_information, allow_refund, applied_discount, approve_status, billing_address, billing_shipping_same_address, buyer_accepts_marketing, cancel_reason, cancelled_at, cart_token, chargeback_amount, chargeback_deadline_at, chargeback_status, checkout_token, client_country_code, client_details, client_ip, client_tracking_location, closed_at, created_at_in_timezone, currency, customer, customer_locale, discount_applications, discount_code, draft_order_number, email, financial_status, fulfillment_status, fulfillments, global_market, hold_amount, invoice_sent_at, invoice_url, is_create_from_checkout, is_in_post_purchase, is_risk, is_test_order, landing_url, line_items, name, note, note_attributes, order_number, order_status_url, order_type, paid_at, payment_gateway, payment_gateway_names, payment_provider_payload, pbase_approve_status, pbase_design_status, phone, plb_profit, previous_shipping_fee, print_file_status, processed_at, processing_method, product_mappings, referring_site, refunds, shipment_status, shipping_address, shipping_lines, shop_id, source_name, subtotal_price, tags, tax_lines, taxes_included, token, total_discounts, total_line_items_discount, total_line_items_price, total_price, total_quantity, total_tax, total_tipping, total_weight, total_weight_in_gram, transaction_id, use_customer_default_address, elton_created_at
Các trường của mỗi sản phẩm trong line_items: applied_discount, base_product_id, can_upload_artwork, custom, discount_allocations, discount_amount, fulfillable_quantity, fulfillment_id, fulfillment_service, fulfillment_status, gift_card, holding_time, id, image_src, is_post_purchase_item, line_item_discount_amount, line_item_discount_price, line_item_price, line_item_price_after_discount, line_item_price_before_discount, line_item_tags, line_item_weight, metadata, name, plus_base_product_group, price, print_file, processing_fee, processing_fee_rate, product_id, product_type, profit, properties, quantity, raw_price, raw_weight, requires_shipping, shipping_rate, sku, source, source_id, tax_amount, tax_lines, tax_rate, taxable, title, total_discount, variant_id, variant_options, variant_title, vendor, weight, weight_unit. Các trường này do ShopBase trả về và được giữ nguyên trong chuỗi JSON, nên có thể khác theo cửa hàng.
Customer
Bảng shopbase_customer_<tên shop>: mỗi dòng là một khách hàng.
| Cột | Ý nghĩa |
|---|---|
id | Mã khách hàng, dùng để nối bảng |
email, first_name, last_name, phone, calling_code | Thông tin liên hệ |
created_at, updated_at | Thời điểm tạo, cập nhật |
orders_count, total_spent, average_spent, currency | Số đơn, tổng chi tiêu, chi tiêu trung bình mỗi đơn, tiền tệ |
first_order_id, last_order_id, last_order_name | Mã đơn đầu tiên, mã và tên đơn gần nhất |
last_order_at | Thời điểm đơn gần nhất, dạng Unix timestamp (số giây) |
accepts_marketing, buyer_accepts_marketing | Đồng ý nhận marketing (buyer_accepts_marketing dạng số 0/1) |
state | Trạng thái tài khoản khách hàng, ví dụ disabled |
default_address, addresses | Địa chỉ mặc định (chuỗi JSON) và mọi địa chỉ (mảng JSON) |
tags, note | Thẻ và ghi chú |
verified_email, tax_exempt | Email đã xác minh, được miễn thuế |
shop_id | Mã cửa hàng ShopBase |
activation_token_expired và last_sent_abandoned_email_at cũng là Unix timestamp (số giây).
Tất cả cột của shopbase_customer_<tên shop>: id, created_at, updated_at, accepts_marketing, activation_token, activation_token_expired, addresses, avatar, average_spent, buyer_accepts_marketing, calling_code, currency, default_address, email, first_name, first_order_id, last_name, last_order_at, last_order_id, last_order_name, last_sent_abandoned_email_at, note, orders_count, phone, platform, shop_id, state, tags, tax_exempt, total_spent, verified_email, elton_created_at
Product
Bảng shopbase_product_<tên shop>: mỗi dòng là một sản phẩm. Phân loại (biến thể: màu, size…) của sản phẩm nằm trong cột variants.
| Cột | Ý nghĩa |
|---|---|
id | Mã sản phẩm, dùng để nối bảng |
title, handle, vendor, product_type, tags | Tên, đường dẫn (handle), nhà cung cấp, loại sản phẩm, thẻ |
created_at, updated_at | Thời điểm tạo, cập nhật |
published, published_at, published_scope | Đã đăng bán chưa, thời điểm đăng, phạm vi đăng |
variants | Các phân loại (mảng JSON): id, product_id, sku, title, option1, option2, option3, price, compare_at_price, cost_per_item, inventory_quantity, weight… |
options, option_sets, custom_options | Các thuộc tính phân loại và tuỳ chọn (mảng JSON) |
image, images | Ảnh chính (chuỗi JSON) và mọi ảnh (mảng JSON) |
collection_ids | Các bộ sưu tập chứa sản phẩm |
product_source | Nguồn sản phẩm, ví dụ ali_dropship_connector |
Tất cả cột của shopbase_product_<tên shop>: id, title, vendor, created_at, updated_at, collection_ids, custom_options, display_options, fulfillment_services, handle, image, images, metafields_global_description_tag, metafields_global_title_tag, option_sets, options, product_availability, product_source, product_type, published, published_at, published_scope, reference_keys, tags, template, template_suffix, variants, elton_created_at
5. Quan hệ giữa các bảng
Các bảng ShopBase nối với nhau qua mã đơn, mã khách hàng, mã sản phẩm và mã phân loại. Hậu tố <tên shop> ở các bảng cần nối phải là của cùng một cửa hàng.
| Bảng | Nối với bảng | Điều kiện nối | Quan hệ |
|---|---|---|---|
shopbase_order_<tên shop> | shopbase_customer_<tên shop> | JSON_VALUE(customer, '$.id') = CAST(id AS STRING) (đọc id trong chuỗi JSON customer) | nhiều đơn – 1 khách hàng |
shopbase_order_<tên shop> | shopbase_product_<tên shop> | line_items[].product_id = id (tách mảng line_items) | nhiều sản phẩm trong 1 đơn |
shopbase_order_<tên shop> | shopbase_product_<tên shop> (cột variants) | line_items[].variant_id = variants[].id (tách cả hai mảng) | 1 dòng sản phẩm trong đơn – 1 phân loại |
shopbase_customer_<tên shop> | shopbase_order_<tên shop> | last_order_id = id, hoặc first_order_id = id | 1 khách hàng – đơn gần nhất / đơn đầu tiên |
Lưu ý khi nối:
idcủa cả ba bảng là số (INTEGER). Giá trị đọc từ chuỗi JSON bằngJSON_VALUElà chuỗi, nên cầnCASTkhi so sánh.- Mã sản phẩm có tên khác nhau:
idởshopbase_product_<tên shop>,product_idtrongline_itemscủashopbase_order_<tên shop>. - Ví dụ trên BigQuery, số lượng bán và doanh thu theo sản phẩm, bỏ đơn thử nghiệm và đơn đã huỷ:
SELECT p.id AS product_id,
p.title,
SUM(CAST(JSON_VALUE(li, '$.quantity') AS INT64)) AS quantity,
SUM(CAST(JSON_VALUE(li, '$.price') AS FLOAT64)
* CAST(JSON_VALUE(li, '$.quantity') AS INT64)) AS revenue
FROM `dataset.shopbase_order_eltondata` o,
UNNEST(o.line_items) AS li
JOIN `dataset.shopbase_product_eltondata` p
ON p.id = CAST(JSON_VALUE(li, '$.product_id') AS INT64)
WHERE o.is_test_order = false
AND o.cancelled_at IS NULL
GROUP BY product_id, p.title
ORDER BY revenue DESC;
6. Lỗi thường gặp
Khi ShopBase trả lỗi, Elton báo lỗi dạng <mã HTTP>: <nội dung ShopBase trả về>.
| Lỗi | Nguyên nhân và cách xử lý |
|---|---|
Lỗi bắt đầu bằng 401: | API key hoặc Password sai, hoặc private app đã bị xoá trên ShopBase. Kiểm tra lại ở Apps → Manage private apps (bước 1), rồi sửa data source. |
Lỗi bắt đầu bằng 403: | Private app chưa có quyền với loại dữ liệu đang lấy (đơn hàng, khách hàng hoặc sản phẩm). Thêm quyền ở mục Admin API của private app. |
Lỗi bắt đầu bằng 404: | Thường do Base Url sai. Nhập đúng dạng https://<tên shop>.onshopbase.com, không có đường dẫn phía sau. |
Lỗi bắt đầu bằng 429: | ShopBase giới hạn số lần gọi API. Elton đã đợi 120 giây và thử lại nhiều lần. Chạy lại connection sau, hoặc giảm tần suất chạy. |
'NoneType' object has no attribute 'startswith' | Data source chưa có Base Url. Sửa data source và điền Base Url. |
| Bảng mới xuất hiện, dữ liệu mới không vào bảng cũ | Base Url của data source đã đổi, nên tên bảng đổi theo (xem mục 4). |
| Customer hoặc Product chỉ có ít dòng ở lần chạy sau | Bình thường: các lần chạy sau chỉ lấy bản ghi vừa thay đổi; bản ghi cũ vẫn nằm trong bảng. |
| Thiếu đơn cũ | Lần chạy đầu chỉ lấy đơn có updated_at từ First run loads from. Sửa ngày này rồi reload connection để lấy thêm. |
Cách tính Billable Pipeline
Cách tính phí theo số pipeline của từng loại source xem ở đây.