Register

Install website tracking by using the PHP tracking library

Our platform gives you the option to add code snippets on your website's HTML so that you can track your website visitors' actions and use them as triggers for your automation workflows. If your website is based on a CMS or an 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 account.

Below you will 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 won't send any data to the server without proper initialization. The initialization phase deals with some Cookies that determine if the 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. Identifying Visitors

The identify call lets you tie users 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');

// You can also 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 our platform's tracker library.
 

<?php

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

 


E. Tracking Item Added to Cart

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

In e-commerce tracking, it is recommended to track when users complete their order 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 following 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-targeting strategies like browse abandonment and engage with your recipients if they view but do not purchase a product. You will need to add the following 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);

 

About our platform's Website Tracking

Website Tracking
Our platform's website tracking is a web analysis service provided by our company. The data collected is made available to the client for actionable insights (reporting, segmentation, and automation). Direct access to the data is only provided to the client through the use of our platform. The data may be used in anonymized and aggregated form from Moosend Ltd. for reporting purposes.

Visitor Tracking
Our platform's website tracking uses proprietary, cookie-based, technology to track data on end-user page visits, product views, user cart, and purchase information but not limited to product quantity and pricing. Data collection at any of the above levels can be turned on or off by the relevant client of our platform. Our company has no control of the status or information shared through its website tracking feature. More specifically the cookie-technology stores:

  • An identification number to uniquely identify a user's browser session.
  • An identification number to uniquely identify a browser. It's not possible to uniquely identify a device through this feature
  • An identification number to uniquely identify the relevant website.
  • Relevant flags to enable or disable certain library features including but not limited to the exit-intent functionality

Identifying Specific Users
Our website tracking uses proprietary, cookie-based, technology to link the aforementioned data to a specific user identified by his email address. This setup allows for more accurate tracking of end-users by associating them with the same ID over various sessions and devices. This functionality is called Identify. Specific user data collection as part of the identify functionality can be turned on or off by the relevant client of our platform. Our company has no control of the status or information shared through its website tracking feature.