Skip to content

Commit

Permalink
Try to use Cloud Firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoyanqing666 committed Nov 15, 2024
1 parent 1877177 commit 04814bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,7 @@ <h3>ByteDance</h3>
<p>And there is an interesting game written by me: <a href="game.html">Click the Ball Game 🎮</a>.</p>
</div>
</footer>

<script src="track-visitor.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions track-visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const firebaseConfig = {
apiKey: "AIzaSyA-3ixTu3oWvwF4z0Jdn5M76zte5SwEDGI",
authDomain: "personal-website-52b2e.firebaseapp.com",
projectId: "personal-website-52b2e",
storageBucket: "personal-website-52b2e.firebasestorage.app",
messagingSenderId: "1007724884899",
appId: "1:1007724884899:web:5957fdab33d460f8e9e016"
};

firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();

fetch('https://ipinfo.io/json?token=a2532467fe1742')
.then(response => response.json())
.then(data => {
const ip = data.ip;
const city = data.city;
const region = data.region;
const country = data.country;

const log = {
ip: ip,
city: city,
region: region,
country: country,
timestamp: new Date().toISOString()
};

db.collection("visitor_logs").add(log)
.then(() => {
console.log("Visitor data saved successfully");
})
.catch(error => {
console.error("Error saving visitor data: ", error);
});
})
.catch(error => {
console.error("Error fetching IP info: ", error);
});

0 comments on commit 04814bd

Please sign in to comment.