跟踪 Google 可视化 API 请求中的事件

Tracking events inside a Google Visualization API request

本文关键字:事件 请求 API Google 可视化 跟踪      更新时间:2023-09-26

这是我用来使用谷歌可视化API从谷歌表格中检索表格的片段。

google.load('visualization', '1', {
    packages: ['table']
});
var visualization;
function drawVisualization() {
    var query = new google.visualization.Query('http://spreadsheets.google.com/tq?key=XXXXXXXX&hl=it_IT');
    query.setQuery('SELECT B, C, D, E, F, G, H where upper(B) like upper("%<?php echo $search; ?>%") or upper(D) like upper("%<?php echo $search; ?>%") or upper(F) like upper("%<?php echo $search; ?>%") order by G DESC label G "Data"');
    query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
    if (response.isError()) {
        alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
    }
    var data = response.getDataTable();
    var formatter = new google.visualization.PatternFormat(
        '<a href="{6}" target="_blank" onclick="var that=this;_gaq.push([''_trackEvent'',''Event Category'',{2},this.href]);setTimeout(function(){location.href=that.href;},200);return false;">{2}</a>');
    // Apply formatter and set the formatted value of the first column.
    formatter.format(data, [0, 1, 2, 3, 4, 5, 6], 2);
    var view = new google.visualization.DataView(data);
    view.setColumns([2, 0, 1, 4, 5]); // Create a view with the first column only.
    visualization = new google.visualization.Table(document.getElementById('table'));
    visualization.draw(view, {
        legend: 'bottom',
        allowHtml: true
    });
}
google.setOnLoadCallback(drawVisualization);

如您所见,我正在尝试跟踪在点击时触发 Javascript 事件的下载。

<a href="URL" target="_blank" onclick="var that=this;_gaq.push(['_trackEvent','EVENT_CATEGORY','EVENT_URL',this.href]);setTimeout(function(){location.href=that.href;},200);return false;">LINK_NAME</a>

如果在"普通"页面中使用,此代码有效(即在Google Analytics中跟踪事件),但它在这里不起作用(我猜是因为它在iframe内?是否有能够跟踪事件的解决方法?

如果您可以在 iframe 中包含 GA 跟踪代码,则可以使用 Google Analytics(分析)为 iframe 设置跟踪,如果您(如果 iframe 来自不同的网域)如果您在 iframe 中附加跨网域跟踪代码,将两个网域链接在一起。从它的声音中,您将无法跟踪这一点。

另请记住,页面上没有在 iframe 中进行跟踪的 iframe 将被视为退回,因为您正在加载页面,然后谷歌触发,然后谷歌会认为您要离开,因为您正在加载一个单独的域。

一种解决方案可能是尝试在 iframe 之外构建您自己的按钮,这将触发图表的下载(通过使用图像 mimetype 有效地再次请求它)。你可以跟踪它。它不一定会获得100%的点击,但至少会得到一些点击(即点击按钮的人,而不是iframe)。