2016-03-07 26 views
0

這是我用來從Google表格使用Google Visualization API檢索表格的代碼片段。追蹤Google Visualization 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事件onclick的下載。

<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> 

此代碼的工作,如果在一個「正常」的網頁(即事件在谷歌Analytics(分析)追蹤)使用,但它不能在這裏工作(我相信,因爲它是一個iframe裏面?)。 有沒有可以跟蹤事件的解決方法?

+0

iframe是否啓用GA跟蹤? – nyuen

+0

iframe是Google從Google加載的表格。我無法訪問它 – MultiformeIngegno

+0

然後根本無法跟蹤iframe中的任何內容。您需要在該頁面上添加您的Google Analytics跟蹤代碼才能進行任何跟蹤。 – nyuen

回答

0

如果您在iframe中包含Google Analytics跟蹤代碼,並且(如果iframe來自其他域),您可以設置跟蹤iframe中的跨網域跟蹤代碼,鏈接兩個領域在一起。從它的聲音,你將無法跟蹤這一點。

另外請記住,頁面上的iframe在沒有跟蹤iframe的情況下會被視爲反彈,因爲您正在加載您的頁面,然後google會觸發,然後Google會認爲您將要離開,因爲您正在加載單獨的域。

一種解決方案可能是嘗試並建立您將觸發下載圖表(通過有效地使用圖像的MIME類型再次請求的話)的iframe之外自己的按鈕。你可以跟蹤。它不一定會獲得100%的點擊次數,但至少有一些(即點擊按鈕的人,而不是iframe)。

+0

感謝您的回答!問題是iframe是由Google Visualization API生成的,我不認爲我可以在其中添加我自己的代碼... – MultiformeIngegno

+0

如果您無法控制iframe,恐怕沒有什麼可以做跟蹤。 –