Skip to content
Permalink
Browse files

Fix path for ga

  • Loading branch information...
marcomontalbano committed Sep 19, 2019
1 parent ed21681 commit af78086546e1b83ea6ebd8125b1b2b6eb57291ff
Showing with 34 additions and 4 deletions.
  1. +6 −4 src/lambda/classes/google-ua.js
  2. +28 −0 src/lambda/classes/google-ua.test.js
@@ -1,22 +1,24 @@
const ua = require('universal-analytics');
const visitor = ua('UA-50467643-11');
const path = require('path');

const sendEvent = (...args) => {
visitor.event(...args).send();
}

const sendLambdaEvent = event => {
const sendLambdaEvent = (event, send = sendEvent) => {
const {
path,
path: functionUrl,
headers: {
referer
} = {}
} = event;

const category = `Lambda Function - ${path}`;
const functionName = path.basename(functionUrl);
const category = `Lambda Function - ${functionName}`;
const label = referer ? `referer - ${referer}` : '';

sendEvent(category, 'invoke', label);
send(category, 'invoke', label);
}

module.exports = { sendLambdaEvent };
@@ -0,0 +1,28 @@
import { sendLambdaEvent } from './google-ua';

describe('google-ua', () => {
describe('sendLambdaEvent', () => {
it('should send event without a specified referer', () => {
const mock = jest.fn();

sendLambdaEvent({
path: '/.netlify/functions/image-json',
}, mock);

expect(mock).toBeCalledWith('Lambda Function - image-json', 'invoke', '');
});

it('should send event with a specified referer', () => {
const mock = jest.fn();

sendLambdaEvent({
path: '/.netlify/functions/image-json',
headers: {
referer: 'https://example.com'
}
}, mock);

expect(mock).toBeCalledWith('Lambda Function - image-json', 'invoke', 'referer - https://example.com');
});
});
});

0 comments on commit af78086

Please sign in to comment.
You can’t perform that action at this time.