<commit_before>from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound, HTTPNotFound

from springboard.views.base import SpringboardViews

from springboard_iogt.utils import get_redirect_url, get_matching_route


ONE_YEAR = 31536000

PERSONAE = {'CHILD', 'TEENAGER', 'PARENT', 'WORKER'}
PERSONA_COOKIE_NAME = 'iogt-persona'
PERSONA_SKIP_COOKIE_VALUE = '__skip__'
PERSONA_REDIRECT_ROUTES = {'home', 'page', 'category', 'flat_page'}


def persona_tween_factory(handler, registry):

    def persona_tween(request):
        if PERSONA_COOKIE_NAME in request.cookies:
            return handler(request)

        route = get_matching_route(request)
        if route and route.name in PERSONA_REDIRECT_ROUTES:
            query = {'next': request.url}
            return HTTPFound(request.route_url('personae', _query=query))

        return handler(request)

    return persona_tween


class IoGTViews(SpringboardViews):

    @view_config(route_name='personae',
                 renderer='springboard_iogt:templates/personae.jinja2')
    def personae(self):
        return self.context()

    @view_config(route_name='select_persona')
    def select_persona(self):
        slug = self.request.matchdict['slug'].upper()
        if slug not in PERSONAE:
            raise HTTPNotFound

        # set cookie and redirect
        response = HTTPFound(location=get_redirect_url(self.request))
        response.set_cookie(PERSONA_COOKIE_NAME, value=slug, max_age=ONE_YEAR)
        return response
<commit_msg>implement persona tracking on GA via custom dimension<commit_after>from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound, HTTPNotFound

from springboard.views.base import SpringboardViews

from springboard_iogt.utils import get_redirect_url, get_matching_route


ONE_YEAR = 31536000

PERSONAE = {'CHILD', 'TEENAGER', 'PARENT', 'WORKER'}
PERSONA_COOKIE_NAME = 'iogt-persona'
PERSONA_SKIP_COOKIE_VALUE = '__skip__'
PERSONA_REDIRECT_ROUTES = {'home', 'page', 'category', 'flat_page'}


def persona_tween_factory(handler, registry):

    def persona_tween(request):
        if PERSONA_COOKIE_NAME in request.cookies:
            return handler(request)

        route = get_matching_route(request)
        if route and route.name in PERSONA_REDIRECT_ROUTES:
            query = {'next': request.url}
            return HTTPFound(request.route_url('personae', _query=query))

        return handler(request)

    return persona_tween


class IoGTViews(SpringboardViews):

    @view_config(route_name='personae',
                 renderer='springboard_iogt:templates/personae.jinja2')
    def personae(self):
        return self.context()

    @view_config(route_name='select_persona')
    def select_persona(self):
        slug = self.request.matchdict['slug'].upper()
        if slug not in PERSONAE:
            raise HTTPNotFound

        # set cookie and redirect
        response = HTTPFound(location=get_redirect_url(self.request))
        response.set_cookie(PERSONA_COOKIE_NAME, value=slug, max_age=ONE_YEAR)

        # set persona dimension value on GA
        # NOTE: the persona dimension has to be configured with scope 'user'
        persona_dimension_id = self.settings.get('ga.persona_dimension_id')
        if persona_dimension_id:
            self.request.google_analytics[persona_dimension_id] = slug

        return response

{'fraction_non_alphanumeric': 0.08640519020937776, 'fraction_numerical': 0.005308168681804777, 'mean_word_length': 3.7374301675977653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2852110', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1031, 'n_words': 218}