2013-07-31 22 views
1

我是新來的谷歌分析自定義變量。我不明白如何從用戶中獲取會話/發佈變量。如何報告Google分析中的會話變量?

我試圖捕獲$_SESSION['usercountry']並將其添加到GA數據中。它只顯示用戶註冊的國家。

從GA

_gaq.push(['_setCustomVar', 
     2,     // This custom var is set to slot #2. Required parameter. 
     'Country', // The name of the custom variable. Required parameter. 
     '???',    // The value of the custom variable. Required parameter. 
          // (you might set this value by default to No) 
     2     // Sets the scope to session-level. Optional parameter. 
]); 

難道我只是把usercountry在我的問號?

回答

1

客戶端JavaScript無權訪問$_SESSION,因爲集合保留在服務器端。

您需要的值以某種方式暴露給JavaScript。一種選擇是簡單地將其包括在從PHP初始輸出:

<script> 
    var userCountry = <? echo json_encode($_SESSION['usercountry']) $>; 
</script> 

這使用json_encode()和利用JSON的關係和共享的語法與JavaScript,因此它會被解析爲JavaScript literal。想必String,所以從PHP echo結果將類似於:

<script> 
    var userCountry = "Country Name"; 
</script> 

然後,你可以使用它的谷歌Analytics(分析):

_gaq.push([ 
    '_setCustomVar', 
    2,     // This custom var is set to slot #2. Required parameter. 
    'Country',   // The name of the custom variable. Required parameter. 
    userCountry ,  // The value of the custom variable. Required parameter. 
         // (you might set this value by default to No) 
    2     // Sets the scope to session-level. Optional parameter. 
]); 
+0

感謝您的幫助。我看了大約20個教程,並沒有看到這些值是如何通過的,因爲GA不知道用戶國家是什麼。將測試它並尋找結果。 – blankip

+0

這是一個字符串。爲什麼「var userCountry =」Country Name「; required? – blankip

+0

@DavidMoore」Country Name「僅僅是前面代碼片段中PHP輸出的一個例子,JavaScript可能解析的內容,您不需要包含它。 –

相關問題