Ivan
02/22/2025, 8:36 PMpopularity
and no_hit_queries
.
I have looked at this documentation and worked with Cursor+Claude to come up with the necessary code to create the various collections, rules, and events handling.
Attached are screenshots showing:
• search aggregation, no hits aggregation, and popularity rule documents in the events
collection
• a product_click event for a "Logitech - G PRO X Wireless Gaming Headset for PC - Black"
• "Logitech - G PRO X Wireless Gaming Headset for PC - Black" popularity remaining as 0
====
Any ideas why popularity
and no_hit_querie*s
may not be incrementing?*
I have also pasted additional code in the thread for reference if helful.
Thanks so much! 🙏Ivan
02/22/2025, 8:36 PMpopularity
value to 0 on creation
export const productsSchema: TypesenseCollection = {
name: 'products',
fields: [
// ... other fields ...
{ name: 'popularity', type: 'int32', optional: true, sort: true },
// ... other fields ...
],
};
Analytics rules setup
const rules: Record<string, AnalyticsRule> = {
product_popularity: {
type: "counter",
params: {
source: {
collections: [searchCollection], // searchCollection = 'products'
events: [
{
type: "click",
name: "product_click_event",
weight: 1
},
{
type: "conversion",
name: "product_purchase_event",
weight: 5
}
]
},
destination: {
collection: searchCollection,
counter_field: "popularity"
}
}
}
};
Event tracking code
async trackProductClick(productId: string, userId?: string, metadata?: any) {
return this.trackEvent('click', {
doc_id: productId,
user_id: userId,
metadata,
timestamp: Date.now()
});
}
async trackEvent(type: string, eventData: any) {
try {
switch (type) {
case 'click':
case 'conversion':
const eventDocument = {
type: eventData.type,
name: type === 'click' ? 'product_click_event' : 'product_purchase_event',
doc_id: eventData.doc_id,
user_id: eventData.user_id || 'anonymous',
timestamp: eventData.timestamp || Date.now(),
metadata: eventData.metadata
};
await this.client.collections('events').documents().create(eventDocument);
break;
}
} catch (error) {
console.error('Failed to track event:', error);
throw error;
}
}
Fanis Tharropoulos
02/24/2025, 8:41 AMtrackEvent
wrapper?Ivan
02/26/2025, 4:31 AM