How do we create Facebook AddToCart event tracking

How do we create facebook AddToCart event tracking? Have tried Facebook pixel conversion tracking
but the orderEvent is returned as null always.

Litium version: 7.6.1

Could you provide the code you have tested?

using Litium.Web;

using Litium.Web.Customers.TargetGroups;

using Litium.Web.Customers.TargetGroups.Events;

using Microsoft.Extensions.DependencyInjection;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Litium.Accelerator.Tracking

{

internal class TrackingServiceTargetGroupProcessorEventListener : ITargetGroupProcessor

{

    private readonly IServiceProvider _serviceProvider;

    public TrackingServiceTargetGroupProcessorEventListener(IServiceProvider serviceProvider)

    {

        _serviceProvider = serviceProvider;

    }

    public void Process(TargetGroupContext context, ITargetGroupEvent targetGroupEvent)

    {

        var orderEvent = targetGroupEvent as OrderEvent;

        if (orderEvent != null)

        {

            _serviceProvider.GetRequiredService<TrackingScriptEventService>().TrackEvent("facebook:order:created", orderEvent.Order.ID);

        }

    }

}

}

using Litium.Websites;

using Litium.Foundation.Modules.ECommerce;

using Litium.Web;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Litium.Accelerator.Tracking

{

public class FacebookTrackingScriptGenerator : TrackingScriptGenerator

{

    private readonly TrackingScriptEventService _trackingScriptEventService;

    private readonly ModuleECommerce _moduleECommerce;

    public FacebookTrackingScriptGenerator(TrackingScriptEventService trackingScriptEventService, ModuleECommerce moduleECommerce)

    {

        _trackingScriptEventService = trackingScriptEventService;

        _moduleECommerce = moduleECommerce;

    }

    public override string GetBodyEndScript(Page page, object data)

    {

        var script = new StringBuilder();

        if (_trackingScriptEventService.IsEventTracked("facebook:order:created", out var eventData)

            && eventData is Guid orderSystemId)

        {

            var order = _moduleECommerce.Orders.GetOrder(orderSystemId, _moduleECommerce.AdminToken);

            if (order != null)

            {

                script.Append("trackFacebookOrder(orderDataValues);"); // Change to the real implementation that with trigger facebook to see the event. 

            }

        }

        return script.ToString();

    }

    public override string GetBodyStartScript(Page page, object data)

    {

        return null;

    }

    public override string GetHeaderScript(Page page, object data)

    {

        return null;

    }

}

}

Its exactly the same from Facebook pixel conversion tracking

The example is for when an order is placed, so it’s using the OrderEvent. But in your case you want to catch when someone adds something to cart, and that’s a different event ItemAddedToCartEvent. Can you test with that instead?

https://docs.litium.com/api-reference/api?product=Litium&version=7.6&xref=Litium.Web.Customers.TargetGroups.Events

It seems to trigger the event correctly, but the script is not reflecting.

When product is added to cart, its added with rest api call to the api/cart/add. So there is no page being rendered, and thus no tag for facebook.

Maybe it is an idea to let that add to cart-call return the data to be sent to the pixel?

if (addtocart jsonreply contains trackingdata)
{
fbq(‘event’, trackingdata);
}

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.