-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce analytics pixels infrastructure
- Loading branch information
Showing
14 changed files
with
572 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
$hackageCssTheme()$ | ||
<title>Analytics pixels for $pkgname$ | Hackage</title> | ||
</head> | ||
|
||
<body> | ||
$hackagePageHeader()$ | ||
|
||
<div id="content"> | ||
|
||
<h2>Adding a analytics pixel to <a href="/package/$pkgname$">$pkgname$</a></h2> | ||
|
||
<p> | ||
Configure an analytics pixel to be automatically loaded on your package’s page on Hackage. | ||
You’ll need an image URL from any external analytics provider, e.g. <a href="about.scarf.sh">Scarf</a>, which is provided | ||
for free and can surface information about web traffic to your package including geographic | ||
distribution, version distribution, and companies. | ||
</p> | ||
|
||
<form method="POST" class="box" action="/package/$pkgname$/analytics-pixels"> | ||
<label for="analytics-pixel">Analytics Image URL</label> | ||
<input name="analytics-pixel" type="text" /> | ||
<input type="submit" /> | ||
</form> | ||
|
||
<h2>Existing analytics pixels for <a href="/package/$pkgname$">$pkgname$</a></h2> | ||
|
||
<ul> | ||
$analyticsPixels:{analyticsPixel| | ||
<li> | ||
<form method="POST" action="/package/$pkgname$/analytics-pixels"> | ||
<label for="analytics-pixel">$analyticsPixel$</label> | ||
<input type="hidden" name="analytics-pixel" value="$analyticsPixel$"/> | ||
<input type="hidden" name="_method" value="DELETE" /> | ||
<input type="submit" value="Delete" /> | ||
</form> | ||
</li> | ||
}; separator=""$ | ||
</ul> | ||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
datafiles/templates/Html/user-analytics-pixels-page.html.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
$hackageCssTheme()$ | ||
<title>Analytics pixels for all of $username$'s packages | Hackage</title> | ||
</head> | ||
|
||
<body> | ||
$hackagePageHeader()$ | ||
|
||
<div id="content"> | ||
|
||
<h2>Create a analytics pixel</h2> | ||
|
||
<p> | ||
Configure an analytics pixel to be automatically loaded on your package’s page on Hackage. | ||
You’ll need an image URL from any external analytics provider, e.g. <a href="about.scarf.sh">Scarf</a>, which is provided | ||
for free and can surface information about web traffic to your package including geographic | ||
distribution, version distribution, and companies. | ||
</p> | ||
|
||
<form method="POST" class="box" action="/user/$username$/analytics-pixels"> | ||
<p> | ||
<label for="package">Package</label> | ||
<select name="package"> | ||
$pkgs:{pkg| | ||
<option value="$pkg$">$pkg$</option> | ||
}; separator=""$ | ||
</select> | ||
</p> | ||
<p> | ||
<label for="analytics-pixel">Analytics pixel URL</label> | ||
<input name="analytics-pixel" type="text" /> | ||
</p> | ||
<input type="submit" /> | ||
</form> | ||
|
||
<h2>Existing analytics pixels</h2> | ||
|
||
$pkgpixels:{pkgpixel| | ||
<h3><a href="/package/$pkgpixel.0$">$pkgpixel.0$</a></h3> | ||
<ul> | ||
$pkgpixel.1:{analyticsPixel| | ||
<li> | ||
<form method="POST" action="/user/$username$/analytics-pixels"> | ||
<label for="analytics-pixel">$analyticsPixel$</label> | ||
<input type="hidden" name="package" value="$pkgpixel.0$" /> | ||
<input type="hidden" name="analytics-pixel" value="$analyticsPixel$"/> | ||
<input type="hidden" name="_method" value="DELETE" /> | ||
<input type="submit" value="Delete" /> | ||
<form> | ||
</li> | ||
}; separator=""$ | ||
</ul> | ||
}; separator=""$ | ||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
{-# LANGUAGE RankNTypes, NamedFieldPuns, RecordWildCards #-} | ||
|
||
-- | Implements a system to allow users to upvote packages. | ||
-- | ||
module Distribution.Server.Features.AnalyticsPixels | ||
( AnalyticsPixelsFeature(..) | ||
, AnalyticsPixel(..) | ||
, initAnalyticsPixelsFeature | ||
) where | ||
|
||
import Data.Set (Set) | ||
|
||
import Distribution.Server.Features.AnalyticsPixels.State | ||
|
||
import Distribution.Server.Framework | ||
import Distribution.Server.Framework.BackupRestore | ||
|
||
import Distribution.Server.Features.Core | ||
import Distribution.Server.Features.Upload | ||
import Distribution.Server.Features.Users | ||
|
||
import Distribution.Package | ||
|
||
-- | Define the prototype for this feature | ||
data AnalyticsPixelsFeature = AnalyticsPixelsFeature { | ||
analyticsPixelsFeatureInterface :: HackageFeature, | ||
analyticsPixelsResource :: Resource, | ||
userAnalyticsPixelsResource :: Resource, | ||
|
||
analyticsPixelAdded :: Hook (PackageName, AnalyticsPixel) (), | ||
analyticsPixelRemoved :: Hook (PackageName, AnalyticsPixel) (), | ||
|
||
-- | Returns all 'AnalyticsPixel's associated with a 'Package'. | ||
getPackageAnalyticsPixels :: forall m. MonadIO m => PackageName -> m (Set AnalyticsPixel), | ||
|
||
-- | Adds a new 'AnalyticsPixel' to a 'Package'. Returns True in case it was added. False in case | ||
-- it's already existing. | ||
addPackageAnalyticsPixel :: forall m. MonadIO m => PackageName -> AnalyticsPixel -> m Bool, | ||
|
||
-- | Remove a 'AnalyticsPixel' from a 'Package'. | ||
removePackageAnalyticsPixel :: forall m. MonadIO m => PackageName -> AnalyticsPixel -> m () | ||
} | ||
|
||
-- | Implement the isHackageFeature 'interface' | ||
instance IsHackageFeature AnalyticsPixelsFeature where | ||
getFeatureInterface = analyticsPixelsFeatureInterface | ||
|
||
-- | Called from Features.hs to initialize this feature | ||
initAnalyticsPixelsFeature :: ServerEnv | ||
-> IO ( CoreFeature | ||
-> UserFeature | ||
-> UploadFeature | ||
-> IO AnalyticsPixelsFeature) | ||
initAnalyticsPixelsFeature env@ServerEnv{serverStateDir} = do | ||
dbAnalyticsPixelsState <- analyticsPixelsStateComponent serverStateDir | ||
analyticsPixelAdded <- newHook | ||
analyticsPixelRemoved <- newHook | ||
|
||
return $ \coref@CoreFeature{..} userf@UserFeature{..} uploadf -> do | ||
let feature = analyticsPixelsFeature env | ||
dbAnalyticsPixelsState | ||
coref userf uploadf analyticsPixelAdded analyticsPixelRemoved | ||
|
||
return feature | ||
|
||
-- | Define the backing store (i.e. database component) | ||
analyticsPixelsStateComponent :: FilePath -> IO (StateComponent AcidState AnalyticsPixelsState) | ||
analyticsPixelsStateComponent stateDir = do | ||
st <- openLocalStateFrom (stateDir </> "db" </> "AnalyticsPixels") initialAnalyticsPixelsState | ||
return StateComponent { | ||
stateDesc = "Backing store for AnalyticsPixels feature" | ||
, stateHandle = st | ||
, getState = query st GetAnalyticsPixelsState | ||
, putState = update st . ReplaceAnalyticsPixelsState | ||
, resetState = analyticsPixelsStateComponent | ||
, backupState = \_ _ -> [] | ||
, restoreState = RestoreBackup { | ||
restoreEntry = error "Unexpected backup entry" | ||
, restoreFinalize = return initialAnalyticsPixelsState | ||
} | ||
} | ||
|
||
|
||
-- | Default constructor for building this feature. | ||
analyticsPixelsFeature :: ServerEnv | ||
-> StateComponent AcidState AnalyticsPixelsState | ||
-> CoreFeature -- To get site package list | ||
-> UserFeature -- To authenticate users | ||
-> UploadFeature -- For accessing package maintainers and trustees | ||
-> Hook (PackageName, AnalyticsPixel) () -- Signals addition of a new AnalyticsPixel | ||
-> Hook (PackageName, AnalyticsPixel) () -- Signals removeal of a AnalyticsPixel | ||
-> AnalyticsPixelsFeature | ||
|
||
analyticsPixelsFeature ServerEnv{..} | ||
analyticsPixelsState | ||
CoreFeature { coreResource = CoreResource{..} } | ||
UserFeature{..} | ||
UploadFeature{..} | ||
analyticsPixelAdded | ||
analyticsPixelRemoved | ||
= AnalyticsPixelsFeature {..} | ||
where | ||
analyticsPixelsFeatureInterface = (emptyHackageFeature "AnalyticsPixels") { | ||
featureDesc = "Allow users to attach analytics pixels to their packages", | ||
featureResources = [analyticsPixelsResource, userAnalyticsPixelsResource] | ||
, featureState = [abstractAcidStateComponent analyticsPixelsState] | ||
} | ||
|
||
analyticsPixelsResource :: Resource | ||
analyticsPixelsResource = resourceAt "/package/:package/analytics-pixels.:format" | ||
|
||
userAnalyticsPixelsResource :: Resource | ||
userAnalyticsPixelsResource = resourceAt "/user/:username/analytics-pixels.:format" | ||
|
||
getPackageAnalyticsPixels :: MonadIO m => PackageName -> m (Set AnalyticsPixel) | ||
getPackageAnalyticsPixels name = | ||
queryState analyticsPixelsState (AnalyticsPixelsForPackage name) | ||
|
||
addPackageAnalyticsPixel :: MonadIO m => PackageName -> AnalyticsPixel -> m Bool | ||
addPackageAnalyticsPixel name pixel = do | ||
added <- updateState analyticsPixelsState (AddPackageAnalyticsPixel name pixel) | ||
when added $ runHook_ analyticsPixelAdded (name, pixel) | ||
pure added | ||
|
||
removePackageAnalyticsPixel :: MonadIO m => PackageName -> AnalyticsPixel -> m () | ||
removePackageAnalyticsPixel name pixel = do | ||
updateState analyticsPixelsState (RemovePackageAnalyticsPixel name pixel) | ||
runHook_ analyticsPixelRemoved (name, pixel) |
Oops, something went wrong.