We are currently implementing analytics events in ...
# community-help
a
We are currently implementing analytics events in Typesense for click, add_to_cart, and purchase, each with a base weight of 1. Now, we want to introduce a
ranking_score
field that should be automatically calculated using the following formula:
Copy code
ranking_score = click + (add_to_cart * 2) + (purchase * 5)
What we need help with: How can we automatically update this
ranking_score
field
whenever new events are recorded? • Is there a way to compute this dynamically within Typesense (e.g., via analytics rules or derived fields)? • Or should this be handled externally (e.g., via backend cron/job or event pipeline)? Goal: We want
ranking_score
to always stay up-to-date and be used in:
Copy code
sort_by: ranking_score:desc
Would appreciate your guidance on the best approach for this. @Kishore Nallan
f
Hey @Akash Kumar , Yes, this should be doable directly in Typesense. You can create multiple counter analytics rules that all write to the same ranking_score field, each with a different weight. That way the field is updated automatically as events come in, and you can sort on it with sort_by=ranking_score:desc. For example:
Copy code
{
    "name": "product_click",
    "type": "counter",
    "collection": "products",
    "event_type": "click",
    "params": {
      "counter_field": "ranking_score",
      "weight": 1
    }
}
{
    "name": "product_add_to_cart",
    "type": "counter",
    "collection": "products",
    "event_type": "custom",
    "params": {
      "counter_field": "ranking_score",
      "weight": 2
    }
}
{
    "name": "product_purchase",
    "type": "counter",
    "collection": "products",
    "event_type": "custom",
    "params": {
      "counter_field": "ranking_score",
      "weight": 5
    }
}
So in practice, yes, ranking_score becomes click + add_to_cart 2 + purchase 5 without needing an external job. The main caveat is that custom event names like add_to_cart and purchase would need to be sent as custom events. Also, updates are asynchronous, so the score is automatically maintained but not strictly instant per event. It relies on the
analytics-flush-interval
server parameter
a
getting error "message": "Rule event type cannot be changed" { "name": "book_click_custom", "type": "counter", "collection": "books_collection", "event_type": "custom", "params": { "destination_collection": "books_collection", "counter_field": "ranking", "weight": 1 } }
@Fanis Tharropoulos
f
You'll have to create new ones if you already use analytics rules with those event types.
a
[ { "collection": "books_collection", "event_type": "conversion", "name": "book_add_to_cart", "params": { "counter_field": "cart_score", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "conversion", "name": "book_purchase", "params": { "counter_field": "sales_score", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "click", "name": "book_clicks", "params": { "counter_field": "popularity", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "search", "name": "popular_queries", "params": { "capture_search_requests": true, "destination_collection": "book_search_analytics", "expand_query": true, "limit": 5000 }, "rule_tag": "", "type": "popular_queries" } ]these are existing rule , where not event present of event_type = "Custom"
f
Clicks should still remain on click event types, no need to change them.
a
can you give me sample curl or payload as per my requirement it will helpful to solve fast
{ "name": "book_add_to_cart_custom", "type": "counter", "collection": "books_collection", "event_type": "custom", "params": { "destination_collection": "books_collection", "counter_field": "ranking", "weight": 1 } } for this payload same error "message": "Rule event type cannot be changed"
f
Can you share the full curl request you're sending over to Typesense? I've already outlined the full json payload of each request in my previous message
a
[ { "collection": "books_collection", "event_type": "custom", "name": "book_purchase_event_custom", "params": { "counter_field": "ranking", "destination_collection": "books_collection", "weight": 5 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "custom", "name": "book_add_tocart_event_custom", "params": { "counter_field": "ranking", "destination_collection": "books_collection", "weight": 2 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "custom", "name": "book_click_event_custom", "params": { "counter_field": "ranking", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "conversion", "name": "book_add_to_cart", "params": { "counter_field": "cart_score", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "conversion", "name": "book_purchase", "params": { "counter_field": "sales_score", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "click", "name": "book_clicks", "params": { "counter_field": "popularity", "destination_collection": "books_collection", "weight": 1 }, "rule_tag": "", "type": "counter" }, { "collection": "books_collection", "event_type": "search", "name": "popular_queries", "params": { "capture_search_requests": true, "destination_collection": "book_search_analytics", "expand_query": true, "limit": 5000 }, "rule_tag": "", "type": "popular_queries" } ] i have created this is list of rule can you verify rules are perfect or not
f
Yup, they look fine. Keep in mind you have to manually send those events out to Typesense, on your click handlers
a
yes its working as expected thanks alot