Tracking Liferay CMS Content on External Sites
You can track views, impressions, and downloads of Liferay Content Management System (Liferay CMS) assets. On a Liferay DXP site that syncs to an LDP property, the tracking is automatic. On an external site that reads CMS assets through the CMS headless APIs, the site sends Liferay Data Platform (LDP) the same events Liferay DXP sends from its own pages, so the CMS Dashboard counts them in each asset’s metrics.
To send events, load the LDP analytics client on each page that displays CMS assets, and add tracking attributes to the HTML element that shows each asset.
Prerequisites
Before tracking assets on an external site, ensure these requirements are met:
-
Your Liferay DXP instance is connected to LDP. See Integrating Liferay Data Platform with Liferay DXP.
-
The space that holds the assets is connected to a site, and that site syncs to an LDP property. The CMS Dashboard’s Performance tab shows metrics only for spaces connected to a site, and an asset’s in-context analytics appear only when that site is synced, even when the assets appear only on an external site.
-
You have the region key and the WeDeploy key for your LDP workspace. The analytics client uses them to send events to your workspace. Request both from Liferay Support.
-
You can edit the HTML of the external site’s pages.
Loading the Analytics Client
The analytics client is a JavaScript library that sends an event to LDP when a visitor sees or downloads an asset. Add it to the HTML template that renders the external site’s pages, so that every page that displays assets loads it.
-
In the template’s
<head>element, add a canonical link:<link rel="canonical" href="https://www.example.com/news/spring-product-launch">Set
hrefto the canonical URL of the page that displays the assets. The analytics client reads this tag each time it sends an event. On a single-page site, update the tag whenever the route changes. -
In the template’s
<body>element, add this script. It loads the analytics client from Liferay’s CDN, connects it to your LDP property and data source, and adds your site’s ID to each event:<script> (function (u, c, a, m, o, l) { (o = 'script'), (l = document), (a = l.createElement(o)), (m = l.getElementsByTagName(o)[0]), (a.async = 1), (a.src = u), (a.onload = c), m.parentNode.insertBefore(a, m); })('https://analytics-js-cdn.liferay.com', function () { var PROPERTY_ID = "Replace with your property ID"; var DATA_SOURCE_ID = "Replace with your data source ID"; var THE_REGION_KEY = "Replace with your region key"; var WEDEPLOY_KEY = "Replace with your WeDeploy key"; var SITE_ID = "Replace with your site ID"; Analytics.create( { channelId: PROPERTY_ID, dataSourceId: DATA_SOURCE_ID, endpointUrl: `https://osbasahpublisher-ac-${THE_REGION_KEY}.lfr.cloud`, projectId: WEDEPLOY_KEY }, [ function (request) { request.context.groupId = SITE_ID; return request; } ] ); }); </script> -
Replace the five placeholder values in the script:
-
PROPERTY_ID: The Property ID of the LDP property that the space’s connected site syncs to. In LDP, go to Settings → Properties and copy it from the Property ID column. -
DATA_SOURCE_ID: In LDP, go to Settings → Data Sources, select the data source your instance is connected with, and copy the Data Source ID from its overview. The CMS performance views look up an asset’s metrics by this ID and the asset’s external reference code (ERC). -
THE_REGION_KEYandWEDEPLOY_KEY: The region key and the WeDeploy key from Liferay Support. The region key sets the address the events are sent to, and the WeDeploy key identifies your workspace there. -
SITE_ID: The site ID of the site that is connected to the space and syncs to the property you set inPROPERTY_ID. To find it, see Identify the Site Containing the Data. The CMS Dashboard’s Performance tab uses this ID to match events to the asset’s space.
-
To attribute the events to a known individual, call Analytics.setIdentity() with the individual’s email and name. Liferay DXP does this automatically for signed-in users. To learn more about Analytics.setIdentity(), see Tracking Events in Third Party Websites.
Adding Tracking Attributes to Each Asset
For an asset’s metrics to appear in the CMS Dashboard’s Performance tab, add these five attributes to each element that renders the asset:
| Attribute | Value |
|---|---|
data-analytics-asset-type | object-entry |
data-analytics-asset-action | view, impression, or download. See How Each Action Sends an Event. |
data-analytics-external-reference-code | The asset’s externalReferenceCode from the API response |
data-analytics-object-definition-name | The object definition name of the asset’s content structure: CMSBasicWebContent for Basic Web Content, CMSBasicDocument for Basic Document, or C_ followed by a custom structure’s Content Structure Name, such as C_ProductGuide |
data-analytics-asset-title | The asset’s title |
The first four are required for the analytics client to send an event. Without the title, the client sends the event, but the asset doesn’t appear in the Performance tab.
This markup shows the full content of a Basic Web Content asset and a download link for a Basic Document asset:
<article
data-analytics-asset-action="view"
data-analytics-asset-title="Spring Product Launch Announcement"
data-analytics-asset-type="object-entry"
data-analytics-external-reference-code="spring-product-launch-announcement"
data-analytics-object-definition-name="CMSBasicWebContent"
>
<h2>Spring Product Launch Announcement</h2>
<p>The spring release adds three new product lines to the catalog.</p>
</article>
<a
data-analytics-asset-action="download"
data-analytics-asset-title="Spring Product Catalog"
data-analytics-asset-type="object-entry"
data-analytics-external-reference-code="spring-product-catalog"
data-analytics-object-definition-name="CMSBasicDocument"
href="https://www.example.com/downloads/spring-product-catalog.pdf"
>
Download the Spring Product Catalog
</a>
If your site builds its pages with JavaScript in the browser, set the attributes as you create each asset’s element. This function renders an asset from the items array that GET /o/cms/basic-web-contents/scopes/{scopeKey}/approved returns, and adds it to a list:
function renderAsset(asset) {
const entry = document.createElement('li');
entry.setAttribute('data-analytics-asset-action', 'impression');
entry.setAttribute('data-analytics-asset-title', asset.title);
entry.setAttribute('data-analytics-asset-type', 'object-entry');
entry.setAttribute(
'data-analytics-external-reference-code',
asset.externalReferenceCode
);
entry.setAttribute(
'data-analytics-object-definition-name',
'CMSBasicWebContent'
);
entry.textContent = asset.title;
document.querySelector('ul').appendChild(entry);
}
Once the client loads, it automatically sends events for every element that carries the attributes, including elements your script adds to the page later. You never call Analytics.track() manually.
In these cases, no event is sent:
- A browser with Do Not Track turned on
- Reading an asset through the headless APIs. A page that loads the client is required to send events.
How Each Action Sends an Event
The value of data-analytics-asset-action sets which event the client sends and when:
| Action | Element | Event | Trigger |
|---|---|---|---|
view | The element that shows an asset’s full content | objectEntryViewed | The element first becomes visible in the browser window. |
impression | An element that shows an asset’s title or summary, such as a list entry | objectEntryImpressionMade | The element first becomes visible in the browser window. |
download | The download link itself, not a container around it | objectEntryDownloaded | A visitor clicks the link. |
The client sends objectEntryDownloaded only when the visitor clicks the element that carries the tracking attributes. If the visitor clicks an element inside the download link, such as an icon or a <span>, the file still downloads, but the client sends no event. No error appears. To make sure the client sends the event, keep only text inside the link, or set pointer-events: none on its children so that clicks target the link itself.
Viewing the Metrics
The CMS Dashboard’s Performance tab and each asset’s in-context analytics show the metrics LDP records from your external site. In an asset’s in-context analytics, they appear when the channel filter is set to All Channels (the default) or to the site whose ID you set in SITE_ID. To find and filter the metrics, see Content Management System Dashboards and Analytics.
Complete Example
This page declares its canonical URL, adds tracking attributes to a viewed asset and a download link, and loads the analytics client:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spring Product Launch</title>
<link rel="canonical" href="https://www.example.com/news/spring-product-launch">
</head>
<body>
<main>
<article
data-analytics-asset-action="view"
data-analytics-asset-title="Spring Product Launch Announcement"
data-analytics-asset-type="object-entry"
data-analytics-external-reference-code="spring-product-launch-announcement"
data-analytics-object-definition-name="CMSBasicWebContent"
>
<h2>Spring Product Launch Announcement</h2>
<p>The spring release adds three new product lines to the catalog.</p>
</article>
<a
data-analytics-asset-action="download"
data-analytics-asset-title="Spring Product Catalog"
data-analytics-asset-type="object-entry"
data-analytics-external-reference-code="spring-product-catalog"
data-analytics-object-definition-name="CMSBasicDocument"
href="https://www.example.com/downloads/spring-product-catalog.pdf"
>
Download the Spring Product Catalog
</a>
</main>
<script>
(function (u, c, a, m, o, l) {
(o = 'script'),
(l = document),
(a = l.createElement(o)),
(m = l.getElementsByTagName(o)[0]),
(a.async = 1),
(a.src = u),
(a.onload = c),
m.parentNode.insertBefore(a, m);
})('https://analytics-js-cdn.liferay.com', function () {
var PROPERTY_ID = "Replace with your property ID";
var DATA_SOURCE_ID = "Replace with your data source ID";
var THE_REGION_KEY = "Replace with your region key";
var WEDEPLOY_KEY = "Replace with your WeDeploy key";
var SITE_ID = "Replace with your site ID";
Analytics.create(
{
channelId: PROPERTY_ID,
dataSourceId: DATA_SOURCE_ID,
endpointUrl: `https://osbasahpublisher-ac-${THE_REGION_KEY}.lfr.cloud`,
projectId: WEDEPLOY_KEY
},
[
function (request) {
request.context.groupId = SITE_ID;
return request;
}
]
);
});
</script>
</body>
</html>