2017-10-16 14:59
浏览 70

GA基础知识 - 基于动态PHP的站点中的页面视图跟踪的最佳实践

Sorry for the basic question, but I've been reading lots on this and haven't been able to piece together a solution (as a rookie developer) so thought maybe asking would be helpful to me and others in the same boat.

I'm attempting to install GA (gtag) on a PHP site. The initialisation code works fine, Google Tag Assistant is happy, but the site functions similar to a single page application whereby the content is all loaded in dynamically via js into a DIV (little floating overlays).

I've read that creating a separate analyticstracking.php file is a best practice for the inclusion of your tracking code on pages (using include_once), but doing this on the dynamically loaded php files seems to throw up an error within tag manager – "Same web property ID is tracked twice."

My assumption was that this was happening because the page name didn't change, which would be logical, but if true, how and where does one change the page name?

Is it best to do this as an event when the nav item loading content is clicked, or is it best to somehow include tracking data in the PHP files for loaded content (and how)? If the latter, is is possible to pass the page name to the included analyticstracking.php call on load?

Or perhaps I doing this all wrong and should I be using Google Tag Manager?

Thanks in advance, Joel

图片转代码服务由CSDN问答提供 功能建议

对不起基本问题,但我一直在阅读很多关于此问题并且无法拼凑 一个解决方案(作为一个新手开发人员),所以想也许会对我和其他人在同一条船上有所帮助。

我正在尝试在PHP网站上安装GA(gtag)。 初始化代码工作正常,Google Tag Assistant很高兴,但网站功能类似于单页面应用程序,其中内容全部通过js动态加载到DIV(小浮动叠加层)。

我读过创建一个单独的analyticstracking.php文件是在页面上包含跟踪代码的最佳实践(使用include_once),但是在动态加载的php文件上执行此操作 似乎在标签管理器中引发了一个错误 - “跟踪了两次相同的网络媒体资源ID。”

我的假设是发生了这种情况,因为页面名称没有改变,这将是 逻辑,但如果是真的,如何以及在何处更改页面名称?

最好是在单击导航项加载内容时将其作为事件执行,还是最好以某种方式在PHP文件中跟踪加载内容的数据(以及如何)? 如果是后者,是否可以将页面名称传递给加载时包含的analyticstracking.php调用?

或者我这样做是错的,我应该使用Google跟踪代码管理器吗? / p>

提前致谢, Joel

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjiaonie6097
    duanjiaonie6097 2017-10-16 19:16

    Best solution for your situation i guess will be something called "virtual pageview". Google Analytics documentation describes it here:

    In shortcut:

    1. Include Google Analytics (or GTag) code only once, for example in head.
    2. Every additional content loading (which is key for you and want to treat as a new pageview) should send JS code with new "virtual" pageview to GA with datas seted by you (like new pageview title, new pageview url etc.).

    Forget about sending events in this case as a "core". Of course you can also track events with nav clicks but first implement virtual pageviews.

    点赞 评论