分析与统计 #
分析概述 #
Algolia提供丰富的分析功能,帮助你:
- 监控搜索性能
- 了解用户行为
- 发现优化机会
- 评估搜索效果
启用分析 #
搜索时启用 #
javascript
const results = await index.search('iphone', {
analytics: true, // 启用分析(默认true)
clickAnalytics: true // 启用点击分析
});
添加分析标签 #
javascript
const results = await index.search('iphone', {
analytics: true,
analyticsTags: ['mobile', 'homepage'] // 自定义标签
});
搜索分析 #
获取搜索统计 #
javascript
const analytics = client.initAnalytics();
const stats = await analytics.getSearchesCount({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31'
});
console.log(stats);
热门搜索 #
javascript
const topSearches = await analytics.getTopSearches({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31',
limit: 10
});
console.log(topSearches.searches);
// [
// { search: "iphone", count: 15000, withFilterCount: 5000 },
// { search: "samsung", count: 12000, withFilterCount: 3000 },
// ...
// ]
无结果搜索 #
javascript
const noResultSearches = await analytics.getSearchesNoResults({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31',
limit: 10
});
console.log(noResultSearches.searches);
// [
// { search: "pixel", count: 2000 },
// { search: "xiaomi", count: 1500 },
// ...
// ]
点击分析 #
发送点击事件 #
javascript
// 搜索时获取queryID
const results = await index.search('iphone', {
clickAnalytics: true
});
const queryID = results.queryID;
// 用户点击结果时发送事件
await index.sendEvents([
{
eventType: 'click',
eventName: 'Product Clicked',
index: 'products',
userToken: 'user-123',
objectIDs: ['prod-001'],
queryID: queryID,
positions: [1]
}
]);
发送转化事件 #
javascript
await index.sendEvents([
{
eventType: 'conversion',
eventName: 'Purchase',
index: 'products',
userToken: 'user-123',
objectIDs: ['prod-001'],
queryID: queryID
}
]);
获取点击统计 #
javascript
const clickStats = await analytics.getClickPositions({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31'
});
console.log(clickStats);
// {
// mean: 2.5,
// distribution: { "1": 5000, "2": 3000, "3": 2000, ... }
// }
用户分析 #
用户统计 #
javascript
const userStats = await analytics.getUsersCount({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31'
});
console.log(userStats);
用户行为分析 #
javascript
const status = await analytics.getStatus({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31'
});
console.log(status);
// {
// searches: 100000,
// users: 25000,
// rateLimitHits: 0,
// avgProcessingTime: 12
// }
分析API #
获取过滤统计 #
javascript
const filterStats = await analytics.getTopFilterAttributes({
index: 'products',
startDate: '2024-01-01',
endDate: '2024-01-31'
});
console.log(filterStats.attributes);
// [
// { attribute: "brand", count: 50000 },
// { attribute: "price", count: 30000 },
// ...
// ]
获取过滤值统计 #
javascript
const filterValues = await analytics.getTopFiltersForAttribute({
index: 'products',
attribute: 'brand',
startDate: '2024-01-01',
endDate: '2024-01-31'
});
console.log(filterValues.values);
// [
// { value: "Apple", count: 20000 },
// { value: "Samsung", count: 15000 },
// ...
// ]
分析工具类 #
javascript
class AnalyticsReporter {
constructor(client, indexName) {
this.client = client;
this.indexName = indexName;
this.analytics = client.initAnalytics();
}
async getOverview(startDate, endDate) {
const [searches, users, clicks, conversions] = await Promise.all([
this.analytics.getSearchesCount({
index: this.indexName,
startDate,
endDate
}),
this.analytics.getUsersCount({
index: this.indexName,
startDate,
endDate
}),
this.analytics.getClickThroughRate({
index: this.indexName,
startDate,
endDate
}),
this.analytics.getConversionRate({
index: this.indexName,
startDate,
endDate
})
]);
return {
searches: searches.count,
users: users.count,
clickThroughRate: clicks.rate,
conversionRate: conversions.rate
};
}
async getTopSearches(limit = 10) {
const result = await this.analytics.getTopSearches({
index: this.indexName,
limit
});
return result.searches;
}
async getNoResultSearches(limit = 10) {
const result = await this.analytics.getSearchesNoResults({
index: this.indexName,
limit
});
return result.searches;
}
async getSearchTrend(startDate, endDate) {
const result = await this.analytics.getSearchesCount({
index: this.indexName,
startDate,
endDate
});
return result;
}
async generateReport(startDate, endDate) {
const overview = await this.getOverview(startDate, endDate);
const topSearches = await this.getTopSearches(10);
const noResults = await this.getNoResultSearches(10);
return {
period: { startDate, endDate },
overview,
topSearches,
noResults,
recommendations: this.generateRecommendations(overview, noResults)
};
}
generateRecommendations(overview, noResults) {
const recommendations = [];
if (overview.clickThroughRate < 0.1) {
recommendations.push({
type: 'ctr',
message: '点击率较低,建议优化排名策略'
});
}
if (noResults.length > 0) {
recommendations.push({
type: 'noResults',
message: `发现${noResults.length}个无结果搜索,建议添加同义词或补充数据`
});
}
return recommendations;
}
}
Dashboard分析 #
查看分析数据 #
- 进入Dashboard
- 选择应用和索引
- 点击"Analytics"标签
分析图表 #
text
┌─────────────────────────────────────────────────────────────┐
│ Search Analytics │
├─────────────────────────────────────────────────────────────┤
│ │
│ Total Searches: 100,000 │
│ Unique Users: 25,000 │
│ No Results Rate: 5.2% │
│ Click Through Rate: 15.3% │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Searches Over Time │ │
│ │ (搜索量趋势图) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ Top Searches: │
│ 1. iphone - 15,000 │
│ 2. samsung - 12,000 │
│ 3. laptop - 8,000 │
│ │
│ No Results Searches: │
│ 1. pixel - 2,000 │
│ 2. xiaomi - 1,500 │
│ │
└─────────────────────────────────────────────────────────────┘
优化建议 #
基于分析优化 #
javascript
async function optimizeBasedOnAnalytics(indexName) {
const analytics = client.initAnalytics();
// 获取无结果搜索
const noResults = await analytics.getSearchesNoResults({
index: indexName,
limit: 20
});
// 分析原因并生成建议
const suggestions = [];
for (const search of noResults.searches) {
// 检查是否需要添加同义词
const similarWords = await findSimilarWords(search.search);
if (similarWords.length > 0) {
suggestions.push({
type: 'synonym',
query: search.search,
suggestion: `添加同义词: ${search.search} → ${similarWords.join(', ')}`
});
}
// 检查是否需要补充数据
suggestions.push({
type: 'data',
query: search.search,
suggestion: `考虑添加相关产品数据`
});
}
return suggestions;
}
监控告警 #
javascript
class AnalyticsMonitor {
constructor(client, thresholds) {
this.client = client;
this.thresholds = thresholds;
}
async checkHealth(indexName) {
const analytics = this.client.initAnalytics();
const stats = await analytics.getStatus({
index: indexName
});
const alerts = [];
// 检查无结果率
const noResultRate = stats.noResultRate || 0;
if (noResultRate > this.thresholds.maxNoResultRate) {
alerts.push({
level: 'warning',
metric: 'noResultRate',
value: noResultRate,
threshold: this.thresholds.maxNoResultRate,
message: `无结果率过高: ${(noResultRate * 100).toFixed(1)}%`
});
}
// 检查响应时间
if (stats.avgProcessingTime > this.thresholds.maxResponseTime) {
alerts.push({
level: 'warning',
metric: 'responseTime',
value: stats.avgProcessingTime,
threshold: this.thresholds.maxResponseTime,
message: `响应时间过长: ${stats.avgProcessingTime}ms`
});
}
return {
healthy: alerts.length === 0,
alerts
};
}
}
导出分析数据 #
javascript
async function exportAnalytics(indexName, startDate, endDate) {
const analytics = client.initAnalytics();
const data = {
overview: await analytics.getStatus({ index: indexName, startDate, endDate }),
topSearches: await analytics.getTopSearches({ index: indexName, startDate, endDate, limit: 100 }),
noResults: await analytics.getSearchesNoResults({ index: indexName, startDate, endDate, limit: 100 }),
clicks: await analytics.getClickPositions({ index: indexName, startDate, endDate }),
filters: await analytics.getTopFilterAttributes({ index: indexName, startDate, endDate })
};
return JSON.stringify(data, null, 2);
}
总结 #
分析与统计要点:
| 要点 | 说明 |
|---|---|
| 启用 | analytics, clickAnalytics参数 |
| 搜索分析 | 热门搜索、无结果搜索 |
| 点击分析 | 点击事件、转化事件 |
| 用户分析 | 用户统计、行为分析 |
| 优化 | 基于数据优化搜索 |
接下来,让我们学习 JavaScript客户端。
最后更新:2026-03-28