Retrieving Product Views in Magento: A Comprehensive Guide
Tracking product views is an essential metric for understanding customer behavior and optimizing your e-commerce store’s performance. In this blog post, we’ll guide you through the process of retrieving the number of views for a specific product in Magento.
Understanding Product View Tracking
Magento 2 provides built-in functionality to track product views. When a visitor browses a product page, Magento automatically logs the view by incrementing the viewed_product_id count in the catalog_product_entity_int table.
To access this data, you can query the database directly or use the Magento API.
Retrieving Product Views via Database Query
You can retrieve product views using a simple SQL query. Here’s an example:
SELECT entity_id, viewed_product_id FROM catalog_product_entity_int WHERE attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'viewed_product_id');
This query retrieves the entity_id (the product ID) and the viewed_product_id (the count of views) from the catalog_product_entity_int table for all products.
To retrieve data for a specific product, you can add a WHERE clause to filter results by entity_id:
SELECT entity_id, viewed_product_id FROM catalog_product_entity_int WHERE attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'viewed_product_id') AND entity_id = YOUR_PRODUCT_ID;
Replace YOUR_PRODUCT_ID with the ID of the product you want to track.
Retrieving Product Views via Magento API
For a more structured approach, you can use Magento’s REST API to retrieve product views. Here are the steps:
- Enable the REST API in your Magento store configuration.
- Create an admin user and generate an access token with appropriate permissions.
- Use cURL or a HTTP client library to send a GET request to the following endpoint:
GET https://your-store.com/rest/V1/products/{product_id}
Replace https://your-store.com with your actual Magento store URL, and {product_id} with the ID of the product you want to track.
- In the response JSON, look for the
viewed_product_idfield under thecustom_attributesarray:
{
"id": 3,
"sku": "24-MB01",
"name": "Misty Morning",
"attribute_set_code": "Default",
"price": 78.5,
"status": 1,
"tax_class_id": 0,
"visibility": 4,
"type_id": "virtual",
"created_at": "2016-03-31 12:09:25",
"updated_at": "2018-04-27 07:03:33",
"viewed_product_id": 10
}
The viewed_product_id field displays the number of times this product has been viewed.
Best Practices and Optimizations
While Magento’s built-in view tracking is efficient for small to medium-sized stores, it can become resource-intensive as your product catalog grows. For larger enterprises, consider implementing a custom solution using an indexer or a third-party extension to optimize performance.
Additionally, ensure that your data collection strategy aligns with privacy regulations and best practices, such as obtaining user consent before tracking their behavior.
Conclusion
Retrieving product views in Magento is a straightforward process once you understand the underlying database structure. By leveraging either direct SQL queries or the Magento REST API, you can easily obtain valuable insights into customer interest levels for specific products.
Remember to keep your data organized and secure, and always follow best practices when implementing tracking solutions on your e-commerce platform. For more advanced customizations and optimizations, consider exploring our professional services at Laravel Company.
Happy tracking!