Skip to content

Commit 98412fe

Browse files
docs: update Firestore visitor counter setup
1 parent b71f79b commit 98412fe

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

source/docs/third-party-services/statistics-and-analytics.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,57 +116,58 @@ plausible:
116116

117117
#### Firebase
118118

119-
Firebase Analytics provides the functionality of visitor statistics.
119+
Cloud Firestore provides the functionality of visitor statistics without loading the Firebase JavaScript SDK.
120120

121121
{% tabs firestore %}
122-
<!-- tab Get apiKey & projectId → -->
123-
Login to [Firebase](https://console.firebase.google.com/u/0/) to get apiKey and projectId. The Web API Key gets generated once you go into the "Authentication" section for the first time.
124-
125-
![Firebase](/images/firebase-1.png)
126-
![Firebase](/images/firebase.png)
127-
128-
[More detailed documentation](https://firebase.google.com/docs/firestore/)
129-
<!-- endtab -->
130-
131122
<!-- tab Create Firestore Database → -->
132123
1. Create a Firestore database in Firebase console.
133124

134125
![Firestore](/images/firestore-1.png)
135126
![Firestore](/images/firestore-2.png)
136127

137-
2. Apply these rules in Firestore Database Rules:
128+
1. Apply these rules in Firestore Database Rules:
138129

139130
```js
140131
rules_version = '2';
141132
service cloud.firestore {
142133
match /databases/{database}/documents {
143-
match /articles/{document=**} {
144-
allow read, create: if true;
145-
allow write: if request.resource.data.count==resource.data.count+1;
146-
}
147-
match /{document=**} {
148-
allow read, write: if false;
134+
match /articles/{document} {
135+
allow read: if true;
136+
allow create: if request.resource.data.keys().hasOnly(['count'])
137+
&& request.resource.data.count is int
138+
&& request.resource.data.count == 1;
139+
allow update: if request.resource.data.diff(resource.data).affectedKeys().hasOnly(['count'])
140+
&& request.resource.data.count is int
141+
&& request.resource.data.count == resource.data.count + 1;
142+
allow delete: if false;
149143
}
150144
}
151145
}
152146
```
153147

154-
This configuration allows anyone to read the visitor count but prevents unauthorized writes to the database.
148+
Replace `articles` if you use a different collection name. These rules allow anonymous reads and count increments while preventing changes to other fields and document deletion. They cannot prevent artificial increments or quota abuse; use a trusted backend if stronger protection is required.
155149

156150
![Firestore](/images/firestore-3.png)
157-
<!-- endtab -->
151+
<!-- endtab -->
158152

159153
<!-- tab NexT Config -->
160-
Edit {% label primary@NexT config file %} and add or change `firestore` section:
154+
Find your Project ID in Firebase console under `Project settings → General`, then edit {% label primary@NexT config file %} and add or change the `firestore` section:
155+
156+
![Firebase Project ID](/images/firebase.png)
157+
158+
Only the Project ID is required. The Web API Key shown on the same page is not used by NexT.
161159

162160
```yml NexT config file
163161
firestore:
164162
enable: true
165163
collection: articles #required, a string collection name to access firestore database
166-
apiKey: #required
167164
projectId: #required
168165
```
169-
Note: Firestore visitor counting tracks unique visitors (UV) only. The count won't increase on page refresh as it uses browser's localStorage. To test the counter:
166+
167+
An API key is not required. NexT uses the [Cloud Firestore REST API](https://firebase.google.com/docs/firestore/use-rest-api), and anonymous requests are authorized by your Firestore Security Rules.
168+
169+
Note: Firestore visitor counting records at most one visit per article and browser profile because it uses the browser's localStorage. It is not a reliable unique-visitor metric. To test the counter:
170+
170171
- Clear localStorage in Developer Tools
171172
- Use incognito/private mode
172173
- Use different browsers

source/images/firebase-1.png

-239 KB
Binary file not shown.

0 commit comments

Comments
 (0)