Skip to content

Adding A New Segmenter Function

All clients have all the same functions. Functions can either be visit or visitor level. A few examples of our current functions are count_of_visitors_sales which counts the number of sales for a visitor and page_viewed_in_visit which is the pages viewed by a visit. Using these functions, we can build Segments for our visits and visitors. For example, we could make a segment for visitors who have more than five sales.

If we have a new idea for a function, for example distance from closest physical store, there are a few steps to follow, which we'll outline below. The first step is the only step we are required to do on control. Most of the work will be done on the DPS project.

Add your function to Backend

Add your new function to segmenter_function and segmenter_function_category via fixtures on the backend project. The file you'll need to update is segmentation.client.json. First add your fixture for segmenterfunction - you will need to set the function_name which will align with your DPS function in a later step, you will also need to set a data type data type. Next, add your fixture for segmenterfunctioncategory - you'll need to align your new pk set in segmenterfunction with segmenterfunctioncategory.function and also set a category level.

        {
        "model": "client.segmenterfunction",
        "pk": 54,
        "fields": {
            "display_name": "Distance from Closest Physical Store",
            "function_name": "distance_from_store",
            "data_type": 4,
            "table_name": null,
            "table_key_name": null,
            "table_value_name": null,
            "key_type_id": 0,
            "active": true,
            "created": "2023-07-26T16:00:00Z",
            "updated": "2023-07-26T16:00:00Z"
        },
        {
            "model": "client.segmenterfunctioncategory",
            "pk": 54,
            "fields": {
                "function": 54,
                "category": 1
            }
        },

Add your function to the DPS

Add your new function to rules.py on the DPS. Example below.

@staticmethod   
@metrics.distance_from_store.time
@segmenter_function
def distance_from_store(conn, visit, function, *args, **kwargs):
    rows = conn.query_dictfetchall('''
    select
        a.visit_id, a.distance
    from agg_visit_store_distance a
    where a.visit_id = %s
    limit 1
    ;
    ''', (visit.id,))    

    if len(rows) == 0:
        return None

    for row in rows:
        condition = prepare_expression(row['distance'], function)                  
        result = eval(condition)
        if result:
            break

    return result

Your function name should align with the function you set in segmenter_function.

Add your test

Using pytest we run tests against all of our functions within rules.py, to run a test, simply navigate into cd /srv/pydps/tests/segmenter and run pytest * to run checks on all functions or pytest {file}.py to check a specific file.