How can I follow redirects of link programmatically?

In typical ad networks, a link redirects through multiple URLs before reaching play store or final website, based on the device's os and country. So I want to follow the redirections of a link by mimicking different OS and IP combinations.