Access previously stored Visitor-level custom variable

这一生的挚爱 提交于 2019-12-09 23:47:30

问题


In Google Analytics, Visitor-level custom variables are stored in cookies.

I'm looking to store some data in a GA custom variable only if no data has been stored in this slot before, i.e. never overwrite visitor-level custom variables.

Is there any way I can do that, either by testing whether some data has already been stored, or by retrieving the stored value?

There's no _getCustomVar, but is there any way I can do something equivalent without "hacking" into the cookies myself?


回答1:


There is _getVisitorCustomVar(index).

eg:

_gaq.push(function() {
  var pageTracker = _gat._getTrackerByName(); // Gets the default tracker.
  var visitorCustomVar1Value = pageTracker._getVisitorCustomVar(1);
});

It only works for Visitor level custom var because that's the only one stored client size in the GA cookie __utmz. But that's exactly what you are looking for I guess.

If you want to see if a non-visitor level custom var that has already been set, then you would need to write the logic yourself storing a separate cookie when you set the custom var to be able to retrieve later. But it should be a simple thing and probably better idea than hacking into proprietary cookies.




回答2:


<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'xxxxxxxxxxxxxxx']);

   _gaq.push(function() {
        var pageTracker = _gat._getTrackerByName();
        var userTypeVar = pageTracker._getVisitorCustomVar(1);
    });

  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>


来源:https://stackoverflow.com/questions/13181514/access-previously-stored-visitor-level-custom-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!