This is informally referred to as the illusion of causality:
Illusions of causality occur when people develop the belief that there
is a causal connection between two events that are actually unrelated.
Such illusions have been proposed to underlie pseudoscience and
superstitious thinking, sometimes leading to disastrous consequences
in relation to critical life areas, such as health, finances, and
This is a cognitive error in the way humans infer cause and effect.
The example that I'm most familiar with is in medical quackery. Suppose someone is experiencing constipation. They try some advice from friends: Go for a long walk, eat some prunes, massage their stomach, use magnesium supplements. Then they try more questionable solutions: Probiotics, colon hydrotherapy, acupuncture, herbal medicines. At some point during the process, the constipation is relieved, and the patient naturally concludes that whatever treatment immediately preceded this event must have been the cure. They "swear by this treatment" from then on, failing to consider that prior treatments may have needed more time to take effect, that other activities not considered treatments may have affected the results (eg, regular exercise, dietary changes), or simply the mere passage of time.