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
Add Google Analytics support #1058
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -23,6 +23,76 @@ function podlove_get_remote_addr() { | |
return $_SERVER['REMOTE_ADDR']; | ||
} | ||
|
||
function ga_track_download($request_id, $media_file, $ua_string, $ptm_context, $ptm_source) { | ||
// GA Tracking | ||
$debug_ga = false; | ||
$ga_collect_endpoint = 'https://www.google-analytics.com/' . ($debug_ga ? 'debug/' : '') . 'collect'; | ||
|
||
$ga_tracking_id = trim(\Podlove\get_setting('tracking', 'ga')); | ||
if (!$ga_tracking_id || $ga_tracking_id === '') { | ||
return; | ||
} | ||
|
||
$ga_params = array( | ||
// Basics | ||
'v' => '1', // version | ||
'tid' => $ga_tracking_id, // tracking id | ||
'cid' => $request_id, // client id | ||
'ua' => $ua_string, // user agent override | ||
'uip' => podlove_get_remote_addr(), // IP override | ||
'ds' => 'podlove', // data source | ||
|
||
// We highjack the campaign fields for context/source data. | ||
// Source / Medium maps to Podlove context / Podlove source. | ||
// This way all Podlove sources can be easily grouped into GA Channels. | ||
'cs' => $ptm_context, // campaign source | ||
'cm' => $ptm_source, // campaign medium | ||
'ci' => $media_file->episode()->number, // campaign id | ||
'cn' => $media_file->episode()->title(), // campaign name | ||
|
||
// Pageview params | ||
't' => 'pageview', // hit type | ||
'dh' => $_SERVER[HTTP_HOST], // document host | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing quotes here and on next line |
||
'dp' => $_SERVER[REQUEST_URI], // document path | ||
'dt' => $media_file->episode()->title(), // document title | ||
); | ||
|
||
$ga_param_fragments = array(); | ||
array_walk($ga_params, function($item, $key) use(&$ga_param_fragments) { | ||
array_push($ga_param_fragments, sprintf('%s=%s', $key, rawurlencode($item))); | ||
}); | ||
|
||
$body = implode('&', $ga_param_fragments); | ||
$curl = new \Podlove\Http\Curl(); | ||
$curl->request( $ga_collect_endpoint, array( | ||
'method' => 'POST', | ||
'body' => $body, | ||
)); | ||
|
||
if (!$curl->isSuccessful()) { | ||
if ($debug_ga) { | ||
header('x-ga-debug: http error'); | ||
} | ||
\Podlove\Log::get()->addError('GA Measurement Protocol request failed.'); | ||
} else { | ||
\Podlove\Log::get()->addInfo('GA Measurement Protocol request successful: ' . $body); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer |
||
if (!$debug_ga) { | ||
return; | ||
} | ||
|
||
$response = json_decode($curl->get_response()['body'], true); | ||
$hit_paring_result = $response['hitParsingResult'][0]; | ||
if ($hit_paring_result['valid']) { | ||
header('x-ga-debug: valid'); | ||
\Podlove\Log::get()->addInfo('GA Measurement Protocol hit valid.'); | ||
} else { | ||
$debug_message = sprintf('%s(%s): %s', $hit_paring_result['parserMessage'][0]['messageType'], $response['hitParsingResult'][0]['parserMessage'][0]['parameter'], $response['hitParsingResult'][0]['parserMessage'][0]['description']); | ||
header(sprintf('x-ga-debug: ' . $debug_message )); | ||
\Podlove\Log::get()->addError('GA Measurement Protocol hit invalid.', $hit_paring_result['parserMessage'][0]); | ||
} | ||
} | ||
} | ||
|
||
function podlove_handle_media_file_tracking(\Podlove\Model\MediaFile $media_file) { | ||
|
||
if (\Podlove\get_setting('tracking', 'mode') !== "ptm_analytics") | ||
|
@@ -68,6 +138,8 @@ function podlove_handle_media_file_tracking(\Podlove\Model\MediaFile $media_file | |
} | ||
|
||
$intent->save(); | ||
|
||
ga_track_download($intent->request_id, $media_file, $ua_string, $ptm_context, $ptm_source); | ||
} | ||
|
||
function podlove_handle_media_file_download() { | ||
|
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
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.
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.
Performance concern: do
$episode = $media_file->episode()
once and use that instead of calling$media_file->episode()
repeatedly as it does duplicate database requests each time.