Updated on: December 11, 2025
Baidu Analytics (also known as Baidu Tongji) supports Event Tracking, which allows you to record specific user interactions on your website—such as button clicks, downloads, video plays, and more.
Compared with basic metrics like pageviews and visitor counts, event tracking gives you a far more accurate understanding of how users actually engage with your site.
Event Structure in Baidu Analytics
The event model in Baidu Analytics is completely different from Google Analytics 4.
Baidu uses the following format:
_hmt.push(['_trackEvent', category, action, label, value]);
Each event contains four predefined fields:
| Field | Description | Example |
|---|---|---|
| category | Group | button |
| action | Action | click |
| label | Label content | “Buy Now Button” |
| value | Value, optional | 1 |
Three Ways to Implement Custom Events
First, make sure the Baidu Analytics script (hm.js) is already installed on your page.
Method 1: Inline JavaScript in the HTML (Simple & Direct)
Then add the event directly inside any HTML element:
<a href=”#” onclick=”_hmt.push([‘_trackEvent’, ‘link’, ‘click_comments’, ‘label’, 1])“>Comment</a>
Explanation:
- category:
"link" - action:
"click_comments" - label:
"label" - value:
1
Pros
- Very easy to implement
- No structural changes required
Cons
- Mixes HTML structure with event logic
- Only one event handler allowed (adding another will overwrite it)
- Harder to maintain for large websites
Best for: Small sites with a limited number of simple interactions
Method 2: Using JavaScript Event Listeners (Recommended for Most Sites)
You can track events by binding listeners in JavaScript—cleaner and more flexible than inline HTML.
document.querySelector('#comment').addEventListener('click', function() {
_hmt.push(['_trackEvent', 'link', 'click_comments', 'label', 1]);
});
Pros
- Keeps HTML clean; logic lives in JS
- Supports multiple event listeners
- Allows use of options like capture/bubble, once, passive
- Ideal for modern front-end frameworks and tag implementations
Cons
- Requires basic JavaScript knowledge
Best for: Medium to large sites, scalable event tracking, engineering teams
Method 3: Using Google Tag Manager (Professional Approach)
This method is ideal for enterprises or teams that want a centralized, code-free way to manage tracking.
Step 1: Set Up the Triggers
Set triggers according to the click location:
Step 2: Set Up the Tags
In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Custom HTML」,Name it “Baidu Event-click_comments”, and make the following settings:
Step 3: Debug with Tag Assistant
Use the GTM preview mode and click the comment button,check in Tag Assistant that the tag (Tags) fired correctly
Pros
- Zero code changes to your website
- Centralized management
- Works well for large-scale analytics operations
- Easier collaboration for marketing & analytics teams
Cons
- Initial setup is more complex
- Requires GTM familiarity
Best for: Enterprise teams, complex tagging plans, ongoing optimization
Read More:Installing Baidu Tongji and Tracking Events Using Google Tag Manager
Summary: Which Method Should You Choose?
| Item | Inline HTML (onclick) |
JavaScript Listener | GTM-Based Event Tracking |
|---|---|---|---|
| Location | HTML tag | JS file or script | GTM interface (no code change) |
| Complexity | Very simple | Moderate | Moderate to high |
| Maintainability | Poor | Good | Excellent |
| Strengths | Quick, minimal setup | Flexible, scalable | Centralized, enterprise-grade |
| Weaknesses | Hard to maintain, limited | Requires JS skills | Setup time, GTM dependency |
| Best Use Case | Small sites, simple needs | Medium/large websites | Enterprise or team environments |





