Issue
- I would like to create a custom event, but after checking the official documentation, I am unsure about the steps.
Environment
- Analytics Cloud
- DXP 7.0+
Resolution
Please follow these steps to create a simple custom event.
DXP 7.0+
Widget Pages' JavaScript Field
-
Go to your Liferay instance
-
Click the Settings gear at the top of the page
-
Click the Advanced tab
- Add the following JavaScript under the JavaScript section
Analytics.track('eventName',{
'propertyName': 'valueName'
}); -
Replace
eventName
with a custom name for your event that you can easily identify-
You can also replace
propertyName
andvalueName
to suit your needs
-
You can also replace
- Visit the page
→ Once you’ve saved this JavaScript code, it will fire every time you visit the page - Check the custom events in Analytics Cloud
DXP 7.0+
Web Content
- Go to your Liferay instance
-
Create a Web Content
-
Copy and add the following JavaScript code to the Web Content via the Source editor
<script>
Analytics.track('eventName',{
'propertyName': 'valueName'
});
</script> -
Replace
eventName
with a custom name for your event that you can easily identify-
You can also replace
propertyName
andvalueName
to suit your needs
-
You can also replace
-
Save this Web Content
-
Go to a page and add it, e.g. in a Web Content Display fragment
Note: no content will actually be visible on the page - Visit the page
→ Once you’ve saved this JavaScript code, it will fire every time you visit the page - Check the custom events in Analytics Cloud
DXP 7.3+
Fragment
Create a fragment with the following code in the JavaScript field:
Analytics.track('eventName',{
'propertyName': 'valueName'
});
or this in the HTML field:
<script>
Analytics.track('neweventNameevent',{
'propertyName': 'valueName'
});
</script>
Results
Additional Information
- If the custom event does not fire, it could happen because the page is loaded but the Analytics instance is not yet available. See the following article for a piece of code you can use to ensure that events trigger after the window load: Analytics Cloud Custom Event is not firing
- Tracking Events
- Event tracking with Liferay Analytics Cloud
Subscriber Exclusive Content
A Liferay Enterprise Subscription provides access to over 1,500 articles that include best practices, troubleshooting, and other valuable solutions. Sign in for full access.
Sign In