Magento 2 et la segmentation client : stratégies de ciblage avancées
Magento 2 and Customer Segmentation: Advanced Targeting Strategies
If you're running a Magento 2 store, you already know that not all clients are the same. Some blignese but never buy, others make small purchases, and a few are your high-valeur VIPs. Wouldn't it be great if you could treat each group differently? That's where client segmentation comes in.
Dans cet article, nous'll dive deep into Magento 2's built-in segmentation tools and explore some advanced strategies to take your targeting to the next level. Whether you're new to segmentation or looking to refine your approche, we've got practical exemples and code snippets to help you implement these strategies effectively.
Why Customer Segmentation Matters
Customer segmentation is the process of dividing your client base into groups basé sur shared characteristics like:
- Demographics (age, gender, location)
- Purchase history (frequency, average commande valeur)
- Behavior (blignesing patterns, cart abandonment)
- Engagement (e-mail opens, loyalty program status)
When done right, segmentation vous permet de:
- Send personalized marketing messages
- Offer targeted promotions
- Create custom shopping experiences
- Improve client retention
Magento 2's Built-in Segmentation Tools
Magento 2 comes with a solid foundation for client segmentation. Voici comment to access it:
- Navigate to Customers > Segments
- Click Add Segment
- Define your segment conditions
Par exemple, to create a segment of clients who spent over $500 in the last 30 days:
1. Name: "High Spenders - Last 30 Days"
2. Conditions:
- Lifetime Sales Amount > 500
- Last Order Date is within last 30 days
3. Save and Apply
Advanced Segmentation with Custom Attributes
Tandis que the built-in tools are useful, you'll often need to go beyond basic demographics and purchase history. Voici comment to create custom attribut clients for more sophisticated segmentation:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/attribute.xsd">
<attribute code="customer_lifestyle" translate="label" module="Magento_Customer">
<label>Lifestyle</label>
<sort_order>100</sort_order>
<visible>true</visible>
<system>false</system>
<forms>
<form>customer_account_create</form>
<form>customer_account_edit</form>
<form>adminhtml_customer</form>
</forms>
<backend>Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend</backend>
<source>Vendor\Module\Model\Customer\Attribute\Source\Lifestyle</source>
<type>varchar</type>
<input>select</input>
<required>false</required>
</attribute>
</config>
Après creating the attribute, you can use it in your segmentation rules just like any built-in attribute.
Behavioral Segmentation with Event Tracking
To create segments basé sur client behavior, you'll need to track specific events. Voici comment to implement a basic event tracker:
require(['jquery', 'mage/cookies'], function($) {
$(document).on('click', '.product-item-link', function() {
var productId = $(this).closest('.product-item').data('product-id');
var currentProducts = $.mage.cookies.get('recently_viewed');
currentProducts = currentProducts ? JSON.parse(currentProducts) : [];
if (currentProducts.indexOf(productId) === -1) {
currentProducts.push(productId);
$.mage.cookies.set('recently_viewed', JSON.stringify(currentProducts), {path: '/'});
}
});
});
Vous pouvez then create segments basé sur these tracked behaviors, like "Viewed Product X but Didn't Purchase."
Dynamic Pricing Strategies
One powerful application of segmentation is dynamic tarification. Here's a basic exemple of comment implement tiered tarification basé sur client segments:
<?php
namespace Vendor\Module\Plugin;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Catalog\Model\Product;
class TieredPricing
{
protected $customerSession;
public function __construct(CustomerSession $customerSession)
{
$this->customerSession = $customerSession;
}
public function afterGetPrice(Product $product, $price)
{
if ($this->customerSession->isLoggedIn()) {
$customer = $this->customerSession->getCustomer();
// Check if customer is in VIP segment
if ($customer->getData('is_vip')) {
return $price * 0.9; // 10% discount for VIPs
}
}
return $price;
}
}
Personalized Email Campaigns
Segmentation shines when combined with e-mail marketing. Voici comment to send targeted e-mails basé sur segments:
<?php
namespace Vendor\Module\Cron;
use Magento\CustomerSegment\Model\ResourceModel\Segment\CollectionFactory as SegmentCollectionFactory;
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory as SubscriberCollectionFactory;
use Magento\Newsletter\Model\QueueFactory;
use Magento\Store\Model\StoreManagerInterface;
class SendSegmentEmails
{
protected $segmentCollectionFactory;
protected $subscriberCollectionFactory;
protected $queueFactory;
protected $storeManager;
public function __construct(
SegmentCollectionFactory $segmentCollectionFactory,
SubscriberCollectionFactory $subscriberCollectionFactory,
QueueFactory $queueFactory,
StoreManagerInterface $storeManager
) {
$this->segmentCollectionFactory = $segmentCollectionFactory;
$this->subscriberCollectionFactory = $subscriberCollectionFactory;
$this->queueFactory = $queueFactory;
$this->storeManager = $storeManager;
}
public function execute()
{
$segments = $this->segmentCollectionFactory->create()
->addFieldToFilter('is_active', 1);
foreach ($segments as $segment) {
$subscribers = $this->subscriberCollectionFactory->create()
->addFieldToFilter('segment_id', $segment->getId());
if ($subscribers->count() > 0) {
$queue = $this->queueFactory->create();
$queue->setTemplateId('segment_specific_template')
->setSubscribers($subscribers)
->setStoreId($this->storeManager->getStore()->getId())
->save();
}
}
}
}
Integnote with Third-Party Tools
Tandis que Magento's built-in tools are powerful, you might want to integrate with specialized client data platforms (CDPs) or marketing automation tools. Here's a basic API integration exemple:
<?php
namespace Vendor\Module\Model;
use Magento\Customer\Model\CustomerFactory;
use Magento\CustomerSegment\Model\SegmentFactory;
use GuzzleHttp\Client;
class CdpIntegration
{
protected $customerFactory;
protected $segmentFactory;
protected $client;
public function __construct(
CustomerFactory $customerFactory,
SegmentFactory $segmentFactory
) {
$this->customerFactory = $customerFactory;
$this->segmentFactory = $segmentFactory;
$this->client = new Client(['base_uri' => 'https://api.yourcdp.com/v1/']);
}
public function syncSegment($segmentId)
{
$segment = $this->segmentFactory->create()->load($segmentId);
$customers = $this->customerFactory->create()
->getCollection()
->addFieldToFilter('segment_id', $segmentId);
$customerData = [];
foreach ($customers as $customer) {
$customerData[] = [
'email' => $customer->getEmail(),
'segment' => $segment->getName(),
'attributes' => $customer->getData()
];
}
$this->client->post('segments/update', [
'json' => [
'segment_id' => $segmentId,
'customers' => $customerData
]
]);
}
}
Measuring Segmentation Success
To ensure your segmentation strategies are working, track these clé metrics:
- Segment-specific conversion rates: Are clients in your segments converting at higher rates?
- Average commande valeur by segment: Are your high-valeur segments spending more?
- Email engagement rates: Do segmented e-mails get better open and click rates?
- Customer lifetime valeur: Are segmented clients more valuable over time?
Voici comment to create a simple tableau de bord to track these metrics:
<?php
namespace Vendor\Module\Block\Adminhtml\Dashboard;
use Magento\Backend\Block\Template;
use Magento\Reports\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
class SegmentPerformance extends Template
{
protected $customerCollectionFactory;
protected $orderCollectionFactory;
public function __construct(
Template\Context $context,
CustomerCollectionFactory $customerCollectionFactory,
OrderCollectionFactory $orderCollectionFactory,
array $data = []
) {
parent::__construct($context, $data);
$this->customerCollectionFactory = $customerCollectionFactory;
$this->orderCollectionFactory = $orderCollectionFactory;
}
public function getSegmentPerformance($segmentId)
{
$customers = $this->customerCollectionFactory->create()
->addFieldToFilter('segment_id', $segmentId);
$orders = $this->orderCollectionFactory->create()
->addFieldToFilter('customer_id', ['in' => $customers->getAllIds()]);
return [
'customer_count' => $customers->count(),
'order_count' => $orders->count(),
'average_order_value' => $orders->count() > 0 ? $orders->getTotals('grand_total') / $orders->count() : 0,
'conversion_rate' => $customers->count() > 0 ? ($orders->count() / $customers->count()) * 100 : 0
];
}
}
Bonnes pratiques for Effective Segmentation
To get the most out of your segmentation efforts:
- Start simple: Begin with basic segments like new vs. returning clients before moving to more complex groupings.
- Use mulconseille criteria: Combine demographic, behavioral, and transactional data for richer segments.
- Keep segments updated: Customer behavior changes, so regularly refresh your segments.
- Test and optimize: Try different segment definitions and measure what works best.
- Respect privacy: Be transparent about data collection and use, especially with GDPR and other regulations.
Conclusion
Customer segmentation in Magento 2 is a powerful tool that goes far beyond simple demographic groupings. By leveraging both built-in fonctionnalités and custom implémentations, you can create highly targeted experiences that drive engagement, increase conversions, and boost client loyalty.
The exemples in this post should give you a solid foundation to start implementing more advanced segmentation strategies in your Magento 2 store. Remember that the most effective segmentation is an ongoing process of test, measuring, and refining basé sur real client behavior and entreprise results.
Have you implemented any interesting segmentation strategies in your Magento store? Share your experiences in the comments!