Revenue Tracking

Scraping the Purchase Confirmation Page for Revenue Tracking

THIS ARTICLE WILL HELP YOU:

Issue Description

There are certain cases in which is not possible to track revenue through the other methods Convert provides. This may be due to, that they do not use GA, they use a custom installation of GA Ecommerce or it is not possible to refer to the revenue amount from javascript variables on the page.

Solution

So, in these cases, you could set up Convert to scrape the purchase confirmation page for the revenue after it has been rendered and then send it to Convert.

To do this you need to install some javascript code in Project Configuration > Global Javascript.

Also, you will need to go through the purchase confirmation and find the following items:

  • Purchase Confirmation URL
  • CSS Selector for the purchase revenue amount.
  • CSS Selector for the amount of the products.

To find the CSS selectors you can look at the following article on how to find a selector of certain content.

Once you got the selectors, you can fill them in the variables of the script.

// Configuración
const CONFIG = {
    purchaseConfirmURL: 'https://www.domain.com/checkout/onepage/success/',
    revenueSelector: 'tr.subtotal > td.amount > .price',
    productCountSelector: 'ul.items-qty > li.item > .content',
    goalId: '10000001'
};

// Función principal
function initializeTracking() {
    // Verifica si estamos en la página de confirmación de compra
    if (!location.href.includes(CONFIG.purchaseConfirmURL)) {
        console.log('No estamos en la página de confirmación de compra');
        return;
    }

    // Verifica si Convert y jQuery están disponibles
    if (typeof convert === 'undefined' || typeof convert.$ === 'undefined') {
        console.error('Convert o jQuery no están disponibles');
        return;
    }

    try {
        // Obtiene los datos
        const revenue = extractRevenue();
        const productsCount = extractProductCount();

        // Registra los datos para debugging
        console.log('Datos reportados a Convert:');
        console.log('Revenue:', revenue);
        console.log('Products Count:', productsCount);

        // Envía los datos a Convert
        window._conv_q = window._conv_q || [];
        window._conv_q.push(["pushRevenue", revenue, productsCount, CONFIG.goalId]);

    } catch (error) {
        console.error('Error al procesar los datos:', error);
    }
}

// Funciones auxiliares
function extractRevenue() {
    const revenueText = convert.$(CONFIG.revenueSelector).text();
    return revenueText.replace(/\$/g, '').trim();
}

function extractProductCount() {
    return convert.$(CONFIG.productCountSelector).text();
}

// Espera a que el documento esté listo
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initializeTracking);
} else {
    initializeTracking();
}