-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from akamai/beaconapi
Add support for beaconing
- Loading branch information
Showing
8 changed files
with
156 additions
and
13 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "mPulseAPI" | ||
uuid = "314d2b54-f2c3-11ea-15f2-0bcf2fc50b35" | ||
authors = ["Akamai mPulse DSWB <[email protected]>"] | ||
version = "1.1.4" | ||
version = "1.2.0" | ||
|
||
[deps] | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
|
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,13 @@ | ||
# Sending Beacons | ||
|
||
mPulseAPI.jl uses the mPulse [REST Beacon API](https://techdocs.akamai.com/mpulse/reference/beacons#rest-api) to send beacons. | ||
|
||
You first need to make a config request using [`mPulseAPI.getBeaconConfig`](@ref) and then include that config in your call to | ||
[`mPulseAPI.sendBeacon`](@ref) along with other beacon parameters. | ||
|
||
Beacon parameters may be named by label or name. | ||
|
||
```@autodocs | ||
Modules = [mPulseAPI] | ||
Pages = ["BeaconAPI.jl"] | ||
``` |
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,104 @@ | ||
""" | ||
Base URL for config.json requests | ||
""" | ||
const CONFIG_URL = "https://c.go-mpulse.net/api/config.json" | ||
|
||
""" | ||
Fetch beacon configuration for an mPulse APP using the [Beacon API](https://techdocs.akamai.com/mpulse/reference/beacons#rest-api) | ||
Caches the result for whatever is specified in `Cache-control: max-age` | ||
""" | ||
function getBeaconConfig(appKey::AbstractString, appDomain::AbstractString) | ||
config_obj = getObjectFromCache("beacon-config", Dict(:appKey => appKey)) | ||
|
||
if !isnothing(config_obj) | ||
return config_obj | ||
end | ||
|
||
config = HTTP.get(CONFIG_URL, query = Dict("key" => appKey, "d" => appDomain)) | ||
|
||
cache_headers = filter(h -> lowercase(h[1]) == "cache-control", config.headers) | ||
if isempty(cache_headers) | ||
cache_headers = Dict{AbstractString, AbstractString}() | ||
else | ||
cache_headers = Dict{AbstractString, AbstractString}( | ||
map( | ||
x -> Pair([split(x, "="); ""][1:2]...), | ||
split( | ||
mapreduce( | ||
h -> h[2], | ||
(l, r) -> string(l, ", ", r), | ||
cache_headers | ||
), | ||
r", *" | ||
) | ||
) | ||
) | ||
end | ||
|
||
expiry = Dates.Second(parse(Int, get(cache_headers, "max-age", "300"); base=10)) | ||
|
||
config_obj = JSON.parse(IOBuffer(config.body)) | ||
|
||
writeObjectToCache("beacon-config", Dict(:appKey => appKey), config_obj; expiry) | ||
|
||
return config_obj | ||
end | ||
|
||
""" | ||
Send a beacon to mPulse | ||
""" | ||
function sendBeacon(config::Dict, params::Dict) | ||
beacon_url = "https:" * config["beacon_url"] | ||
|
||
now_ms = Int(datetime2unix(now())*1000) | ||
|
||
beacon_params = Dict{String, Any}( | ||
"api" => 1, | ||
"api.v" => 1, | ||
"h.cr" => config["h.cr"], | ||
"h.d" => config["h.d"], | ||
"h.key" => config["h.key"], | ||
"h.t" => config["h.t"], | ||
"rt.end" => now_ms, | ||
"rt.si" => get(params, "SessionID", config["session_id"]), | ||
"rt.sl" => get(params, "SessionLength", 1), | ||
"rt.ss" => get(params, "SessionStart", now_ms), | ||
"rt.start" => "manual", | ||
) | ||
|
||
if haskey(params, "PageGroup") | ||
beacon_params["h.pg"] = params["PageGroup"] | ||
end | ||
if haskey(params, "Url") | ||
beacon_params["u"] = params["Url"] | ||
end | ||
if haskey(params, "tDone") | ||
beacon_params["t_done"] = params["tDone"] | ||
end | ||
|
||
vars = ["customMetrics", "customDimensions", "customTimers"] | ||
t_other = [] | ||
for v in vars | ||
for p in config["PageParams"][v] | ||
for k in ["label", "name"] | ||
if haskey(params, p[k]) | ||
if v == "customTimers" | ||
push!(t_other, string(p["label"], "|", params[p[k]])) | ||
else | ||
beacon_params[p["label"]] = params[p[k]] | ||
end | ||
break | ||
end | ||
end | ||
end | ||
end | ||
|
||
if !isempty(t_other) | ||
beacon_params["t_other"] = join(t_other, ",") | ||
end | ||
|
||
|
||
res = HTTP.get(beacon_url, query = beacon_params) | ||
|
||
return res.status == 204 | ||
end |
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,19 @@ | ||
config = mPulseAPI.getBeaconConfig(beaconKey, "mPulseAPIDemo.net") | ||
|
||
@test config isa Dict | ||
@test haskey(config, "site_domain") | ||
@test config["site_domain"] == "mPulseAPIDemo.net" | ||
|
||
if !haskey(config, "rate_limited") | ||
@test haskey(config, "h.cr") | ||
@test haskey(config, "h.d") | ||
@test haskey(config, "h.key") | ||
@test haskey(config, "h.t") | ||
|
||
t_end = Int(datetime2unix(now())*1000) | ||
@test mPulseAPI.sendBeacon(config, Dict("PageGroup" => "mPulseAPI Test", "tDone" => t_end - t_start, "Conversion" => 1, "ResourceTimer" => 500, "Url" => "https://github.com/akamai/mPulseAPI.jl/")) | ||
end | ||
|
||
config2 = mPulseAPI.getBeaconConfig(beaconKey, "mPulseAPIDemo.net") | ||
|
||
@test config == config2 |
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
571d927
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
571d927
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/93728
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: