Giter VIP home page Giter VIP logo

ga's Introduction

ga

oak ci deno doc

Utilities for server side processing of Google Analytics in Deno CLI and Deploy.

When you server side render pages, it can be more efficient to not add Google Analytics to the client side app, and instead send the messages directly via your edge worker. This library provides a framework for doing this.

Usage

The library is designed to generate a measure message for each request and response handled by a Deno CLI or Deno Deploy server. These messages are then queued up and asynchronously batched to Google Analytics.

createReporter()

If you are using the Deno HTTP APIs directly, std/http, or various other HTTP frameworks, createReporter() will return a function which can be used to dispatch messages to Google Analytics.

You need to create the reporter function, and then call the reporter with information about the current request and response.

import { createReporter } from "https://deno.land/x/g_a/mod.ts";

const reporter = createReporter();

If you are using the low-level Deno API for HTTP servers, usage of the reporter would look something like this:

import { createReporter } from "https://deno.land/x/g_a/mod.ts";

const ga = createReporter();

for await (const conn of Deno.listen({ port: 0 })) {
  (async () => {
    const httpConn = Deno.serveHttp(conn);
    for await (const requestEvent of httpConn) {
      let err;
      const start = performance.now();
      try {
        // processing of the request...
        const response = new Response(/* response details */);
        await requestEvent.respondWith(response);
      } catch (e) {
        err = e;
      } finally {
        await ga(requestEvent.request, conn, response, start, err);
      }
    }
  })();
}

If you are using the std library HTTP API then it would look something like this:

import { createReporter } from "https://deno.land/x/g_a/mod.ts";
import { serve } from "https://deno.land/std/http/server.ts";
import type { ConnInfo } from "https://deno.land/std/http/server.ts";

const ga = createReporter();

function handler(req: Request, conn: ConnInfo) {
  let err;
  let res: Response;
  const start = performance.now();
  try {
    // processing of the request...
    res = new Response(/* response details */);
  } catch (e) {
    err = e;
  } finally {
    ga(req, conn, res!, start, err);
  }
  return res!;
}

serve(handler);

createReportMiddleware()

If you are using oak, then createReportMiddleware() can be used to create a middleware which will do the job:

import { createReportMiddleware } from "https://deno.land/x/g_a/mod.ts";
import { Application } from "https://deno.land/x/oak/mod.ts";

const ga = createReportMiddleware();
const app = new Application();

app.use(ga);
// register additional middleware...

app.listen({ port: 0 });

ga's People

Contributors

kitsonk avatar ko1103 avatar roj1512 avatar ultirequiem avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.