Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activations from same user counted multiple times in a cohort #15

Open
alexhanh opened this issue Apr 18, 2015 · 1 comment
Open

Activations from same user counted multiple times in a cohort #15

alexhanh opened this issue Apr 18, 2015 · 1 comment

Comments

@alexhanh
Copy link

CohortMe.analyze(period: 'months', activation_class: Transfer.unscoped) results in this query

SELECT DISTINCT transfers.user_id, transfers.created_at, cohort_date, FLOOR(extract(epoch from (transfers.created_at - cohort_date))/2592000) as periods_out FROM "transfers" JOIN (SELECT transfers.user_id, MIN(transfers.created_at) as cohort_date FROM "transfers" WHERE (created_at > '2014-04-18 11:31:53.062696') GROUP BY "transfers"."user_id") AS cohorts ON transfers.user_id = cohorts.user_id WHERE (created_at > '2014-04-18 11:31:53.062696')

If I make couple of transfers as a user in a cohort, then the count will increase by the number of transfers (activations) instead of just 1. That is, starting with this [2, 0, 0, 0, 0, 0, 0, 0, 0, 0] and then after multiple transfer by the same user results in [2, 0, 0, 0, 0, 0, 0, 0, 2, 0] instead of [2, 0, 0, 0, 0, 0, 0, 0, 1, 0].

I'm using Rails 4.2.1 and Postgresql 9.4.1. Please advice!

@procommerz
Copy link

Better late than never, for anyone who bumps into this (sorry for not posting a commit):

For Rails 4.2+ this line https://github.com/n8/cohort_me/blob/master/lib/cohort_me.rb#L54 has to to read like:

data.all.to_a.uniq {|d| [d.send(activity_user_id), d.cohort_date, d.periods_out] }

Otherwise ActiveRecord's relation uniq method takes precedence and the closure is not actually applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants