2013-07-31 15 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可能解析的内容,您不需要包含它。 –

相关问题