Dokumentaatio
Selaa dokumentaatiota

Add Privatus Analytics to WooCommerce

Add cookieless analytics to a WooCommerce store, then track each purchase with its total and currency from the thank you page without order details.

Näytä Markdown-muodossa

WooCommerce runs on WordPress, so you add the snippet the WordPress way. WordPress lists the options, including a child theme hook and the WordPress plugin.

html
<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID" data-modules="engage,auto,vitals,clicks,ask"></script>

The WordPress plugin tracks WooCommerce purchases for you. It is free in the WordPress plugin directory. See WordPress for how to install it.

Purchases without the plugin#

WooCommerce runs the woocommerce_thankyou action on the order received page and passes it the order id. In a child theme's functions.php, use the order only to read its total and currency:

php
add_action( 'woocommerce_thankyou', function ( $order_id ) {
    $order = wc_get_order( $order_id );
    if ( ! $order || $order->get_meta( '_privatus_tracked' ) ) {
        return; // count each order once, even if the page reloads
    }
    $order->update_meta_data( '_privatus_tracked', '1' );
    $order->save();
    printf(
        '<script>window.addEventListener("load",function(){privatus.track("purchase",{revenue:%s,currency:%s});});</script>',
        wp_json_encode( (float) $order->get_total() ),
        wp_json_encode( $order->get_currency() )
    );
} );

revenue and currency are read as the event's revenue. See Revenue. Never send the order id, names or email addresses as properties.

Caching plugins#

Exclude pa.js from "combine", "defer JS" and "delay JS" features. See Caching plugins.