Nag in .NET
Ask for support every N runs or every N days with SponsorkitNag, never in CI or scripts, and never someone who already contributes.
A nag is a gentle "Enjoying this? Consider supporting it" message. sk.Nag(...) gives you a SponsorkitNag that
remembers how often your app ran and when the message was last shown. It decides when; you decide what to
print and who should see it. Sponsorkit never prints anything itself and never signs anyone in from a nag.
using Sponsorkit;
var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", AppName = "my-cli" });
var nag = sk.Nag(new NagOptions("my-cli") { Every = 10, OrEvery = TimeSpan.FromDays(7) });
if (await nag.DueAsync(fc => fc.Active().Count == 0))
{
Console.Error.WriteLine($"Enjoying my-cli? Support it: {sk.CheckoutUrl(new CheckoutUrlOptions { Frequency = Frequencies.Monthly })}");
nag.Shown();
}Print to stderr, as above, so the message never ends up in output that another program parses.
Options#
NagOptions | Default | Meaning |
|---|---|---|
Id (constructor) | required | A stable name, such as your app's name. Counters are kept per id, so one app can have several nags. An empty id throws ArgumentException. |
Every | 10 | Due once the app has run this many times since the nag was last shown. |
OrEvery | null | Also due once this much time has passed since it was last shown. Never before the first Shown(). |
NagOptions.ParseDuration("P7D") turns an ISO-8601 duration (P7D, PT12H) into a TimeSpan, for
durations read from a config file. Invalid input throws invalid_config.
How DueAsync decides#
DueAsync(predicate, force = false, cancellationToken = default):
- Returns
falseifSPONSORKIT_DISABLEis set. - Counts this run, once per process per id, even if it later returns
false. CallingDueAsynctwice in one process counts one run. - Returns
falseifSPONSORKIT_NO_NAGorCIis set, or if stdout or stderr is redirected, unlessforceistrue. - Calls
GetFinancialContributionAsync(served from the cache when fresh) and passes the result to your predicate. Returnsfalseif the predicate does. - Returns
trueifruns >= Every, or ifOrEveryis set and at leastOrEveryhas passed since the lastShown().
Shown() resets the run counter and the timer. Call it only when you actually displayed the message.
The counters live in the state file next to the session, so they survive restarts and SignOutAsync. They're
kept per target set, not per user account on your side.
Choosing the predicate#
The predicate is your policy. A few common ones:
using System.Linq;
using Sponsorkit;
public static class Predicates
{
// Everyone without an active contribution, including signed-out users.
public static bool NotActive(ContributionResult fc) => fc.Active().Count == 0;
// Only signed-in users who have never contributed: don't pester anonymous users.
public static bool SignedInNeverContributed(ContributionResult fc) => fc.SignedIn && fc.Contributions.Count == 0;
// Lapsed supporters: they had a recurring contribution that's no longer active.
public static bool Lapsed(ContributionResult fc) =>
fc.Active().Count == 0 && fc.Recurring().Any(c => c.Status == ContributionStatuses.Cancelled);
}Use them as await nag.DueAsync(Predicates.Lapsed). Because a signed-out result has no contributions, a
predicate like NotActive also nags signed-out users; pair the message with a hint such as "Already a supporter?
Run my-cli login."
Terminals, CI and GUIs#
The terminal check exists so that a nag never lands in a log file or a pipe. Two options change it:
SponsorkitOptions.Interactive = truetreats the process as interactive regardless of redirection. Use it in GUI apps, where stdout is often redirected or absent but the user sees your dialog, not the console.DueAsync(predicate, force: true)skips the terminal,CIandSPONSORKIT_NO_NAGchecks for one call, for example from an explicitmy-cli aboutcommand.SPONSORKIT_DISABLEstill wins.
using Sponsorkit;
// A desktop app: the nag is a dialog, so stdout redirection doesn't matter.
var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", AppName = "My App", Interactive = true });
var nag = sk.Nag(new NagOptions("my-app-dialog") { Every = 20, OrEvery = NagOptions.ParseDuration("P30D") });
if (await nag.DueAsync(fc => fc.Active().Count == 0))
{
// ShowSupportDialog(sk.CheckoutUrl());
nag.Shown();
}To test a nag without running your app ten times, see testing nags. The nag guide has the same patterns in every language.