Skip to content

Commit

Permalink
Merge pull request #1978 from maple3142/master
Browse files Browse the repository at this point in the history
Feature: firestore visitor counter
  • Loading branch information
ivan-nginx committed Nov 1, 2017
2 parents 8988398 + 0935f11 commit 4613a57
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
13 changes: 11 additions & 2 deletions _config.yml
Expand Up @@ -432,14 +432,14 @@ gitment:
# Evernote,Friendfeed,Vkontakte,Odnoklassniki,Mailru
needmoreshare2:
enable: false
postbottom:
postbottom:
enable: false
options:
iconStyle: box
boxForm: horizontal
position: bottomCenter
networks: Weibo,Wechat,Douban,QQZone,Twitter,Facebook
float:
float:
enable: false
options:
iconStyle: box
Expand Down Expand Up @@ -522,6 +522,15 @@ leancloud_visitors:
app_id: #<app_id>
app_key: #<app_key>

# Another tool to show number of visitors to each article.
# visit https://console.firebase.google.com/u/0/ to get apiKey and projectId
# visit https://firebase.google.com/docs/firestore/ to get more information about firestore
firestore:
enable: false
collection: articles #required, a string collection name to access firestore database
apiKey: #required
projectId: #required

# Show PV/UV of the website/page with busuanzi.
# Get more information on http://ibruce.info/2015/04/04/busuanzi/
busuanzi_count:
Expand Down
1 change: 1 addition & 0 deletions layout/_layout.swig
Expand Up @@ -81,6 +81,7 @@
{% include '_third-party/comments/index.swig' %}
{% include '_third-party/search/index.swig' %}
{% include '_third-party/analytics/lean-analytics.swig' %}
{% include '_third-party/analytics/firestore.swig' %}
{% include '_third-party/seo/baidu-push.swig' %}
{% include '_third-party/needsharebutton.swig' %}
{% include '_third-party/rating.swig' %}
Expand Down
54 changes: 54 additions & 0 deletions layout/_third-party/analytics/firestore.swig
@@ -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 %}

0 comments on commit 4613a57

Please sign in to comment.