简体   繁体   English

Mixpanel数据采样/事件采样

[英]Mixpanel Data Sampling/Event Sampling

I found this code for Google Analytics which lets you analyze just a subset of data for your analytics. 我在Google Analytics(分析)中找到了这段代码,可让您仅分析其中一部分数据。

_gaq.push(['_setSampleRate', '80']);

I want to do the same thing with Mixpanel but from what I understand SetSampleRate is a function that is specific to Google Analytics. 我想对Mixpanel做同样的事情,但是据我了解,SetSampleRate是一个专门针对Google Analytics(分析)的函数。

How might I do something like this in Mixpanel? 我如何在Mixpanel中做类似的事情?

I have browsed their KB & Help articles but haven't found anything that talks about this. 我浏览了他们的知识库和帮助文章,但没有发现任何有关此的文章。

All you have to do is create a Random number from 0 to 100 and check if it's lower than the sample target you have. 您所要做的就是创建一个介于0到100之间的随机数,并检查它是否低于您的样本目标。 If it's lower you track it, otherwise you don't. 如果它较低,则跟踪它,否则就不跟踪。

The way _setSampleRate works in Google Analytics is that it samples by user not by hit. _setSampleRate在Google Analytics(分析)中的工作方式是按用户而不是按点击进行采样。 So when you generate the Random number you also have to store it in a cookie so that you can check for further interactions and either track it or not. 因此,当您生成随机数时,还必须将其存储在Cookie中,以便您可以检查是否有进一步的交互作用,也可以跟踪或不跟踪它。

In the Example below I created a helper function that checks if the user is in the Sample and handles the cookie logic for me. 在下面的示例中,我创建了一个辅助函数,该函数检查用户是否在示例中,并为我处理cookie逻辑。

function inSample(target) { 
  var domain_name = 'mysite.com'; // CUSTOMIZE WITH YOUR DOMAIN
  var sampleCookie = 'mixpanel_sample='; // COOKIE NAME

  var current = document.cookie;

  if (current.indexOf(sampleCookie) > -1) {
    // Cookie already exists use it
    var current = document.cookie.substring(
      document.cookie.indexOf(sampleCookie) + sampleCookie.length
    );
    if (current.indexOf(';') > -1)
      current = current.substring(0,current.indexOf(';'));

    current = parseInt(current);
  } else {
    // Cookie not found calculate a random number
    current = Math.floor(Math.random()*100)

  }
    // reset the cookie to expire in 2 years
    var two_years = new Date();
    two_years.setTime(two_years.getTime() + 2*365*24*60*60*1000);
    two_years = two_years.toGMTString();

    document.cookie = sampleCookie + current + 
      '; domain=' + domain_name + '; path=/' + 
      ' ; expires=' + two_years + ';'

    return target >= current;
}

Now all you have to do is use this function in order to fire or not the mixPanel tracking Code. 现在,您所要做的就是使用此功能来启动或不启动mixPanel跟踪代码。

if (inSample(80)) {
  // MIXPANEL TRACKING CODE GOES HERE
}

What you have in the end is a report in Mixpanel that only includes 80% of your users. 最后,您将获得一份Mixpanel中的报告,其中仅包含80%的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM