We are trying to create an affiliate URL, so that we can track users when they install our app on their Shopify store. We provide a URL, https://ourappname.com/services/convert?ref=xxxx, and when user clicks this url, we set browser cookies and then they are redirected to https://apps.shopify.com/appname.
@GetMapping("/convert")
public void setCookieAndRedirect(HttpServletRequest request,HttpServletResponse response,
@RequestParam(value ="ref", required = false) String ref) {
System.out.println(" REceived ref code " + ref +" ::: redirecting to appstore.");
Cookie cookie = new Cookie("RefCookie", ref);
try {
//add cookie to response
response.addCookie(cookie);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 24 * 365 * 10);
URL urlToRedirect = new URL("https://apps.shopify.com/appname");
cookie.setDomain(urlToRedirect.getHost());
response.addCookie(cookie);
response.sendRedirect("https://apps.shopify.com/appname");
}catch(Exception ex) {
ex.printStackTrace();
}
}
Our plan was - once they install the app and we get the control in our server, we are checking the browser cookies and if the ref is saved with in our database, we provide a special plan for the user.
@GetMapping("/checkForDiscounts")
public ResponseEntity<String> checkForDiscounts(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for(Cookie ck : cookies) {
System.out.println(" Cookie Name = "+ck.getName()+" :::: Value = "+ck.getValue());
if("RefCookie".equals(ck.getName())) {
//provide special discount plan for user
}
}
}else {
System.out.println(" Cookie Not Found");
}
return new ResponseEntity<String>("success", HttpStatus.OK);
}
But unfortunately, cookies will not work for redirect urls, as per this answer. Is there any other solution for this issue?