Segment tracking

What I’m trying to do:
App: https://quiet-droopy-pension.anvil.app
Implement Segment tracking on my app to send event when someone clicks a tab (link). Currently, it keeps returning the error “AttributeError: ‘Window’ object has no attribute ‘analytics’”.

What I’ve tried and what’s not working:

# this is a formatted code snippet.
from ._anvil_designer import MainFormTemplate
from anvil import *
from .TabForm1 import TabForm1
from .TabForm2 import TabForm2
from .TabForm3 import TabForm3
from anvil.js import window

class MainForm(MainFormTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    
  def tab_click(self, **event_args):
    window.analytics.track("trackClick")
    sender = event_args.get("sender", None)
    [....]
# paste your code between ``` 

Additional note: @stucork here is the new post, hoping it’s clearer.

Any help is appreciated, thank you

Probably best to add a clone link rather than a link to the live app
Otherwise it’s not possible to debug

My apologies, here is the cloning link: Anvil | Login

Your script is in segment_tracking.html is never loaded by the app.
Assets are not loaded by default.
The best place for that script is native libraries.

Once it’s in native libraries you’ll want to make the adjustments from the linked post.

@stucork It works, amazing. Thanks a lot for your help.

1 Like