YaWK  24.1
Yet another WebKit
googleAnalytics.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Google Analytics Widget - Embed Google Analytics on your page</b>
6  *
7  * <p>If you need to track your users with google analytics, you can use this widget.
8  * It requires a tracking ID and allows a few settings. In order to work properly, it
9  * is required to embed this widget on 'all pages' in any position that is not used
10  * by anything else. A recommended position would be debug or any other empty position.</p>
11  *
12  *
13  * @author Daniel Retzl <[email protected]>
14  * @copyright 2018 Daniel Retzl
15  * @version 1.0.0
16  * @brief Embed Google Analytics on your page.
17  */
18  class googleAnalytics extends \YAWK\widget
19  {
20  /** @param object global widget object data */
21  public $widget = '';
22  /** @param string Tracking ID */
23  public $gaTrackingID = '';
24  /** @param string Tracking Code */
25  public $gaTrackingCode = '';
26 
27  /**
28  * @brief Load all widget settings from database and fill object
29  * @param object $db Database Object
30  * @brief Load all widget settings on object init.
31  */
32  public function __construct($db)
33  {
34  // load this widget settings from db
35  $this->widget = new \YAWK\widget();
37  foreach ($settings as $property => $value) {
38  $this->$property = $value;
39  $this->gaTrackingID = $this->gaTrackingCode;
40  }
41  }
42 
43  /**
44  * @brief Print all object data
45  * @brief (for development and testing purpose)
46  */
47  public function printObject()
48  { // output data to screen
49  echo "<pre>";
50  print_r($this);
51  echo "</pre>";
52  }
53 
54  /**
55  * @brief Init and embed Google Analytics
56  * @brief Embed Google Analytics
57  */
58  public function init()
59  { // embed GoogleAnalytics
60  $this->embed();
61  }
62 
63  public function embed()
64  {
65  if (isset($this->gaTrackingID)
66  && (!empty($this->gaTrackingID)))
67  {
68  // make sure that the string contains no html
69  $this->gaTrackingID = strip_tags($this->gaTrackingID);
70 
71  // tracking code seems to be valid -
72  // embed Google Analytics
73  echo'<!-- google analytics -->
74 <!-- Global site tag (gtag.js) - Google Analytics -->
75 <script async src="https://www.googletagmanager.com/gtag/js?id='.$this->gaTrackingID.'"></script>
76 <script>
77  window.dataLayer = window.dataLayer || [];
78  function gtag(){dataLayer.push(arguments);}
79  gtag(\'js\', new Date());
80  gtag(\'config\', \''.$this->gaTrackingCode.'\');
81 </script>';
82  }
83  else
84  { // tracking code not set, empty or wrong data type
85  echo "Google Analytics could not be loaded - tracking code not set, empty or wrong data type.";
86  }
87  }
88  }
89 }
90 ?>
Embed Google Analytics on your page.
init()
Init and embed Google Analytics.
__construct($db)
Load all widget settings from database and fill object.
Widgets are small, useful tools that you can include everywhere in your website.
Definition: widget.php:22
getWidgetSettingsArray($db)
Get widget settings and return it as array.
Definition: widget.php:69
if(isset($_POST['save'])) $settings
$value