Skip to content

Commit

Permalink
Merge pull request #3345 from kbase/update-ga4-user-3
Browse files Browse the repository at this point in the history
UIP-36 use the anonymous user id in Google Analytics calls
  • Loading branch information
briehl committed Aug 28, 2023
2 parents b2688da + 1ef8ecd commit 0cc1432
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 24 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This is built on the Jupyter Notebook v6.4.12 and IPython 8.5.0 (more notes will
- PTV-1810 - address object name display issues in the View Configure tab of app cells. This now saves all app inputs as UPAs in the cell. It also includes an update to input transforms to properly convert from UPAs <-> names or references as appropriate before starting the app.
- PTV-1877 - fix app descriptions to replace the documentation link for the upload / download guide
- PTV-1878 - fix some failing front end unit tests
- UIP-36 - add support for anonymous user ids sent to Google Analytics

### Dependency Changes

Expand Down
4 changes: 2 additions & 2 deletions kbase-extension/kbase_templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '{{google_analytics_id}}', {
'username': '{{userName}}',
'username': '{{anon_user_id}}',
'Page_location': document.location,
'page_path': document.location.pathname,
'page_title': document.location.pathname
});
gtag('set', {'user_id': '{{userName}}'});
gtag('set', {'user_id': '{{anon_user_id}}'});
{% if google_ad_id %}
gtag('config', '{{google_ad_id}}');
gtag('event', 'conversion', {'send_to': '{{google_ad_id}}'+'{{google_ad_conversion}}'});
Expand Down
3 changes: 2 additions & 1 deletion src/biokbase/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def get_token_info(token: str) -> TokenInfo:
return token_info


def init_session_env(token_info: TokenInfo, ip: str) -> None:
def init_session_env(token_info: TokenInfo, user_info: UserInfo, ip: str) -> None:
"""
Initializes the internal session environment.
Parameters:
Expand All @@ -111,6 +111,7 @@ def init_session_env(token_info: TokenInfo, ip: str) -> None:
set_environ_token(token_info.token)
kbase_env.session = token_info.id
kbase_env.user = token_info.user_name
kbase_env.anon_user_id = user_info.anon_user_id
kbase_env.client_ip = ip


Expand Down
2 changes: 2 additions & 0 deletions src/biokbase/narrative/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class _KBaseEnv:
env_workspace = "KB_WORKSPACE_ID"
env_user = "KB_USER_ID"
env_env = "KB_ENVIRONMENT"
env_anon_user_id = "KB_ANON_USER_ID"

_defaults = {
"auth_token": "none",
Expand All @@ -41,6 +42,7 @@ class _KBaseEnv:
"user": "anonymous",
"workspace": "none",
"env": "none",
"anon_user_id": "none",
}

def __getattr__(self, name):
Expand Down
17 changes: 13 additions & 4 deletions src/biokbase/narrative/handlers/authhandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from notebook.auth.logout import LogoutHandler
from traitlets.config import Application

from biokbase.auth import get_token_info, init_session_env, set_environ_token
from biokbase.auth import (
get_token_info,
get_user_info,
init_session_env,
set_environ_token,
)
from biokbase.narrative.common.kblogging import get_logger, log_event
from biokbase.narrative.common.util import kbase_env

Expand Down Expand Up @@ -52,7 +57,8 @@ def get(self):
if auth_cookie:
token = urllib.parse.unquote(auth_cookie.value)
try:
token_info = get_token_info(token)
auth_info = get_token_info(token)
user_info = get_user_info(token)
except Exception:
app_log.error(
"Unable to get user information from authentication token!"
Expand All @@ -65,7 +71,7 @@ def get(self):
# app_log.debug("KBaseLoginHandler.get: user_id={uid} token={tok}"
# .format(uid=token_info.get('user', 'none'),
# tok=token))
init_session_env(token_info, client_ip)
init_session_env(auth_info, user_info, client_ip)
self.current_user = kbase_env.user
log_event(
g_log, "session_start", {"user": kbase_env.user, "user_agent": ua}
Expand Down Expand Up @@ -99,7 +105,10 @@ def password_from_settings(cls, settings):

@classmethod
def login_available(cls, settings):
"""Whether this LoginHandler is needed - and therefore whether the login page should be displayed."""
"""
Whether this LoginHandler is needed - and therefore whether the login page should be
displayed.
"""
return True


Expand Down
6 changes: 3 additions & 3 deletions src/biokbase/narrative/handlers/narrativehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tornado import web
from traitlets.config import Application

from biokbase.auth import get_token_info, init_session_env
from biokbase.auth import get_token_info, get_user_info, init_session_env
from biokbase.narrative.common.kblogging import get_logger, log_event
from biokbase.narrative.common.url_config import URLS
from biokbase.narrative.common.util import kbase_env
Expand Down Expand Up @@ -35,7 +35,7 @@ def _init_session(request, cookies):
reason="Authorization required for Narrative access",
)
if token != kbase_env.auth_token:
init_session_env(get_token_info(token), client_ip)
init_session_env(get_token_info(token), get_user_info(token), client_ip)
log_event(g_log, "session_start", {"user": kbase_env.user, "user_agent": ua})


Expand Down Expand Up @@ -83,7 +83,7 @@ def get(self, path):
kill_kernel=False,
mathjax_url=self.mathjax_url,
google_analytics_id=URLS.google_analytics_id,
userName=kbase_env.user,
anon_user_id=kbase_env.anon_user_id,
google_ad_id=URLS.google_ad_id,
google_ad_conversion=URLS.google_ad_conversion,
)
Expand Down
8 changes: 4 additions & 4 deletions src/biokbase/narrative/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import time
from typing import Union

import biokbase.auth
import biokbase.narrative.clients as clients
from biokbase.auth import get_auth_token, get_user_info


def strict_system_variable(var: str) -> Union[str, int]:
Expand Down Expand Up @@ -49,12 +49,12 @@ def system_variable(var: str) -> Union[str, int, None]:
except Exception:
return None
elif var == "user_id":
token = biokbase.auth.get_auth_token()
token = get_auth_token()
if token is None:
return None
try:
user_info = biokbase.auth.get_user_info(token)
return user_info.get("user")
user_info = get_user_info(token)
return user_info.user_name
except Exception:
return None
# TODO: make this better with more exception handling.
Expand Down
8 changes: 6 additions & 2 deletions src/biokbase/narrative/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from biokbase.auth import (
TokenInfo,
UserInfo,
get_agent_token,
get_auth_token,
get_display_names,
Expand Down Expand Up @@ -144,14 +145,17 @@ def test_init_session_env():
ip = "127.0.0.1"
token_id = "some-token-id"
user = "kbase_user"
anonymous_id = "some_anon_id"

token_info = TokenInfo({"id": token_id, "user": user}, token=token)
init_session_env(token_info, ip)
user_info = UserInfo({"user": user, "anonid": anonymous_id})
init_session_env(token_info, user_info, ip)
assert get_auth_token() == token
assert kbase_env.session == token_info.id
assert kbase_env.user == token_info.user_name
assert kbase_env.client_ip == ip
init_session_env(TokenInfo({}), None)
assert kbase_env.anon_user_id == anonymous_id
init_session_env(TokenInfo({}), UserInfo({}), None)


def test_get_agent_token_ok(mock_token_endpoint):
Expand Down
18 changes: 10 additions & 8 deletions src/biokbase/narrative/tests/test_system.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
import biokbase.auth
from unittest import mock
from biokbase.narrative.system import (
strict_system_variable,
system_variable
)
from .narrative_mock.mockclients import get_mock_client
import time
from . import util
from unittest import mock

import pytest

import biokbase.auth
from biokbase.narrative.system import strict_system_variable, system_variable

from . import util
from .narrative_mock.mockclients import get_mock_client

user_token = "SOME_TOKEN"
config = util.ConfigTests()
user_id = config.get("users", "test_user")
Expand Down Expand Up @@ -82,9 +82,11 @@ def test_strict_sys_var_user_ok():
if user_token:
biokbase.auth.set_environ_token(user_token)
assert strict_system_variable("user_id") == user_id
biokbase.auth.set_environ_token(None)

def test_strict_sys_var_user_bad():
biokbase.auth.set_environ_token(bad_fake_token)
with pytest.raises(ValueError, match='Unable to retrieve system variable: "user_id"') as e:
strict_system_variable("user_id")
assert e
biokbase.auth.set_environ_token(None)

0 comments on commit 0cc1432

Please sign in to comment.