Updated: November 20, 2025
Websites operating in mainland China often need to use both Google Analytics 4 and Baidu Tongji (Baidu Analytics) for data monitoring. The former is more suitable for cross-region analysis and advertising attribution, while the latter has fast loading speed and stable network within China and is often required for advertising campaigns (e.g., Baidu Ads).
Google Tag Manager (GTM), as a tag management tool, allows you to deploy Baidu Tongji without modifying code and enables flexible, maintainable event tracking.
Why Use GTM to Install Baidu Tongji
Although you can directly write Baidu Tongji into your website code, there are clear drawbacks:
| Issue | Description |
|---|---|
| Cannot manage centrally | Page tags are scattered across code, making maintenance difficult |
| No version control | Changes cannot be rolled back |
| Hard to align with GA4 | Difficult to unify event standards (like dataLayer) |
| Slow deployment | Every change requires developer involvement |
| High cross-department communication cost | Requires engineering team participation |
Using GTM solves all these problems:
- One-click deployment without code changes
- Unified management of all events
- Event structure aligned with GA4; dataLayer can be shared
- Individual testing and preview possible (no impact on production)
- Risk controlled through version publishing
If you are using Adobe Launch or Tealium iQ, the method is identical; only triggers and code management interfaces differ.
What You Need Before Installing Baidu Tongji
A Baidu Tongji account,Log in to Baidu Tongji,Click “使用设置” → “+新增网站” to create a website and obtain the hm.js code:
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?86e39c295c390b79e347ea7a9cd47fe3";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
Deploying Baidu Tongji Base Code (hm.js) via GTM
In GTM , click「Tags」——「New」——「Choose a tags type to begin setup…」——「Custom HTML」,Name it “Baidu Tongji”, and make the following settings:
This completes the basic deployment of Baidu Tongji.
Setting Up Baidu Tongji Event Tracking (Event Tracking) in GTM
The structure of Baidu Tongji events is completely different from GA4. The format is as follows:
_hmt.push(['_trackEvent', category, action, label, value]);
Field Description:
| Field | Description | Example |
|---|---|---|
| category | Group | button |
| action | Action | click |
| label | Label content | “Buy Now Button” |
| value | Value, optional | 1 |
Example: Tracking Comment Button Clicks
To track website comment clicks, the Baidu Tongji event code becomes:
_hmt.push(['_trackEvent', 'link',' click_comments', 'label', 1]);
Set Up the Triggers
Set triggers according to the click location:
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:
How to Verify if Baidu Tongji Events Are Sent Successfully
Using the Baidu Event click_comments as a validation example for event tracking.
GTM Tag Assistant Debug
Use the GTM preview mode and click the comment button,check in Tag Assistant that the tag (Tags) fired correctly
Check Requests in Network
Open browser → F12 → Network → search for “hm.gif”, this is the Baidu Tongji tracking request
Click to see the event details, confirming that the event was successfully sent and received by Baidu Tongji






