RE: Popularity & No Hits Counting Hey guys! I am l...
# community-help
i
> RE: Popularity & No Hits Counting Hey guys! I am looking to make my events tracking more robust. I am coming back to an issue I had when I first started on this journey, specifically, having issues with successfully tracking
popularity
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! 🙏
Products collection schema: • I have also explicitly set the
popularity
value to 0 on creation
Copy code
export const productsSchema: TypesenseCollection = {
  name: 'products',
  fields: [
    // ... other fields ...
    { name: 'popularity', type: 'int32', optional: true, sort: true },
    // ... other fields ...
  ],
};
Analytics rules setup
Copy code
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
Copy 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;
  }
}
f
Could you spin up a local repository using a docker image and link it here? It would be pretty useful for us debugging this, since it's a more complicated use-case involving a full-stack project. Also, are there any errors being thrown from your
trackEvent
wrapper?
i
Hey @Fanis Tharropoulos ! Thanks for your response. I will attempt to do a little more debugging this week(end) on this specifically and if I'm unsuccessful, will get the docker image your way and message back here. Cheers!!
🙌 1