Register

Install website tracking using the PHP tracking library

If are unable to install website tracking using our supported plugins, you have the option to use the PHP tracking library on your website. This enables website tracking of your visitors' actions which you can use as triggers for automation workflows. Use the following procedures to install different types of website tracking using the PHP tracking library after adding your website

A. Install website tracking

To install website tracking using the PHP tracking library, add a local, per-project dependency to your project using Composer:

composer require moosend/website-tracking

B. Create a tracker instance and initialize

You must create an instance of the tracker and perform proper initialization to enable sending of data to the server.  In the following code, the init phase deals with cookies that determine if the current user is a new visitor or a returned visitor.

<?php

$siteId = 'some-site-id';
$trackerFactory = new Moosend\TrackerFactory();
$tracker = $trackerFactory->create($siteId);

$tracker->init($siteId);

C. Identify Visitors

You can add an identify call to allow you to identify your visitors, connect them to their actions, and record their traits. The code in this call allows you to to identify a user via a unique email, and any optional traits you know about them like name. We recommend that you call identify after a user registers, after a user logs in, and if applicable, after a user provides their email as part of your newsletter subscription form. 

<?php

$tracker->identify('some@mail.com');
// identify with email and name
// commented out as you need to choose one of these calls only
// $tracker->identify('john@doe.com', 'John Doe');

// You can also check if a user is already identified
$tracker->isIdentified('john@doe.com'); // returns boolean


 D. Track page view events

If you also want to track page view events, you can use the track page view call on our platform's tracker library.  

<?php
$tracker->pageView('https://example.com');

E. Track item added to cart

One of the most recommended tracking in e-commerce is tracking when a user adds an item to their cart and when they complete their order. By tracking these events, you can implement Cart abandonment emails when a user adds an item to their cart.

<?php

// mandatory - a unique code for the product, like its SKU
$itemCode = 'COW-T-SHIRT';
// mandatory - the price of this product
$itemPrice = 12.02;
// mandatory - the url to get to the relevant product page
$itemUrl = 'http://your.store/product-101';
// mandatory
$itemQuantity = 2;
// mandatory - the total price for purchasing the given quantity of this product $itemTotalPrice = 24.04;
// mandatory - the name / title of this product
$itemName = 'Cow T-Shirt'; // mandatory
// mandatory - the image url of this product
$itemImage = 'http://your.store/product-color-blue.jpg';
// optional - the category of this product
$itemCategory = 'T-Shirts';
// optional - the manufacturer, brand name or company / owner of this product (if any) $itemManufacturer = 'Acme Co';

// you can add custom properties and later use them in segmentations or automations
// You can track things like the category or the manufacturer of the t-shirt in this case

$extraProps = array(
  "itemCategory" => $itemCategory,
  "itemManufacturer" => $itemManufacturer
);

// Tracking add to cart events with mandatory arguments
$tracker->addToOrder($itemCode, $itemPrice, $itemUrl, $itemQuantity);

// Tracking add to cart events with mandatory arguments + optional
$tracker->addToOrder($itemCode, $itemPrice, $itemUrl, $itemQuantity, $itemTotalPrice, $itemName, $itemImage, $extraProps);

F. Track order completed events

Another useful e-commerce tracking is to track when someone successfully completes an order. Using a tracking code, you can track this type of event to implement Purchase completed follow-up emails when an item is purchased. You must to add this tracking code on the Checkout completed or Thank you page. 

<?php

// mandatory - a unique code for the product, like its SKU
$itemCode = 'COW-T-SHIRT';
// mandatory - the price of this product
$itemPrice = 12.02;
// mandatory - the url to get to the relevant product page
$itemUrl = 'http://your.store/product-101';
// mandatory $itemQuantity = 2;
// mandatory - the total price for purchasing the given quantity of this product $itemTotalPrice = 24.04;
// mandatory - the name / title of this product
$itemName = 'Cow T-Shirt';
// mandatory - the image url of this product
$itemImage = 'http://your.store/product-color-blue.jpg',
// optional - the category of this product $itemCategory = 'T-Shirts';
// optional - the manufacturer, brand name or company / owner of this product (if any) $itemManufacturer = 'Acme Co';

// You can track things like the category or the manufacturer of the t-shirt in this case

$extraProps = array(

"itemCategory" => $itemCategory,
"itemManufacturer" => $itemManufacturer

);

//order completed
$order = $tracker->createOrder();

$order->addProduct($itemCode, $itemPrice, $itemUrl, $itemQuantity, $itemTotalPrice, $itemName, $itemImage, $extraProps);
//add as many products as you want before tracking and order completed event
$order->addProduct($itemCode, $itemPrice, $itemUrl, $itemQuantity, $itemTotalPrice, $itemName, $itemImage, $extraProps);

$tracker->orderCompleted($order);

  G. Track product view events

You can also track the event when a user views a product, before adding it to cart or purchasing.  By tracking these events, you can implement advanced re-targeting strategies like browse abandonment. You can then engage recipients when there is a user who viewed a product but did not purchase it.

<?php

$properties = array(
  array(
        "product"   =>     array(
             "itemCode" => "COW-T-SHIRT",
              
"itemPrice" => 12.02,
              "itemUrl" => "http://your.store/product-101",
              "itemQuantity" => 2, 
              "itemTotal" => 24.04,
              "itemImage" => "http://your.store/product-color-blue.jpg",
              "itemName" => "Cow T-Shirt",
              "itemDescription" => "Some Product Description",
              // optional - the category of this product
              "itemCategory" => "T-Shirts",
              // optional - the manufacturer, brand name or company / owner of this product (if any)
              "itemManufacturer" => "Acme Co"
         )
    )
);

$tracker->pageView('https://example.com', $properties);