mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
UTM for links on posts
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.UrchinTrackingModule
|
||||
{
|
||||
public struct UtmParameters
|
||||
{
|
||||
public string Source;
|
||||
public string Type;
|
||||
public string Campaign;
|
||||
public string Term;
|
||||
public string Content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
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)
|
||||
{
|
||||
var uri = new Uri(url);
|
||||
var query = HttpUtility.ParseQueryString(uri.Query);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user