You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ga('send', 'pageview')方法替换下方代码?

Alternatives to ga('send', 'pageview')

Hey there! Let's walk through the modern alternatives to the old ga('send', 'pageview') syntax—since analytics.js (the library behind that call) is deprecated and no longer supported by Google. Here are the most reliable, up-to-date approaches you can use:

1. Google Analytics 4 (GA4) with gtag.js

GA4 is Google's current analytics platform, and it uses gtag.js for tracking. To send a pageview, you'll use the page_view event (GA4's native page tracking event):

// Send a GA4 pageview with optional context parameters
gtag('event', 'page_view', {
  page_title: document.title,
  page_location: window.location.href,
  page_path: window.location.pathname
});

The parameters here are optional but highly recommended—they help enrich your tracking data with specific page details. If you've already initialized your GA4 property with gtag('config', 'G-XXXXXXXXXX'), you can also pass pageview parameters directly in the config call to auto-send a pageview on load:

gtag('config', 'G-XXXXXXXXXX', {
  page_title: 'Custom Page Title',
  page_path: '/custom-page-path'
});

2. Universal Analytics (UA) with gtag.js (Legacy)

If you're still using Universal Analytics (note: UA stopped processing new data in July 2023), you can use gtag.js to send pageviews instead of the old ga() call:

// Send a UA pageview (replaces ga('send', 'pageview'))
gtag('config', 'UA-XXXXXXXXX-X', {
  page_title: 'Your Page Title',
  page_path: '/your-page-path'
});

This config method initializes your UA property and automatically sends a pageview. You can override the default page details by passing the optional parameters shown above.

3. Google Tag Manager (GTM)

If you're using GTM to manage your analytics tags, you can trigger a pageview via the data layer instead of writing direct tracking code:

// Push a pageview event to the GTM data layer
dataLayer.push({
  'event': 'custom_pageview',
  'pageTitle': document.title,
  'pagePath': window.location.pathname
});

After pushing this event, you'll need to set up a Custom Event Trigger in GTM that listens for custom_pageview, then link it to your GA4 or UA tag to fire the pageview tracking.

Important Note

The original ga('send', 'pageview') relies on analytics.js, which is fully deprecated. Migrating to GA4 is strongly recommended to ensure your tracking continues to work and you get access to Google's latest analytics features.

内容的提问来源于stack exchange,提问作者Pradeep Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:47:13