Skip to content

Adding Google Analytics

skitsanos edited this page Aug 21, 2022 · 3 revisions

Foxx Builder comes with some utilities that can help you with extending your API functionality, one of them is ga.js - a Google Analytics client; you can add it to your API router in the following way:

module.context.use((req, _res, next) =>
{
    const {runTask} = module.context;
    runTask(
        'Google Analytics PageView recording',
        'ga',
        {
            clientId: req.headers['x-user-id'],
            path: req.path,
            headers: req.headers
        });

    next();
});

It runs as ArangoDB tasks via runTask utility accessible within module.context and depends on googleAnalyticsId variable from your manifest.json file configuration section:

{
    "configuration": {

    "googleAnalyticsId": {
      "default": "G-Y32FMJEM1W",
      "type": "string",
      "description": "Google Analytics Measurement ID"
    }
  }
}

You can set googleAnalyticsId value in manifest.json directly or via web interface if it is accessible.

The minimal required payload for qa.js task is the path - request path, and the headers - request headers that will be used to extract the request IP address and User-Agent.

Tring out

This feature is available as an example in feat/google-analytics branch: https://github.com/skitsanos/foxx-builder/tree/feat/google-analytics

Clone this wiki locally