-
Notifications
You must be signed in to change notification settings - Fork 200
Add support for embedding of analytics pixels on package pages #1042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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, 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 hidden or 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 hidden or 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, 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.