# 📊 Factorize Analytics

> Who looked, who carted, who bought — per product, per week.
>
> **Suite identity:** icon 📊 · accent `#5ecb70` · part of the [Factorize suite](../README-FACTORIZE.md)

## What it does

Tracks per-product view and add-to-cart counters as post meta on each product — a lifetime total plus a rolling 7-day figure that resets itself by ISO week, with no cron job and no external analytics service. It prints a social-proof line on the single product page, adds sortable Views / Carts / Sold columns to the Products admin list, and provides the "Store Stats" admin page under the shared Factorize menu: a drill-down dashboard from site totals to per-category to per-product rows (including each product's popularity score and stock status).

## Configuration

There are no tunables yet. The `config.php` scaffold exists so future settings can be added without changing call sites.

| key | default | effect |
| --- | ------- | ------ |
| *(none)* | — | — |

`empire_analytics_config( $key )` returns the merged configuration: an empty defaults array, overlaid with the `factorize-analytics_options` option (an array, settable via `update_option`; the legacy `empire-product-analytics_options` option is still read as a fallback), then passed through the `factorize-analytics_config` filter for code-level overrides. Passing no key returns the whole array.

## Works with

- **WooCommerce** — required for anything useful: view counting only runs on `is_product()` pages, cart counting hooks `woocommerce_add_to_cart`, lifetime sales come from `wc_get_product()->get_total_sales()`, and the Store Stats page needs the `product` post type / `product_cat` taxonomy. Calls to `is_product`, `wc_get_product` are guarded with `function_exists`, so nothing fatals without WooCommerce.
- **factorize-catalog** (sibling plugin) — provides `empire_tools_weekly_sales()`, used for the weekly "Sold" figures. Every call is wrapped in `function_exists()`; without that plugin, weekly sold counts simply read 0.
- Reads the `et_popularity` post meta directly for the Store Stats "Popularity" column (written by the sibling catalog plugin); a missing value renders as an em dash.
- The theme keeps all CSS, including the `.et-stats` class styling for the storefront stats line.

## Hooks it registers

- `template_redirect` — counts a product page view (skips admins, users with `edit_products`, and bots).
- `woocommerce_add_to_cart` (priority 10, 3 args) — counts add-to-carts by quantity.
- `woocommerce_single_product_summary` (priority 12) — prints the social-proof stats line under the price.
- `manage_edit-product_columns` / `manage_product_posts_custom_column` / `manage_edit-product_sortable_columns` — Views / Carts / Sold columns in the Products list.
- `pre_get_posts` — orders the Products list by lifetime view/cart totals when those columns are sorted.
- `admin_menu` — registers the "Store Stats" page (`et-store-stats`, capability `manage_woocommerce`) as a submenu of the Factorize hub.
- `admin_init` — registers the plugin's card (Insights category) with the shared Factorize hub, which any Factorize plugin alone stands up.
- `current_screen` — strips all admin notices on the Store Stats screen.

## Publishing

`readme.txt` (WordPress.org listing format) and `QUICKSTART.md` (5-minute setup guide) live alongside this file for the WordPress.org release.
