Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1978 from maple3142/master
Feature: firestore visitor counter
- Loading branch information
Showing
3 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{% if theme.firestore.enable %} | ||
<script src="https://www.gstatic.com/firebasejs/4.6.0/firebase.js"></script> | ||
<script src="https://www.gstatic.com/firebasejs/4.6.0/firebase-firestore.js"></script> | ||
<script> | ||
(function(){ | ||
var title='{{ page.title }}' | ||
if(title){ | ||
|
||
firebase.initializeApp({ | ||
apiKey: '{{ theme.firestore.apiKey }}', | ||
projectId: '{{ theme.firestore.projectId }}' | ||
}) | ||
|
||
|
||
var db = firebase.firestore() | ||
var articles=db.collection('{{ theme.firestore.collection }}') | ||
var doc=articles.doc(title) | ||
|
||
doc.get().then(function(d){ | ||
var count | ||
if(!d.exists){ //has no data, initialize count | ||
doc.set({ | ||
count: 1 | ||
}) | ||
count=1 | ||
} | ||
else{ //has data | ||
count=d.data().count | ||
if(!(window.localStorage&&window.localStorage.getItem(title))){ //if first view this article | ||
doc.set({ //increase count | ||
count: count+1 | ||
}) | ||
count++ | ||
} | ||
} | ||
if(window.localStorage){ //mark as visited | ||
localStorage.setItem(title,true) | ||
} | ||
|
||
//append to meta | ||
$('.post-meta').append( | ||
$('<span>').addClass('post-visitors-count').append( | ||
$('<span>').addClass('post-meta-divider').text('|') | ||
).append( | ||
$('<span>').addClass('post-meta-item-icon').append( | ||
$('<i>').addClass('fa fa-users') | ||
) | ||
).append($('<span>').text('{{ __("post.visitors")}} '+count)) | ||
) | ||
}) | ||
} | ||
})() | ||
</script> | ||
{% endif %} |