A browser client for simulating client-side tracking.
| 158 | |
| 159 | |
| 160 | class SimBrowserClient(SimClient): |
| 161 | """A browser client for simulating client-side tracking.""" |
| 162 | |
| 163 | LIB_NAME = "web" |
| 164 | |
| 165 | # Parent |
| 166 | person: "SimPerson" |
| 167 | |
| 168 | # Properties |
| 169 | device_id: str |
| 170 | device_type: str |
| 171 | os: str |
| 172 | browser: str |
| 173 | |
| 174 | # State |
| 175 | active_distinct_id: str |
| 176 | active_session_id: Optional[str] |
| 177 | super_properties: Properties |
| 178 | current_url: Optional[str] |
| 179 | is_logged_in: bool |
| 180 | |
| 181 | def __init__(self, person: "SimPerson"): |
| 182 | self.person = person |
| 183 | self.matrix = person.cluster.matrix |
| 184 | self.device_type, self.os, self.browser = self.person.cluster.properties_provider.device_type_os_browser() |
| 185 | self.device_id = str(UUID(int=self.person.cluster.random.getrandbits(128))) |
| 186 | self.active_distinct_id = self.device_id # Pre-`$identify`, the device ID is used as the distinct ID |
| 187 | self.active_session_id = None |
| 188 | self.super_properties = {} |
| 189 | self.current_url = None |
| 190 | self.is_logged_in = False |
| 191 | |
| 192 | def __enter__(self): |
| 193 | """Start session within client.""" |
| 194 | self.active_session_id = str(self.person.cluster.roll_uuidt()) |
| 195 | |
| 196 | def __exit__(self, exc_type, exc_value, exc_traceback): |
| 197 | """End session within client. Handles `$pageleave` event.""" |
| 198 | if self.current_url is not None: |
| 199 | self.capture(EVENT_PAGELEAVE) |
| 200 | self.current_url = None |
| 201 | |
| 202 | def _get_person(self, _: str): |
| 203 | return self.person |
| 204 | |
| 205 | def capture(self, event: str, properties: Optional[Properties] = None): |
| 206 | """Capture an arbitrary event. Similar to JS `posthog.capture()`.""" |
| 207 | combined_properties: Properties = { |
| 208 | "$device_type": self.device_type, |
| 209 | "$os": self.os, |
| 210 | "$browser": self.browser, |
| 211 | "$session_id": self.active_session_id, |
| 212 | "$device_id": self.device_id, |
| 213 | } |
| 214 | if self.super_properties: |
| 215 | combined_properties.update(self.super_properties) |
| 216 | if self.current_url is not None: |
| 217 | parsed_current_url = urlparse(self.current_url) |
no outgoing calls
no test coverage detected
searching dependent graphs…