单击按钮后如何插入Google Analytics(分析)? [英] How to insert Google Analytics after clicking a button?

查看:63
本文介绍了单击按钮后如何插入Google Analytics(分析)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮后插入Google Analytics(分析)摘要,因此将其计为一次访问.

I'm trying to insert Google Analytics snippet after clicking a button, and therefore counting it as a visit.

是否可以执行而无需重新加载页面?

Is it possible to do without needing to reload the page?

推荐答案

首先将Google Analytics(分析)代码保存到变量中.

First save the Analytics code into a variable.

然后在单击按钮时将其附加到head标记.

Then append it to the head tag when the button is clicked.

ga = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" +
     "(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o)," +
     "m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)" +
     "})(window,document,'script','//www.google-analytics.com/analytics.js','ga');" +
     "ga('create', 'UA-XXXXXXXX-X', 'auto');" +
     "ga('send', 'pageview');"

$('a.button').on('click', function() {
  $('head').append('<script>' + ga + '</script>');
});

这篇关于单击按钮后如何插入Google Analytics(分析)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆