-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1197 from cityofaustin/jc-last-seen
Adds basic user analytics tracking
- Loading branch information
Showing
8 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
moped-database/migrations/1699408061245_create_table_public_moped_user_events/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DROP TRIGGER set_user_last_seen_date_trigger; | ||
|
||
DROP FUNCTION set_user_last_seen_date; | ||
|
||
DROP TABLE "public"."moped_user_events"; | ||
|
||
ALTER TABLE "public"."moped_users" DROP COLUMN "last_seen_date"; |
38 changes: 38 additions & 0 deletions
38
moped-database/migrations/1699408061245_create_table_public_moped_user_events/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
ALTER TABLE "public"."moped_users" | ||
ADD COLUMN "last_seen_date" timestamptz NULL; | ||
|
||
COMMENT ON COLUMN moped_users.last_seen_date IS 'Tracks the last time a user loaded the Moped app in their browser. This value is set by the set_last_seen_date function. This value is not 100% reliable because it is updated by an API call that can be blocked by the client.'; | ||
|
||
|
||
CREATE TABLE "public"."moped_user_events" ( | ||
"id" serial NOT NULL, | ||
"user_id" integer NOT NULL, | ||
"event_name" text NOT NULL CHECK (event_name in('app_load')), | ||
"created_at" timestamptz NOT NULL DEFAULT now(), | ||
PRIMARY KEY ("id"), | ||
FOREIGN KEY ("user_id") REFERENCES "public"."moped_users" ("user_id") ON UPDATE CASCADE ON DELETE CASCADE | ||
); | ||
|
||
COMMENT ON TABLE "public"."moped_user_events" IS E'Tracks user activities for analytics purposes'; | ||
|
||
CREATE FUNCTION public.set_user_last_seen_date () | ||
RETURNS TRIGGER | ||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
UPDATE | ||
moped_users | ||
SET | ||
last_seen_date = NEW.created_at | ||
WHERE | ||
user_id = NEW.user_id; | ||
RETURN NEW; | ||
END; | ||
$$; | ||
|
||
CREATE TRIGGER set_user_last_seen_date_trigger | ||
AFTER INSERT ON public.moped_user_events | ||
FOR EACH ROW | ||
EXECUTE FUNCTION public.set_user_last_seen_date (); | ||
|
||
COMMENT ON FUNCTION set_user_last_seen_date IS 'Updates the moped_users.last_seen_date based on inserts into the moped_user_events table'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect } from "react"; | ||
import { useMutation } from "@apollo/client"; | ||
import { INSERT_USER_EVENT } from "src/queries/staff"; | ||
import { useUser } from "src/auth/user"; | ||
/** | ||
* This wrapper calls the set_last_seen endpoint, which updates the user's | ||
* last_seen_date timestamp based on their session ID. this action happens | ||
* once each time this component mounts | ||
*/ | ||
export default function ActivityMetrics({ children }) { | ||
const { user } = useUser(); | ||
const [setLastSeen] = useMutation(INSERT_USER_EVENT, { | ||
variables: { event_name: "app_load" }, | ||
}); | ||
const isLoggedIn = !!user; | ||
useEffect(() => { | ||
if (!isLoggedIn) { | ||
return; | ||
} | ||
setLastSeen().catch((error) => { | ||
console.error( | ||
"Failed to set the last seen date for the current user.", | ||
error | ||
); | ||
}); | ||
}, [setLastSeen, isLoggedIn]); | ||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters