2

I have created a Gatsby blog for my friend, which means it is serverless and does not use a database. This works quite well for a blog which will only need builds every time I make changes or he adds a blog post - however it wouldn't work too well for tracking unique user visits as it would have to do too many builds if many people visited within a short time span.

Is there a way I can track unique user visits without a database involved?

Thas Me
  • 53
  • 7
  • 3
    without a database - yes, without server side code - no - though, technically, even a file on the server to store the info is a database, albeit a simple one – Bravo Jul 20 '21 at 07:41
  • 1
    You need some sort of server-side code as far as I'm aware. Doesn't have to be directly connected to your application, it can be done with an API for example. But you can't count up each visitor without collecting it. A workaround would be not doing it yourself, but letting a third-party doing it for you, like Google Analytics. – n212 Jul 20 '21 at 07:42

1 Answers1

2

Like it has been said, you need a third-party tool (like Google Analytics) since any workaround done in the front-end part will be related to the client/browser-side, then you'll lose the tracking if a user changes the device for example.

You can easily install Analytics with some of the available plugins (like gatsby-plugin-google-gtag). This is recommended since under the hood uses gtag.js instead of analytics.js, which is what Google recommends due the last API changes.

To use is just install it by:

npm install gatsby-plugin-google-gtag // or yarn add gatsby-plugin-google-gtag

And add your configurations:

// In your gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        // You can add multiple tracking ids and a pageview event will be fired for all of them.
        trackingIds: [
          "GA-TRACKING_ID", // Google Analytics / GA
        ],
        // This object gets passed directly to the gtag config command
        // This config will be shared across all trackingIds
        gtagConfig: {
          optimize_id: "OPT_CONTAINER_ID",
          anonymize_ip: true,
          cookie_expires: 0,
        },
        // This object is used for configuration specific to this plugin
        pluginConfig: {
          // Puts tracking script in the head instead of the body
          head: false,
          // Setting this parameter is also optional
          respectDNT: true,
          // Avoids sending pageview hits from custom paths
          exclude: ["/preview/**", "/do-not-track/me/too/"],
        },
      },
    },
  ],
}

You can ignore the options you don't need.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Much appreciated, I will check this out this evening. – Thas Me Jul 20 '21 at 08:15
  • Hey, I've added this as the answer as the script has shown up in devtools just fine, however as of yet all stats are 0 even though I've visited the site. Any way I can make 100% sure it's working? Also I'm unsure what this optimise_id is? – Thas Me Jul 21 '21 at 07:04
  • Usually, GA untracks your own visits check the options to validate/invalidate this rule. – Ferran Buireu Jul 21 '21 at 07:06