-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.js
More file actions
127 lines (108 loc) · 3.8 KB
/
analytics.js
File metadata and controls
127 lines (108 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// This dotenv import is required for the `.env` file to be read
require('dotenv').config();
const sharetribeIntegrationSdk = require('sharetribe-flex-integration-sdk');
// Create rate limit handler for queries.
// NB! If you are using the script in production environment,
// you will need to use sharetribeIntegrationSdk.util.prodQueryLimiterConfig
const queryLimiter = sharetribeIntegrationSdk.util.createRateLimiter(
sharetribeIntegrationSdk.util.devQueryLimiterConfig
);
// Create rate limit handler for commands.
// NB! If you are using the script in production environment,
// you will need to use sharetribeIntegrationSdk.util.prodCommandLimiterConfig
const commandLimiter = sharetribeIntegrationSdk.util.createRateLimiter(
sharetribeIntegrationSdk.util.devCommandLimiterConfig
);
const integrationSdk = sharetribeIntegrationSdk.createInstance({
// These two env vars need to be set in the `.env` file.
clientId: process.env.SHARETRIBE_INTEGRATION_CLIENT_ID,
clientSecret: process.env.SHARETRIBE_INTEGRATION_CLIENT_SECRET,
// Pass rate limit handlers
queryLimiter: queryLimiter,
commandLimiter: commandLimiter,
// Normally you can just skip setting the base URL and just use the
// default that the `createInstance` uses. We explicitly set it here
// for local testing and development.
baseUrl: process.env.SHARETRIBE_INTEGRATION_BASE_URL || 'https://flex-integ-api.sharetribe.com',
});
const totalItems = response => {
return response.data.meta.totalItems;
};
// Let's query minimal data of different resources just to see the
// response metadata.
const minimalOptions = {
'fields.user': 'none',
'fields.listing': 'none',
'fields.transaction': 'none',
perPage: 1,
};
const now = new Date();
const currentMonthStart = new Date(now.getFullYear(), now.getMonth());
Promise.all([
integrationSdk.marketplace.show(),
// All users, listings, and transactions
//
integrationSdk.users.query(minimalOptions),
integrationSdk.listings.query(minimalOptions),
integrationSdk.transactions.query(minimalOptions),
// Listings in different states
//
integrationSdk.listings.query({
states: ['draft'],
...minimalOptions,
}),
integrationSdk.listings.query({
states: ['pendingApproval'],
...minimalOptions,
}),
integrationSdk.listings.query({
states: ['published'],
...minimalOptions,
}),
integrationSdk.listings.query({
states: ['closed'],
...minimalOptions,
}),
// New users, listings, and transactions this month
integrationSdk.users.query({
createdAtStart: currentMonthStart,
...minimalOptions,
}),
integrationSdk.listings.query({
createdAtStart: currentMonthStart,
...minimalOptions,
}),
integrationSdk.transactions.query({
createdAtStart: currentMonthStart,
...minimalOptions,
}),
]).then(([
marketplace,
users,
listings,
transactions,
draftListings,
pendingListings,
publishedListings,
closedListings,
newUsers,
newListings,
newTransactions,
]) => {
const { name } = marketplace.data.data.attributes;
console.log(`================ ${name} analytics ================`);
console.log('');
console.log(`Listings: ${totalItems(listings)}`);
console.log(` - ${totalItems(draftListings)} draft(s)`);
console.log(` - ${totalItems(pendingListings)} pending approval`);
console.log(` - ${totalItems(publishedListings)} published`);
console.log(` - ${totalItems(closedListings)} closed`);
console.log('');
console.log(`Users: ${totalItems(users)}`);
console.log(`Transactions: ${totalItems(transactions)}`);
console.log('');
console.log(`This month, starting from ${currentMonthStart.toDateString()}:`);
console.log(` - ${totalItems(newUsers)} new user(s)`);
console.log(` - ${totalItems(newListings)} new listing(s)`);
console.log(` - ${totalItems(newTransactions)} new transaction(s)`);
});