A capsule for traversing the gastrointestinal tract, including one or more compartments configured to sample the environment surrounding the capsule or configured to deliver content to the environment surrounding the capsule, an internal controller configured to activate the one or more compartments to sample or deliver, wherein the capsule includes a tracking system configured to identify the location of the capsule in real-time and activate a compartment when reaching a specific location in the gastrointestinal tract.