简体   繁体   English

Google Analytics onClick发送事件

[英]Google Analytics onClick to send an event

I asked a question on the Webmasters Stack Exchange about how to track which users were clicking on an application login URL on our website. 我问了一个问题,在网络管理者堆栈交易所如何跟踪哪些用户点击我们网站上的应用程序登录URL。

The set up is that I have a website, https://www.example.com and a web application on a subdomain, https://app.example.com/login.php . 设置是,我有一个网站https://www.example.com和一个位于子域https://app.example.com/login.php上的Web应用程序。

Both sites have the same Google Analytics tracking code installed. 两个网站都安装了相同的Google Analytics(分析)跟踪代码。

In the navigation on https://www.example.com I have a URL to the login page of the web application: https://www.example.com上的导航中,我有一个指向Web应用程序登录页面的URL:

<a href="https://app.example.com/login.php">Login</a>

All I'm trying to do is show in Analytics who has clicked on this link. 我要做的就是在Google Analytics(分析)中显示谁点击了此链接。

So one of the answers given involves using Events. 因此,给出的答案之一就是使用事件。 I've read both Google's Event Tracking documentation and Google Analytics Event Tracking onClick Code but I can't get this to work programatically. 我已经阅读了Google的事件跟踪文档Google Analytics(分析)事件跟踪onClick代码,但无法使其以编程方式工作。

I've tried adding an onClick to my anchor as follows: 我尝试将onClick添加到锚点中,如下所示:

<a href="https://app.example.com/login.php" onClick="ga('send', 'event', 'Web Application', 'Login', 'Logged in via public website'); ">Login</a>

This doesn't seem to record anything in Analytics - nothing is showing up under Events, including the Real Time data section. 这似乎并未在Analytics(分析)中记录任何内容-事件(包括「即时资料」部分)下没有任何显示。

However, I'm not convinced this works from a programatic perspective because if I run the ga() command in my browser console: 但是,我不相信从编程角度来看这是可行的,因为如果在浏览器控制台中运行ga()命令,则:

ga('send', 'event', 'Web Application', 'Login', 'Logged in via public website')

It just returns undefined . 它只是返回undefined I don't understand this as my website has the Analytics tracking code installed, and already uses things such as ga('send', 'pageview'); 我不明白这一点,因为我的网站已安装了Google Analytics(分析)跟踪代码,并且已经在使用ga('send', 'pageview'); . There is loads of data in Analytics for the website, so clearly it is running. 该网站的Analytics(分析)中有大量数据,因此很明显它正在运行。

I feel like this - and all of the documentation - is really complicated to do what should be a very simple task. 我觉得这样-和所有文档-要做一件非常简单的任务真的很复杂。 Please can someone enlighten me as to how (or indeed if) this is possible? 请有人能启发我(或实际上是否)可行吗?

Your code: 您的代码:

ga('send', 'event', 'Web Application', 'Login', 'Logged in via public website')

From the Documentation events 从文档事件

eventCategory text yes Typically the object that was interacted with (eg 'Video') eventCategory文本是通常与之交互的对象(例如“视频”)

eventAction text yes The type of interaction (eg 'play') eventAction文本是交互的类型(例如“播放”)

eventLabel text no Useful for categorizing events (eg 'Fall Campaign') eventLabel文本否可用于对事件进行分类(例如“秋季战役”)

eventValue integer no A numeric value associated with the event (eg 42) eventValue 整数否与事件关联的数值(例如42)

The last value is an int not a string 'Logged in via public website' is not valid 最后一个值是整数,不是字符串'通过公共网站登录'无效

ga('send', 'event', 'Web Application', 'Login', 42)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM