Register

Ηow can I install website tracking by using the PHP tracking library ?

Moosend gives you the option to add code snippets on your website so that you can track that actions of your website visitors and use those as triggers for your automations. If your website is based on a CMS or a e-commerce platform you can use one of our practical plugins to achieve website tracking with just one click.

Both the unique code snippets and our plugins, around which the tracking functionality is based, are readily available right inside your Moosend account.

Below you'll find some examples of what the tracking code looks like as well as different configuration examples of how you can track different types of actions on your site by adding the code snippets to different areas of your site.

Let's get right to it:


A. Installing Website Tracking

You can add this library as a local, per-project dependency to your project using Composer:

composer require moosend/website-tracking

 

B. Usage

Before you dive in with sending events you have to create an instance of Tracker first and perform initialization. This is very important as it wont send any data to the server without a proper initialization. The init phase deals with some Cookies that determines if current user is a new visitor or a returned one.

<?php

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

$tracker->init($siteId);

 


C. Identifing Visitors

The identify call lets you tie a user to their actions and record traits about them. It includes a unique email and any optional traits you know about them like their name, etc. Our recommendation is to call "identify" after a user registers, after a user logs in and after a user provides their email as part of your newsletter subscription form (if applicable). 

<?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');


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


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

 

D. Tracking Page View Events

You can track page view events by issuing the track page view call on the Moosend tracker library.
 

<?php

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

 


E. Tracking Item Added to Cart

In e-commerce tracking it's recommended to track when a user adds an item to their cart and subsequently when they finalize their order successfully. Such events can be then used to implement cart abandonment emails. You will need to add the below tracking code whenever 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 properies 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. Tracking Order Completed Events

In e-commerce tracking it's recommended to track when a user completes their order and subsequently when they finalizes their order successfully. Such events can be then used to implement cart abandonment emails. You will need to add the below 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. Tracking Product View Events

You can track when a user views a product (before adding to cart or purchasing). This event helps to implement advanced re-targetting strategies like browse abandonment and engage with your recipients if they view but not purchase a product. You will need to add the below tracking code whenever a user views a product. 

<?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);