2020-03-04 11:55:10 +04:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
|
|
|
|
|
|
|
|
namespace Kruzya.TelegramBot.RichSiteSummary.UrchinTrackingModule
|
|
|
|
|
{
|
|
|
|
|
public class UtmUtility
|
|
|
|
|
{
|
|
|
|
|
public static string ApplyParameters(string url, UtmParameters parameters)
|
2023-02-15 18:28:37 +03:00
|
|
|
=> ApplyParameters(new Uri(url), parameters);
|
|
|
|
|
|
|
|
|
|
public static string ApplyParameters(Uri uri, UtmParameters parameters)
|
2020-03-04 11:55:10 +04:00
|
|
|
{
|
|
|
|
|
var query = HttpUtility.ParseQueryString(uri.Query);
|
2023-02-15 18:28:37 +03:00
|
|
|
|
2020-03-04 11:55:10 +04:00
|
|
|
SetIfNotNull(query, "utm_source", parameters.Source);
|
|
|
|
|
SetIfNotNull(query, "utm_medium", parameters.Type);
|
|
|
|
|
SetIfNotNull(query, "utm_campaign", parameters.Campaign);
|
|
|
|
|
SetIfNotNull(query, "utm_term", parameters.Term);
|
|
|
|
|
SetIfNotNull(query, "utm_content", parameters.Content);
|
2023-02-15 18:28:37 +03:00
|
|
|
|
2020-03-04 11:55:10 +04:00
|
|
|
var uriBuilder = new UriBuilder(uri);
|
|
|
|
|
uriBuilder.Query = BuildQueryString(query);
|
|
|
|
|
return uriBuilder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static string BuildQueryString(NameValueCollection query)
|
|
|
|
|
{
|
|
|
|
|
var queryParameters = new List<string>();
|
|
|
|
|
|
|
|
|
|
foreach (var keyName in query.AllKeys)
|
|
|
|
|
{
|
|
|
|
|
var value = query[keyName];
|
|
|
|
|
queryParameters.Add($"{HttpUtility.UrlEncode(keyName)}={HttpUtility.UrlEncode(value)}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Join('&', queryParameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static void SetIfNotNull(NameValueCollection queryParameteters, string key, string value)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryParameteters.Set(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|