Skip to content

Commit

Permalink
Added google tag manager plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
raven-wing authored Dec 12, 2023
1 parent 90ab34f commit a1e2400
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### 0.2.1
* added csrf token to form
* added google tag manager plugin

#### 0.2.0
* Changed database schema
Expand Down
16 changes: 16 additions & 0 deletions goodmap/platzky/platzky.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def __init__(self, config: Config, db, import_name):
self.config.from_mapping(config.dict(by_alias=True))
self.db = db
self.notifiers = []
self.dynamic_bodies = ""
self.dynamic_headers = ""

babel_translation_directories = ";".join(config.translation_directories)

Expand All @@ -39,6 +41,12 @@ def notify(self, message: str):
def add_notifier(self, notifier):
self.notifiers.append(notifier)

def add_dynamic_body(self, body: str):
self.dynamic_bodies += body

def add_dynamic_header(self, body: str):
self.dynamic_headers += body

def get_locale(self) -> str:
domain = request.headers["Host"]
domain_to_lang = self.config.get("DOMAIN_TO_LANG")
Expand Down Expand Up @@ -103,6 +111,14 @@ def utils():
"secondary_color": app.db.get_secondary_color(),
}

@app.context_processor
def dynamic_bodies():
return {"dynamic_bodies": app.dynamic_bodies}

@app.context_processor
def dynamic_headers():
return {"dynamic_headers": app.dynamic_headers}

@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html", title="404"), 404
Expand Down
29 changes: 29 additions & 0 deletions goodmap/platzky/plugins/google-tag-manager/entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def process(app, plugin_config):
gtm_id = plugin_config["ID"]

header = (
"""<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','"""
+ gtm_id
+ """');</script>
<!-- End Google Tag Manager -->
"""
)
app.add_dynamic_header(header)

body = (
"""<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id="""
+ gtm_id
+ """
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
"""
)
app.add_dynamic_body(body)

return app
8 changes: 2 additions & 6 deletions goodmap/platzky/templates/body_meta.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MRQ7FDB"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
{{ dynamic_bodies | safe }}

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->

Expand Down
10 changes: 1 addition & 9 deletions goodmap/platzky/templates/head_meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@
href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800'
rel='stylesheet' type='text/css'>

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-MRQ7FDB');
</script>
<!-- End Google Tag Manager -->

{{ dynamic_headers | safe }}

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='blog.css') }}">

0 comments on commit a1e2400

Please sign in to comment.