javascript - How do I report on a session variable in google analytics? -
i new custom variables in google analytics. don't understand how grab session/post variables user.
i trying capture $_session['usercountry'] , add ga data. displays country user signed up.
from ga
_gaq.push(['_setcustomvar', 2, // custom var set slot #2. required parameter. 'country', // name of custom variable. required parameter. '???', // value of custom variable. required parameter. // (you might set value default no) 2 // sets scope session-level. optional parameter. ]); do put in usercountry in have question marks?
client-side javascript doesn't have access $_session, collection kept server-side.
you'll need expose value javascript somehow. 1 option include in initial output php:
<script> var usercountry = <? echo json_encode($_session['usercountry']) $>; </script> this uses json_encode() , takes advantage of json's relation , shared syntax javascript, it'll parsed javascript literal. presumably string, result php echo similar to:
<script> var usercountry = "country name"; </script> then, can use google analytics:
_gaq.push([ '_setcustomvar', 2, // custom var set slot #2. required parameter. 'country', // name of custom variable. required parameter. usercountry , // value of custom variable. required parameter. // (you might set value default no) 2 // sets scope session-level. optional parameter. ]);
Comments
Post a Comment