# Buggy code
def _track_printjob_event(self, event, payload):
    if not self._settings.get_boolean(["events", "printjob"]):
        return

    unique_id = self._settings.get([b"unique_id"])
    if not unique_id:
        return

    sha = hashlib.sha1()
    sha.update(payload.get("path"))
    sha.update(unique_id)

    track_event = None
    args = dict(origin=payload.get(b"origin"), file=sha.hexdigest())

    if event == Events.PRINT_STARTED:
        track_event = "print_started"
    elif event == Events.PRINT_DONE:
        try:
            elapsed = int(payload.get(b"time"))
            args[b"elapsed"] = elapsed
        except ValueError:
            pass
        track_event = "print_done"
    elif event == Events.PRINT_FAILED:
        try:
            elapsed = int(payload.get(b"time"))
            args[b"elapsed"] = elapsed
        except ValueError:
            pass
        args[b"reason"] = payload.get(b"reason", "unknown")

        if b"error" in payload and self._settings.get_boolean(["events", "commerror"]):
            args[b"commerror_text"] = payload[b"error"]

        track_event = "print_failed"
    elif event == Events.PRINT_CANCELLED:
        try:
            elapsed = int(payload.get(b"time"))
            args[b"elapsed"] = elapsed
        except ValueError:
            pass
        track_event = "print_cancelled"

    if callable(self._helpers_get_throttle_state):
        try:
            throttle_state = self._helpers_get_throttle_state(run_now=True)
            if throttle_state and (
                throttle_state.get(b"current_issue", False)
                or throttle_state.get(b"past_issue", False)
            ):
                args[b"throttled_now"] = throttle_state[b"current_issue"]
                args[b"throttled_past"] = throttle_state[b"past_issue"]
                args[b"throttled_mask"] = throttle_state[b"raw_value"]
        except:
            # ignored
            pass

    if track_event is not None:
        self._track(track_event, **args)


# Traceback (TypeError)
2019-02-25 14:42:31,564 - octoprint.startup - INFO - ******************************************************************************
2019-02-25 14:42:31,566 - octoprint.startup - INFO - Starting OctoPrint 1.3.10
2019-02-25 14:42:31,567 - octoprint.startup - INFO - ******************************************************************************
2019-02-25 14:42:31,627 - octoprint.util.connectivity_checker - INFO - Connectivity changed from offline to online
2019-02-25 14:42:31,808 - octoprint.startup - INFO - Blacklist processing done, adding 1 blacklisted plugin versions: roomtemp (any)
2019-02-25 14:42:31,853 - octoprint.plugin.core - INFO - Loading plugins from /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins, /home/wf/.octoprint/plugins and installed plugin packages...
2019-02-25 14:42:34,624 - octoprint.plugins.discovery - INFO - pybonjour is not installed, Zeroconf Discovery won't be available
2019-02-25 14:42:34,642 - octoprint.plugin.core - INFO - Found 16 plugin(s) providing 16 mixin implementations, 23 hook handlers
2019-02-25 14:42:34,645 - octoprint.server.heartbeat - INFO - Starting server heartbeat, 900.0s interval
2019-02-25 14:42:34,677 - octoprint.server - INFO - Intermediary server started
2019-02-25 14:42:34,678 - octoprint.plugin.core - INFO - Loading plugins from /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins, /home/wf/.octoprint/plugins and installed plugin packages...
2019-02-25 14:42:35,308 - octoprint.plugin.core - INFO - Found 16 plugin(s) providing 16 mixin implementations, 23 hook handlers
2019-02-25 14:42:35,351 - octoprint.filemanager.storage - INFO - Initializing the file metadata for /home/wf/.octoprint/uploads...
2019-02-25 14:42:36,019 - octoprint.filemanager.storage - INFO - ... file metadata for /home/wf/.octoprint/uploads initialized successfully.
2019-02-25 14:42:36,039 - octoprint.plugins.tracking - INFO - Initialized anonymous tracking
2019-02-25 14:42:36,099 - octoprint.plugins.softwareupdate - INFO - Loaded version cache from disk
2019-02-25 14:42:37,886 - octoprint.util.pip - INFO - Using "/home/wf/source/python/OctoPrint/venv/bin/python -m pip" as command to invoke pip
2019-02-25 14:42:39,670 - octoprint.util.pip - INFO - Version of pip is 19.0.3
2019-02-25 14:42:39,672 - octoprint.util.pip - INFO - pip installs to /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages (writable -> yes), --user flag needed -> no, virtual env -> yes
2019-02-25 14:42:39,675 - octoprint.util.pip - INFO - ==> pip ok -> yes
2019-02-25 14:42:39,682 - octoprint.plugin.core - INFO - Initialized 16 plugin implementation(s)
2019-02-25 14:42:39,694 - octoprint.plugin.core - INFO - 16 plugin(s) registered with the system:
|  Action Command Prompt Support (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/action_command_prompt
|  Announcement Plugin (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/announcements
|  Anonymous Usage Tracking (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/tracking
|  Application Keys Plugin (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/appkeys
|  Backup & Restore (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/backup
|  Core Wizard (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/corewizard
|  CuraEngine (<= 15.04) (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/cura
|  Discovery (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/discovery
|  Force Login (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/forcelogin
|  Logging (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/logging
|  Octolapse (0.3.4) = /home/wf/source/python/OctoPrint/venv/local/lib/python2.7/site-packages/octoprint_octolapse
|  Pi Support Plugin (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/pi_support
|  Plugin Manager (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/pluginmanager
|  Printer Safety Check (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/printer_safety_check
|  Software Update (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/softwareupdate
|  Virtual Printer (bundled) = /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/virtual_printer
2019-02-25 14:42:39,714 - octoprint.environment - INFO - Detected environment is Python 2.7.13 under Linux (linux2). Details:
|  hardware:
|    cores: 4
|    freq: 1400.0
|    ram: 972230656
|  os:
|    id: linux
|    platform: linux2
|  plugins:
|    pi_support:
|      model: Raspberry Pi 3 Model B Plus Rev 1.3
|  python:
|    pip: 19.0.3
|    version: 2.7.13
|    virtualenv: /home/wf/source/python/OctoPrint/venv
2019-02-25 14:42:39,726 - octoprint.server - INFO - Reset webasset folder /home/wf/.octoprint/generated/webassets...
2019-02-25 14:42:39,748 - octoprint.server - INFO - Reset webasset folder /home/wf/.octoprint/generated/.webassets-cache...
2019-02-25 14:42:39,759 - octoprint.plugins.octolapse - INFO - Octolapse is loading assets.
2019-02-25 14:42:40,125 - octoprint.server - INFO - Shutting down intermediary server...
2019-02-25 14:42:40,401 - octoprint.server - INFO - Intermediary server shut down
2019-02-25 14:42:40,404 - octoprint.events - INFO - Processing startup event, this is our first event
2019-02-25 14:42:40,405 - octoprint.events - INFO - Adding 1 events to queue that were held back before startup event
2019-02-25 14:42:40,408 - octoprint.filemanager - INFO - Adding backlog items from all storage types to analysis queue...
2019-02-25 14:42:40,432 - octoprint.plugins.pluginmanager - INFO - Loaded plugin repository data from disk, was still valid
2019-02-25 14:42:40,794 - octoprint.filemanager - INFO - Added 0 items from storage type "local" to analysis queue
2019-02-25 14:42:41,005 - octoprint.plugins.discovery - INFO - Registered OctoPrint instance on pito for SSDP
2019-02-25 14:42:41,007 - octoprint.plugins.pluginmanager - INFO - Loaded notice data from disk, was still valid
2019-02-25 14:42:41,381 - octoprint.server - INFO - Listening on http://[::]:5000
2019-02-25 14:42:41,385 - octoprint.plugins.octolapse - INFO - Loading existing settings file from: /home/wf/.octoprint/data/octolapse/settings.json.
2019-02-25 14:42:41,503 - octoprint.server.util.sockjs - INFO - New connection from client: 2.0.0.15
2019-02-25 14:42:41,712 - octoprint.plugins.pluginmanager - INFO - Loaded plugin repository data from disk, was still valid
2019-02-25 14:42:42,084 - octoprint.plugins.pluginmanager - INFO - Loaded notice data from disk, was still valid
2019-02-25 14:42:42,144 - octoprint.server.util.flask - INFO - Passively logging in user seppladmin from 2.0.0.15
2019-02-25 14:42:42,640 - octoprint.server.util.sockjs - INFO - User seppladmin logged in on the socket from client 2.0.0.15
2019-02-25 14:42:43,920 - octoprint.util.pip - INFO - Using "/home/wf/source/python/OctoPrint/venv/bin/python -m pip" as command to invoke pip
2019-02-25 14:42:43,922 - octoprint.util.pip - INFO - pip installs to /home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages (writable -> yes), --user flag needed -> no, virtual env -> yes
2019-02-25 14:42:43,923 - octoprint.util.pip - INFO - ==> pip ok -> yes
2019-02-25 14:42:43,994 - octoprint.server.preemptive_cache - INFO - Preemptively caching / (ui _default) for {'query_string': 'l10n=de', 'path': '/', 'base_url': 'http://pito:5000/'}
2019-02-25 14:42:44,217 - octoprint.plugins.tracking - INFO - Sent tracking event startup, payload: {'python': '2.7.13', 'ram': 972230656, 'version': u'1.3.10', 'pip': '19.0.3', 'cores': 4, 'freq': 1400.0, 'pi_model': 'Raspberry Pi 3 Model B Plus Rev 1.3', 'os': 'linux'}
2019-02-25 14:42:57,877 - octoprint.server.util.flask - INFO - Passively logging in user seppladmin from 2.0.0.15
2019-02-25 14:43:04,426 - octoprint.server.preemptive_cache - INFO - ... done in 20.43s
2019-02-25 14:43:04,577 - octoprint.server.util.sockjs - INFO - Client connection closed: 2.0.0.15
2019-02-25 14:43:11,044 - octoprint.server.util.sockjs - INFO - New connection from client: 2.0.0.15
2019-02-25 14:43:11,161 - octoprint.server.util.flask - INFO - Passively logging in user seppladmin from 2.0.0.15
2019-02-25 14:43:14,697 - octoprint.server.util.sockjs - INFO - User seppladmin logged in on the socket from client 2.0.0.15
2019-02-25 14:47:12,904 - octoprint.util.comm - INFO - Changing monitoring state from "Offline" to "Opening serial port"
2019-02-25 14:47:12,919 - octoprint.util.comm - INFO - Changing monitoring state from "Opening serial port" to "Connecting"
2019-02-25 14:47:12,928 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2019-02-25 14:47:13,820 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2019-02-25 14:47:16,355 - octoprint.util.comm - INFO - Changing monitoring state from "Connecting" to "Operational"
2019-02-25 14:47:16,369 - octoprint.util.comm - INFO - M110 detected, setting current line number to 0
2019-02-25 14:47:16,400 - octoprint.util.comm - INFO - Printer reports firmware name "Marlin 1.1.8 (Github)"
2019-02-25 14:47:16,426 - octoprint.util.comm - INFO - Firmware states that it supports temperature autoreporting
2019-02-25 14:47:16,616 - octoprint.plugins.tracking - INFO - Sent tracking event printer_connected, payload: {u'printer_baudrate': 250000, u'printer_port': u'/dev/ttyUSB0', 'firmware_name': 'Marlin 1.1.8 (Github)'}
2019-02-25 14:50:56,243 - octoprint.util.comm - INFO - Printer seems to support the busy protocol, will adjust timeouts and set busy interval accordingly
2019-02-25 14:51:06,284 - octoprint.util.comm - INFO - Changing monitoring state from "Operational" to "Printing from SD"
2019-02-25 14:51:06,298 - octoprint.util.comm - INFO - Telling the printer to set the busy interval to our "communicationBusy" timeout - 1s = 2s
2019-02-25 14:51:06,602 - octoprint.plugins.tracking - INFO - Sent tracking event print_started, payload: {'origin': 'sdcard', 'file': '25ec113ccb80bcf07f11f3a9025374d1b3f4eed7'}
2019-02-25 14:57:34,648 - octoprint.server.heartbeat - INFO - Server heartbeat <3
2019-02-25 14:57:44,164 - octoprint.plugins.tracking - INFO - Sent tracking event ping, payload: {}
2019-02-25 15:00:33,172 - octoprint.util.comm - INFO - Changing monitoring state from "Printing from SD" to "Finishing"
2019-02-25 15:00:33,206 - octoprint.plugin - ERROR - Error while calling plugin tracking
Traceback (most recent call last):
  File "/home/wf/source/python/OctoPrint/venv/local/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugin/__init__.py", line 230, in call_plugin
    result = getattr(plugin, method)(*args, **kwargs)
  File "/home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/tracking/__init__.py", line 119, in on_event
    self._track_printjob_event(event, payload)
  File "/home/wf/source/python/OctoPrint/venv/lib/python2.7/site-packages/OctoPrint-1.3.10-py2.7.egg/octoprint/plugins/tracking/__init__.py", line 246, in _track_printjob_event
    elapsed = int(payload.get(b"time"))
TypeError: int() argument must be a string or a number, not 'NoneType'
2019-02-25 15:01:02,647 - octoprint.util.comm - INFO - Changing monitoring state from "Finishing" to "Pausing"
2019-02-25 15:07:46,080 - octoprint.server.util.sockjs - INFO - Client connection closed: 2.0.0.15


# Fixed code
def _track_printjob_event(self, event, payload):
    if not self._settings.get_boolean(["events", "printjob"]):
        return

    unique_id = self._settings.get([b"unique_id"])
    if not unique_id:
        return

    sha = hashlib.sha1()
    sha.update(payload.get("path"))
    sha.update(unique_id)

    track_event = None
    args = dict(origin=payload.get(b"origin"), file=sha.hexdigest())

    if event == Events.PRINT_STARTED:
        track_event = "print_started"
    elif event == Events.PRINT_DONE:
        try:
            elapsed = int(payload.get(b"time", 0))
            if elapsed:
                args[b"elapsed"] = elapsed
        except ValueError:
            pass
        track_event = "print_done"
    elif event == Events.PRINT_FAILED:
        try:
            elapsed = int(payload.get(b"time", 0))
            if elapsed:
                args[b"elapsed"] = elapsed
        except ValueError:
            pass
        args[b"reason"] = payload.get(b"reason", "unknown")

        if b"error" in payload and self._settings.get_boolean(["events", "commerror"]):
            args[b"commerror_text"] = payload[b"error"]

        track_event = "print_failed"
    elif event == Events.PRINT_CANCELLED:
        try:
            elapsed = int(payload.get(b"time", 0))
            if elapsed:
                args[b"elapsed"] = elapsed
        except ValueError:
            pass
        track_event = "print_cancelled"

    if callable(self._helpers_get_throttle_state):
        try:
            throttle_state = self._helpers_get_throttle_state(run_now=True)
            if throttle_state and (
                throttle_state.get(b"current_issue", False)
                or throttle_state.get(b"past_issue", False)
            ):
                args[b"throttled_now"] = throttle_state[b"current_issue"]
                args[b"throttled_past"] = throttle_state[b"past_issue"]
                args[b"throttled_mask"] = throttle_state[b"raw_value"]
        except:
            # ignored
            pass

    if track_event is not None:
        self._track(track_event, **args)