Skip to content

Commit

Permalink
Fix GA events
Browse files Browse the repository at this point in the history
  • Loading branch information
bingneef committed May 28, 2019
1 parent d880999 commit 63d689a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
14 changes: 5 additions & 9 deletions src/components/analytics/google-analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ describe("GoogleAnalytics", () => {
});

describe("#sendEvent", () => {
it("logs if ga is not defined", () => {
it("logs if gtag is not defined", () => {
console.log = jest.fn();
window.ga = undefined;
window.gtag = undefined;

sendEvent({ eventCategory: "TestEvent", eventAction: "Sending Event" });

Expand All @@ -57,10 +57,8 @@ describe("#sendEvent", () => {
);
});

it("sends to ga if defined", () => {
window.ga = {
send: jest.fn()
};
it("sends to gtag if defined", () => {
window.gtag = jest.fn();

sendEvent({
eventCategory: "TestEvent",
Expand All @@ -69,10 +67,8 @@ describe("#sendEvent", () => {
eventValue: "EventValue"
});

expect(window.ga.send).toHaveBeenCalledWith({
hitType: "event",
expect(window.gtag).toHaveBeenCalledWith("event", "Sending Event", {
eventCategory: "TestEvent",
eventAction: "Sending Event",
eventLabel: "Event label",
eventValue: "EventValue"
});
Expand Down
10 changes: 2 additions & 8 deletions src/components/analytics/google-analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ export function sendEvent({
eventLabel,
eventValue
}: EventConfig) {
if (typeof ga === "undefined") {
if (typeof gtag === "undefined") {
// tslint:disable-next-line:no-console
console.log(
`Not sending event: ${eventCategory}:${eventAction}, not in production environment`
);
} else {
ga.send({
eventAction,
eventCategory,
eventLabel,
eventValue,
hitType: "event"
});
gtag("event", eventAction, { eventCategory, eventLabel, eventValue });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/landing/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import jump from "jump.js";
import React from "react";

import { sendEvent as sendFullStoryEvent } from "../analytics/full-story";
import { sendEvent as sendDataLayerEvent } from "../analytics/google-analytics";
import { sendEvent as sendGoogleAnalyticsEvent } from "../analytics/google-analytics";
import { sendEvent as sendMixpanelEvent } from "../analytics/mixpanel";
import { RippleButton } from "./components";
import styles from "./landing.module.scss";
Expand All @@ -12,7 +12,7 @@ function Landing() {
function navToPortfolio() {
sendFullStoryEvent("Landing CTA");
sendMixpanelEvent("Landing CTA");
sendDataLayerEvent({ eventCategory: "Landing", eventAction: "CTA" });
sendGoogleAnalyticsEvent({ eventCategory: "Landing", eventAction: "CTA" });

const $el = document.querySelector("#portfolio");
if ($el !== null) {
Expand Down
9 changes: 5 additions & 4 deletions types/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ type GAEventInterface = {
eventLabel?: string;
eventValue?: string;
};
interface GAInterface {
send: (event: GAEventInterface) => null;
}

type Smartlook = (
eventType: string,
Expand All @@ -29,4 +26,8 @@ declare module "react-ab-test";
declare var FS: FSInterface;
declare var mixpanel: MixpanelInterface;
declare var smartlook: Smartlook;
declare var ga: GAInterface;
declare var gtag: (
event: string,
action: string,
config: { [key: string]: string | undefined }
) => null;

1 comment on commit 63d689a

@vercel
Copy link

@vercel vercel bot commented on 63d689a May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.