使用Google Analytics跟踪不同域中的访问者

时间:2021-12-26 15:18:46

We provide a <div> block to our clients to put on their websites, which they then integrate into their site using a script that will generate the html.

我们为客户提供了一个

块来放置他们的网站,然后使用将生成html的脚本将其集成到他们的网站中。

For example:

<div class="currentDiv">

<script type="text/javascript" id="axxaax99" src="xxxx-xxx.js"></script>

</div>

How do I track the number of visitors in Google Analytics, through our <div> block that is placed on our clients websites?

如何通过我们客户网站上的

块跟踪Google Analytics中的访问者数量?

1 个解决方案

#1


0  

I was able to solve this and I want to share my solution to save other people time.

我能够解决这个问题,我想分享我的解决方案以节省其他人的时间。

so first thing to do on client website is to check if google analytics is already there or we load it and then you can fire a page-event send using the tracker name that you already created.

所以在客户端网站上要做的第一件事就是检查谷歌分析是否已经存在,或者我们加载它,然后你可以使用你已经创建的跟踪器名称来发送页面事件发送。

<script type="text/javascript">
                
var appendAnalytics = function (i, s, o, g, r, cb) {
                    i["GoogleAnalyticsObject"] = r;
i[r] = i[r] || function () {
    (i[r].q = i[r].q || []).push(arguments)}, i[r].l = 1 * new Date();
var a = s.createElement(o);
var m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
    
a.onload = cb;
m.parentNode.insertBefore(a, m)};
    
if (window.ga === undefined) {
    
appendAnalytics(window, document, "script", "//www.google-analytics.com/analytics.js", "ga", sendPageView);
} else {
    
sendPageView();
}
    
function sendPageView() {
    
window.ga("create", "UA-XXXXXX”, "auto", {
    name: “YourTrackerName”
                    });
window.ga("YourTrackerName.send", "pageview");
    
}
    
</script>

#1


0  

I was able to solve this and I want to share my solution to save other people time.

我能够解决这个问题,我想分享我的解决方案以节省其他人的时间。

so first thing to do on client website is to check if google analytics is already there or we load it and then you can fire a page-event send using the tracker name that you already created.

所以在客户端网站上要做的第一件事就是检查谷歌分析是否已经存在,或者我们加载它,然后你可以使用你已经创建的跟踪器名称来发送页面事件发送。

<script type="text/javascript">
                
var appendAnalytics = function (i, s, o, g, r, cb) {
                    i["GoogleAnalyticsObject"] = r;
i[r] = i[r] || function () {
    (i[r].q = i[r].q || []).push(arguments)}, i[r].l = 1 * new Date();
var a = s.createElement(o);
var m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
    
a.onload = cb;
m.parentNode.insertBefore(a, m)};
    
if (window.ga === undefined) {
    
appendAnalytics(window, document, "script", "//www.google-analytics.com/analytics.js", "ga", sendPageView);
} else {
    
sendPageView();
}
    
function sendPageView() {
    
window.ga("create", "UA-XXXXXX”, "auto", {
    name: “YourTrackerName”
                    });
window.ga("YourTrackerName.send", "pageview");
    
}
    
</script>