Integrating Usermaven with SureCart
This guide explains how to integrate Usermaven with your SureCart-powered WordPress site to track customer purchases and identify logged-in users.
Prerequisites:
- A Usermaven account.
- The Usermaven WordPress plugin installed and activated. (Usermaven WordPress Integration doc).
- SureCart plugin installed and active.
- Access to your WordPress theme’s
functions.php
file or a code snippets plugin (recommended). - For tracking across subdomains/domains: access to
wp-config.php
.
Step 1: Install the Usermaven main tracking script
First, ensure the Usermaven WordPress plugin is installed, activated, and configured with your Usermaven Workspace ID. This adds the main Usermaven tracking script to your site.
You can install the main tracking script without the Usermaven WordPress plugin by using another method (e.g., a code snippet plugin). But it is recommended to use the plugin for ease of use and to avoid any potential issues with theme updates.
Learn more about the main tracking script here: Installing Usermaven.
Step 2: Track SureCart purchase events
To track when customers make a purchase via SureCart, add the following PHP code to your active WordPress theme’s functions.php
file or using a code snippets plugin. This script listens for SureCart’s checkout completion and sends a purchase
event to Usermaven.
add_action('wp_footer', function() {
if (function_exists('SureCart')) { // Basic check if SureCart is active
?>
<script>
document.addEventListener('scCheckoutCompleted', function(e) {
const checkout = e.detail;
if (checkout && checkout.amount_due) {
usermaven('track', 'purchase', {
currency: checkout.currency,
value: checkout.amount_due / 100, // SureCart amount is in cents
total: checkout.total,
email: checkout.email,
name: checkout.name
});
}
});
</script>
<?php
}
});
For more detailed product tracking: You should inspect the e.detail
object from the scCheckoutCompleted
event, specifically within the checkout.line_items
array (if it exists in your SureCart version and setup), to find product-specific details like product ID and include them in your usermaven('track', 'purchase', {...})
call.
Step 3: Track logged-in users (Excluding admins)
This setup identifies users when they log into your WordPress site (e.g., a member portal) and sends their details to Usermaven. It does not track WordPress administrators.
A. Configure COOKIE_DOMAIN
(For cross-subdomain tracking)
If your main site (e.g., www.yourdomain.com
) and member portal (e.g., portal.yourdomain.com
) are on different subdomains of the same primary domain, enable cookie sharing:
- Open your
wp-config.php
file (in your WordPress root directory). - Before
/* That's all, stop editing! Happy publishing. */
, add (replaceyourdomain.com
with your actual domain):Example: Fordefine('COOKIE_DOMAIN', '.yourdomain.com');
example.com
andeducation.example.com
, use.example.com
. Apply this to all relevant WordPress installations.
B. Add user identification PHP snippets
Add this PHP code to your member portal site’s functions.php
(or a code snippets plugin). If your entire site is one WordPress install, add it there.
// On wp_login set session cookie + token
add_action('wp_login', function($login, $user){
$cookie_domain_defined = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '';
$token = wp_hash(time().$user->ID.uniqid('um_',true));
update_user_meta($user->ID,'um_last_login_token',$token);
setcookie('um_identify_after_login', $token, 0, COOKIEPATH, $cookie_domain_defined, is_ssl(), true);
},10,2);
// In wp_footer verify cookie once, fire identify, then clear
add_action('wp_footer', function(){
if (!is_user_logged_in() || current_user_can('manage_options') || empty($_COOKIE['um_identify_after_login'])) return;
$cookie_domain_defined = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '';
$user = wp_get_current_user();
$token = sanitize_text_field($_COOKIE['um_identify_after_login']);
$stored_token = get_user_meta($user->ID, 'um_last_login_token', true);
if (!$stored_token || $token !== $stored_token) {
setcookie('um_identify_after_login', '', time() - 3600, COOKIEPATH, $cookie_domain_defined, is_ssl(), true);
delete_user_meta($user->ID, 'um_last_login_token');
return;
}
setcookie('um_identify_after_login', '', time() - 3600, COOKIEPATH, $cookie_domain_defined, is_ssl(), true);
delete_user_meta($user->ID, 'um_last_login_token');
$first_name = get_user_meta($user->ID, 'first_name', true);
$last_name = get_user_meta($user->ID, 'last_name', true);
$created_at_timestamp = strtotime($user->user_registered);
$created_at_iso = $created_at_timestamp ? gmdate('c', $created_at_timestamp) : '';
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
if (typeof usermaven === 'function') {
try {
usermaven('id', {
id: '<?php echo esc_js($user->ID); ?>',
email: '<?php echo esc_js($user->user_email); ?>',
created_at: '<?php echo esc_js($created_at_iso); ?>',
first_name: '<?php echo esc_js($first_name); ?>',
last_name: '<?php echo esc_js($last_name); ?>',
custom: {
roles: <?php echo wp_json_encode(array_values($user->roles)); ?>
}
});
} catch (e) {
console.error('Usermaven identify error:', e);
}
}
});
</script>
<?php
});
// On wp_logout clear cookie
add_action('wp_logout', function(){
if (isset($_COOKIE['um_identify_after_login'])) {
$cookie_domain_defined = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '';
setcookie('um_identify_after_login', '', time() - 3600, COOKIEPATH, $cookie_domain_defined, is_ssl(), true);
}
});
This code sets a cookie on login, uses it to identify the user to Usermaven on the next page load, and then clears the cookie.
(Optional)Tracking across different domains
If your marketing site (e.g., www.siteA.com
) and member portal (e.g., app.siteB.org
) are on completely different primary domains, you need Usermaven’s cross-domain tracking.
-
Ensure
COOKIE_DOMAIN
(Step 3A) is set if any individual site uses subdomains (e.g.,portal.siteB.org
needsCOOKIE_DOMAIN
set to.siteB.org
in itswp-config.php
). -
Modify the Usermaven main tracking script on both domains by adding
data-cross-domain-linking
anddata-domains
attributes. If using the Usermaven WordPress plugin, you might need to check its settings for these options or manually add/modify the script in your theme.Example modification to the Usermaven script:
<script> // ... (Usermaven init code) ... o.setAttribute('data-cross-domain-linking', 'true'); // Add this o.setAttribute('data-domains', 'yourmarketingsite.com,yourportalsite.com'); // Add this, list your domains // ... (rest of Usermaven script) ... </script>
Replace with your actual domains.
-
Learn more: Cross-Domain Tracking in Usermaven.
Step 4: Caching considerations
To ensure tracking works correctly, exclude dynamic pages like your user portal, checkout, and dashboard areas from full-page or CDN caching, or ensure caching is bypassed for logged-in users.
Step 5: Test the integration
- Clear all caches (WordPress, server, CDN, browser).
- Test Purchase: In an incognito window, make a test SureCart purchase. Check for the
purchase
event in your Usermaven “Events” section (may take a few minutes). - Test User ID: Log out. Log in as a non-admin test user to your portal. Check Usermaven “Contacts Hub” to see if the user profile is created/updated.
Important Notes:
- Code Snippets Plugin: Recommended for adding PHP to avoid theme update issues (e.g., “Code Snippets” plugin).
- Child Theme: If editing
functions.php
directly, use a child theme. - Script Conflicts: Check your browser’s developer console for JavaScript errors if issues arise.
By following these steps, you’ll have a robust SureCart analytics and attribution setup with Usermaven, empowering you to make informed decisions and grow your online store. If you encounter any issues, contact our support team via email or in-app live chat.