If you are into affiliate marketing then you must be getting promo codes from your partners which are promoted on various platforms. For ex, Amazon publishes promo codes for Associates which can then be pormoted by the Associates and they get a cut from the sales. Its a nice way to boost conversion and earn commission. Associates share the link to temporary promotion pages which is tagged with their affiliate id. These promotions run for a short duration and need to be managed more frequently by adding/removing them from your site almost on a daily basis so it makes sense to have a small database at your end which you can use to manage it at your end and it can also take care of the promotions which have ended. If a promotion has ended, Amazon takes you to the deals page when a promotion link is clicked but it makes more sense to not show the promotion at the first place to your customers. Here is a nice and quick way to manage it for an ASP.NET application.
Create an XML file to keep a list of promotions which you want to advertise on your site. An example XML would be as following
<?xml version="1.0" encoding="utf-8" ?> <promotions> <promotion> <title>Save 15% on select TP-Link camera products.</title> <url>http://amzn.to/2D0s1A0</url> <type>Camera</type> <date>02-23-2018</date> </promotion> <promotion> <title>Save $1.50 on select Sabrent pc products.</title> <url>http://amzn.to/2Dcl3uI</url> <type>Electronics/PC</type> <date>01-19-2018</date> </promotion> </promotions>
Now we will create a web service in ASP.NET and add a web method which will read the XML file from the server and return list of active promotions. A date is kept in the XML file for tracking end dates of promotions. The web service only returns the list of promotions which are active after the current date. Add a web method as following in your application.
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Web; using System.Web.Script.Services; using System.Web.Services; using System.Xml.Linq; namespace com.Web.WebService { /// <summary> /// Summary description for Promotions /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class Promotions : System.Web.Services.WebService { [WebMethod(CacheDuration = 86400)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetPromotions() { try { System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string filePath = HttpContext.Current.Server.MapPath("~/promotions/") + "promotions.xml"; XElement xelement = XElement.Load(filePath); IEnumerable<XElement> promotions = xelement.Elements(); IList ps = new List<Promotion>(); // Read the entire XML foreach (var promotion in promotions) { try { var p = new Promotion(); p.Title = promotion.Element("title").Value; p.Url = promotion.Element("url").Value; p.Type = promotion.Element("type").Value; p.Date = promotion.Element("date").Value; if(DateTime.ParseExact(p.Date, "MM-dd-yyyy", null) >= DateTime.Today) ps.Add(p); } catch (Exception e) { } } return serializer.Serialize(ps); } catch (Exception ex) { return "Failed"; //return ex.Message; } } } public class Promotion { public string Title { get; set; } public string Url { get; set; } public string Type { get; set; } public string Date { get; set; } } }
The XML file is kept in promotions folder at your web site root. Next step is to call this web service wherever you want to display the promotions. One way is to simply create an HTML file and make an AJAX call. You can then insert this HTML file using an iFrame in your website pages wherever you want to display these promotions.
First create a JS file to define web service call method.
function GetPromos() { var param = {}; $.ajax({ type: "POST", url: "/WebService/Promotions.asmx/GetPromotions", contentType: "application/json; charset=utf-8", data: JSON.stringify(param), dataType: "json", success: function (response) { var data = response.d; if (data && data !== "" && data !== "Failed") { var promos = JSON.parse(data); var str = "<h3>Promotions</h3>"; for (var i = 0, len = promos.length; i < len; i++) { var p = promos[i]; str += "[" + p.Type + "] " + "<a target='_blank' href='" + p.Url + "'>" + p.Title + "</a> | "; } $(".promotionAd").html(str); } }, failure: function (response) { //alert(response.d); } }); }
Nest step is to create an HTML file and make an AJAX call to this method.
<!DOCTYPE html> <head> <style> body, html, .promotionAd { margin: 0; padding: 0; } .promotionAd { text-align: center; } h3{margin:0; padding:0;} </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="/js/promotions.js"></script> </head> <body> <div class="promotionAd"> </div> <script type="text/javascript"> $(function () { GetPromos(); }); </script> </body> </html>
Now open this HTML file from your web serice and you will start seeing promotions. You can manage promotions at your convenience without worrying about expired promotions which won’t be shown anymore. You can keep adding incremental promotions to the XML file and upload it to the server. Make sure to remove expired ones since you don’t want to put unnecessary load on the server to process huge XML files having expired promotions.
You can use caching and other mechanism to optimize the web service calls. The quick way is to use ASP.NET caching for web services as you can see in the WebMethod declaration of web service code.
I hope you are now thinking about possible use cases of this method. If you have any suggestion or question, please leave a comment.