Skip to content

Instantly share code, notes, and snippets.

@michaelhjulskov
Created May 6, 2015 13:07
Show Gist options
  • Save michaelhjulskov/3bf7afb5638d6749c96d to your computer and use it in GitHub Desktop.
Save michaelhjulskov/3bf7afb5638d6749c96d to your computer and use it in GitHub Desktop.
override Media to make google adwords conv tracking work
<?php
// Michael Hjulskov - added this to fix issue with Google Adwords conversion tracking code
// REMEMBER TO ADD data-keepinline="true" to the order-confirmation.tpl to avoid javascript to get broken
Class Media extends MediaCore
{
public static function deferScript($matches)
{
if (!is_array($matches))
return false;
$inline = '';
if (isset($matches[0]))
$original = trim($matches[0]);
if (isset($matches[1]))
$inline = trim($matches[1]);
// prestarocket
// This is an inline script, add its content to inline scripts stack then remove it from content
if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
return '';
//end prestarocket
// This is an external script, if it already belongs to js_files then remove it from content
preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
if (array_key_exists(1, $results))
{
if (substr($results[1], 0, 2) == '//')
{
$protocol_link = Tools::getCurrentUrlProtocolPrefix();
$results[1] = $protocol_link.ltrim($results[1], '/');
}
if (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src))
return '';
}
// return original string because no match was found
return $original;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment