Skip to content

Commit

Permalink
Add UTM Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
a-gubskiy committed May 11, 2024
1 parent 227461c commit 5224e3d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/X.Web.UTM/UTMParser.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
using System;
using System.Web;

namespace X.Web.UTM;

public class UTMParser
{
public UrchinTrackingModule Parse(string url)
{
throw new NotImplementedException();
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}

return Parse(new Uri(url));
}

public UrchinTrackingModule Parse(Uri url)
{
throw new NotImplementedException();
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}

var queryParameters = HttpUtility.ParseQueryString(url.Query);

var utm = new UrchinTrackingModule
{
Source = queryParameters["utm_source"] ?? string.Empty,
Medium = queryParameters["utm_medium"] ?? string.Empty,
Campaign = queryParameters["utm_campaign"] ?? string.Empty,
Term = queryParameters["utm_term"] ?? string.Empty,
Content = queryParameters["utm_content"] ?? string.Empty
};

return utm;
}
}
58 changes: 58 additions & 0 deletions tests/Tests.X.Web.UTM/UTMParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using Xunit;
using X.Web.UTM;

namespace Tests.X.Web.UTM;

public class UTMParserTests
{
[Fact]
public void Parse_ValidUrlWithAllParameters_ReturnsCorrectUTM()
{
var parser = new UTMParser();
var testUrl = "http://example.com?utm_source=google&utm_medium=cpc&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content";

var result = parser.Parse(testUrl);

Assert.NotNull(result);
Assert.Equal("google", result.Source);
Assert.Equal("cpc", result.Medium);
Assert.Equal("test_campaign", result.Campaign);
Assert.Equal("test_term", result.Term);
Assert.Equal("test_content", result.Content);
}

[Fact]
public void Parse_ValidUriWithAllParameters_ReturnsCorrectUTM()
{
var parser = new UTMParser();
var testUri = new Uri("http://example.com?utm_source=google&utm_medium=cpc&utm_campaign=test_campaign&utm_term=test_term&utm_content=test_content");

var result = parser.Parse(testUri);

Assert.NotNull(result);
Assert.Equal("google", result.Source);
Assert.Equal("cpc", result.Medium);
Assert.Equal("test_campaign", result.Campaign);
Assert.Equal("test_term", result.Term);
Assert.Equal("test_content", result.Content);
}

[Theory]
[InlineData("http://example.com", "", "", "", "", "")]
[InlineData("http://example.com?utm_source=", "", "", "", "", "")]
[InlineData("http://example.com?utm_medium=&utm_campaign=&utm_term=&utm_content=", "", "", "", "", "")]
public void Parse_UrlWithMissingOrEmptyUtmParameters_ReturnsUtmWithEmptyFields(string url, string expectedSource, string expectedMedium, string expectedCampaign, string expectedTerm, string expectedContent)
{
var parser = new UTMParser();

var result = parser.Parse(url);

Assert.NotNull(result);
Assert.Equal(expectedSource, result.Source);
Assert.Equal(expectedMedium, result.Medium);
Assert.Equal(expectedCampaign, result.Campaign);
Assert.Equal(expectedTerm, result.Term);
Assert.Equal(expectedContent, result.Content);
}
}

0 comments on commit 5224e3d

Please sign in to comment.