This step is the first of the series: How to track External Links with Sitecore DMS. In this step we are going to create a page (link.aspx) that will register the outgoing traffic into the Sitecore Analytics database and redirects the visitor to the external website.
Save the outgoing click information to the Analytics database
Create the custom page event
Open the content editor, go to /sitecore/system/Settings/Analytics/Page Events and create a Page event called External Link.
Under the Options field section select the IsSystem checkbox
We refer to the name of this Page Event Item when saving the outgoing link to the Analytics database.
Create the sublayout
Create a sublayout called LogExternalLink. In the Page_Load event add the code.
protected void Page_Load(object sender, EventArgs e) { if (Tracker.IsActive) { bool trackClick = true; //Check if outgoing traffic is generated within a visit of the site if (Tracker.CurrentVisit.PreviousPage == null) { //The visit is a direct visit or from another website, don't add this link to the analytics db. //OPTIONAL; Log these links based on referrer url. trackClick = false; } //Get linkUrl from querystring string externalUrl = Request.QueryString["url"]; //Check if link is set if (!string.IsNullOrEmpty(externalUrl)) { if (trackClick) { //Create PageEvent, the name must match Page Event item in Sitecore PageEventData data = new PageEventData("External Link"); //Set PageEvent data data.Text = string.Format("Outgoing traffic to: {0}", externalUrl); ; data.DataKey = externalUrl; data.Data = externalUrl; //Add the event to the previouspage; that's where the link is clicked Tracker.CurrentVisit.PreviousPage.Register(data); Tracker.Submit(); } //Redirect visitor Response.Redirect(externalUrl); } } //Something not right here, send them back to the homepage //TODO: Log Error or Warning Response.Redirect("/"); }
Based on your businesscase you could choose to create a handler. A handler will have less overhead. If you create a Sublayout you could create a screen telling the visitor that there are leaving the website (for example like Facebook does).
Now create an item link underneath the website root and place the LogExternalLink sublayout that we just created on this item. This item will be the link.aspx page. All external links will be redirected and processed trough this page.
Test if it works
At this point we cannot test this part of the solution. Testing this solution is only possible if the tracking page (link.aspx) is referred from the website. We don’t want to track URLs that are triggered from a direct request.
We will test this part of the solution after Step 2 of the series: How to track External Links with Sitecore DMS.
2 Replies to “Step 1: Save outgoing click to the DMS Analytics database”