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:
@@ -6,6 +6,8 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using Kruzya.TelegramBot.RichSiteSummary.UrchinTrackingModule;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace Kruzya.TelegramBot.RichSiteSummary.Data
|
||||
@@ -28,8 +30,15 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Data
|
||||
|
||||
protected override string ViewableUrl
|
||||
{
|
||||
get => Url;
|
||||
get => UtmUtility.ApplyParameters(Url, new UtmParameters()
|
||||
{
|
||||
Source = "telegram",
|
||||
Type = "rss_post",
|
||||
Campaign = "subscriber",
|
||||
Content = PostId.ToString()
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Handler
|
||||
}
|
||||
}
|
||||
|
||||
[Command("feed_subscriptions")]
|
||||
[Command("rss_subscriptions")]
|
||||
public async Task Subscriptions()
|
||||
{
|
||||
if (!(await GenerateMenu(":sub")))
|
||||
|
||||
@@ -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