Is it inevitable that colonial empires will splinter and no longer be empires? I would argue that none of the great power empires have survived til today. They might have a tiny territory exclave somewhere, but not massive colonies of the type we're talking about with Spain. Britain comes closest, but Canada and Australia, while technically part of an empire, are realistically independent countries. So, unless world events track much differently, sooner or later the Spanish colonial empire will break apart. In that case, the challenge is to keep the Spanish Empire intact for as long as possible.
Carlos II not being bewitched, but rather being blessed with competence galore, would be a pod. This would rewrite the entire history of Europe, and probably the New World. France, Dutch Republic, probably Britain and the Glorious Revolution, possibly Austria, all go a different path. Within all this, Spain could figure out a way to stabilize and modernize.
Philip V being a competent ruler who follows up a good start with continuing to develop the country. Instead, he marries Elizabeth Farnese, and Spain expends all energy/focus on putting sons on Italian thrones.
Ferdinand was an OK steward, but his reign was mostly just having a moment to breathe after Philip V's non stop wars.
Carlos III seems semi competent, but he was a disaster. His reign starts with extreme folly of jumping into the 7 Y W after his ally France had lost the war. Spain is left in shambles. He then institutes the Bourbon Reforms, which are great for squeezing money out of the colonies, but are horrible for colonial unity. As mentioned above, instead of harnessing the power of the criolle elite, he sowed the seeds of revolution. Then, he spends a lot of resources to help the American Revolution and another war with Britain, ending up with Florida for his efforts, which is then ignored. When Carlos III dies, the Empire is no better than when he started, and arguably, sent on the road to revolution.
Carlos IV is an idiot. He inherits a country unable to protect itself. He makes his wife's lover, Godoy (a man with no credentials for the job), top dog in the gov't. Maybe they'd have been able to muddle through if the world was stable, but the French Revolution, and the Napoleon Era turned everything upside down. Godoy and Carlos IV were way in over their heads, and Spain was destroyed.
Ferdinand (another one) was another idiot like his father, and takes an Empire mostly destroyed, and destroys it further.
There's about 150 years of very poor management, with a little meh thrown in once in a while. First order of business is to magically correct that. Presto, hand wavium, Spain has competent leadership. Doesn't have to be non stop brilliance. Just mostly competent. There's going to be wars, but there doesn't need to be as much involvement as OTL. I think getting to a point Philip V did in 1715ish, with centralization, and a start to good fiscal policies, is a start. But then, it has to be continued. Pursue reforming/building the country instead of derailing progress in trying to retake Italy. Next reform the colonies. They aren't going to be kept forever, but allow them to grow instead of squeezing money out of them. Make them part of the empire, not just a resource to be sucked dry. There's a lot of corruption all over the empire, and getting it under control is a herculean task, but something has to be done. Even if the colonies are treated semi similar, but just a little better, the Empire can thrive for a long time.
Napoleon is a tough obstacle. The easiest thing is to magically eliminate him. Now the world is completely different. If we keep him, have Spain enter the era in better shape, and with better leadership avoid being ripped apart. Sans that big invasion/take over attempt, the Empire can easily rumble along for another 50-100 years.