def stats(self, start_date, end_date, registered_only=False):
visitors = self.filter(<EOL>start_time__gte=start_date,<EOL>start_time__lt=end_date<EOL>)<EOL>stats = {<EOL>'<STR_LIT>': <NUM_LIT:0>,<EOL>'<STR_LIT>': <NUM_LIT:0>,<EOL>'<STR_LIT>': <NUM_LIT:0>,<EOL>}<EOL>stats['<STR_LIT>'] = total_count = visitors.count()<EOL>unique_count = <NUM_LIT:0><EOL>if not total_count:<EOL><INDENT>return stats<EOL><DEDENT>total_time_on_site = visitors.aggregate(<EOL>avg_tos=Avg('<STR_LIT>'))['<STR_LIT>']<EOL>stats['<STR_LIT>'] = timedelta(seconds=int(total_time_on_site))<EOL>registered_visitors = visitors.filter(user__isnull=False)<EOL>registered_total_count = registered_visitors.count()<EOL>if registered_total_count:<EOL><INDENT>registered_unique_count = registered_visitors.values(<EOL>'<STR_LIT:user>'<EOL>).distinct().count()<EOL>time_on_site = registered_visitors.aggregate(<EOL>avg_tos=Avg('<STR_LIT>'))['<STR_LIT>']<EOL>unique_count += registered_unique_count<EOL>returns = (registered_total_count - registered_unique_count)<EOL>stats['<STR_LIT>'] = {<EOL>'<STR_LIT>': registered_total_count,<EOL>'<STR_LIT>': registered_unique_count,<EOL>'<STR_LIT>': (returns / registered_total_count) * <NUM_LIT:100>,<EOL>'<STR_LIT>': timedelta(seconds=int(time_on_site)),<EOL>}<EOL><DEDENT>if TRACK_ANONYMOUS_USERS and not registered_only:<EOL><INDENT>guests = visitors.filter(user__isnull=True)<EOL>guest_total_count = guests.count()<EOL>if guest_total_count:<EOL><INDENT>guest_unique_count = guests.values(<EOL>'<STR_LIT>'<EOL>).distinct().count()<EOL>guest_time_on_site = guests.aggregate(<EOL>avg_tos=Avg('<STR_LIT>'))['<STR_LIT>']<EOL>returns = (guest_total_count - guest_unique_count)<EOL>return_ratio = (returns / guest_total_count) * <NUM_LIT:100><EOL>time_on_site = timedelta(seconds=int(guest_time_on_site))<EOL><DEDENT>else:<EOL><INDENT>guest_total_count = <NUM_LIT:0><EOL>guest_unique_count = <NUM_LIT:0><EOL>return_ratio = <NUM_LIT:0.0><EOL>time_on_site = timedelta(<NUM_LIT:0>)<EOL><DEDENT>unique_count += guest_unique_count<EOL>stats['<STR_LIT>'] = {<EOL>'<STR_LIT>': guest_total_count,<EOL>'<STR_LIT>': guest_unique_count,<EOL>'<STR_LIT>': return_ratio,<EOL>'<STR_LIT>': time_on_site,<EOL>}<EOL><DEDENT>returns = (total_count - unique_count)<EOL>stats['<STR_LIT>'] = unique_count<EOL>stats['<STR_LIT>'] = (returns / total_count) * <NUM_LIT:100><EOL>if TRACK_PAGEVIEWS:<EOL><INDENT>if '<STR_LIT>' in stats:<EOL><INDENT>pages_per_visit = registered_visitors.annotate(<EOL>page_count=Count('<STR_LIT>')<EOL>).filter(page_count__gt=<NUM_LIT:0>).aggregate(<EOL>pages_per_visit=Avg('<STR_LIT>'))['<STR_LIT>']<EOL>stats['<STR_LIT>']['<STR_LIT>'] = pages_per_visit<EOL><DEDENT>if TRACK_ANONYMOUS_USERS and not registered_only:<EOL><INDENT>stats['<STR_LIT>']['<STR_LIT>'] = guests.annotate(<EOL>page_count=Count('<STR_LIT>')<EOL>).filter(page_count__gt=<NUM_LIT:0>).aggregate(<EOL>pages_per_visit=Avg('<STR_LIT>'))['<STR_LIT>']<EOL>total_per_visit = visitors.annotate(<EOL>page_count=Count('<STR_LIT>')<EOL>).filter(page_count__gt=<NUM_LIT:0>).aggregate(<EOL>pages_per_visit=Avg('<STR_LIT>'))['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>if '<STR_LIT>' in stats:<EOL><INDENT>total_per_visit = stats['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>total_per_visit = <NUM_LIT:0><EOL><DEDENT><DEDENT>stats['<STR_LIT>'] = total_per_visit<EOL><DEDENT>return stats<EOL>
Returns a dictionary of visits including:

            * total visits
            * unique visits
            * return ratio
            * pages per visit (if pageviews are enabled)
            * time on site

        for all users, registered users and guests.