Skip to content

Commit d4cf912

Browse files
Merge branch 'feature-add-google-ads-plugins' into master
2 parents 84ab7b2 + 4d503a2 commit d4cf912

File tree

13 files changed

+90
-17
lines changed

13 files changed

+90
-17
lines changed

blog/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from blog.constants import MAX_BLOG_ARTICLE_TITLE_LENGTH
2424
from company_website.models import Employees
25+
from company_website.view_helpers import GoogleAdsMixin
2526

2627

2728
class MixinSeoFields(models.Model):
@@ -48,7 +49,7 @@ def get_paginated_articles(articles: QuerySet, request: WSGIRequest) -> Page:
4849
return paginated_articles
4950

5051

51-
class BlogIndexPage(MixinSeoFields, Page, MixinPageMethods):
52+
class BlogIndexPage(MixinSeoFields, Page, MixinPageMethods, GoogleAdsMixin):
5253
template = "blog_index_page.haml"
5354

5455
def get_all_articles(self) -> PageQuerySet: # pylint: disable=no-self-use
@@ -77,7 +78,7 @@ def get_context(self, request: WSGIRequest, *args: Any, **kwargs: Any) -> dict:
7778
return context_data
7879

7980

80-
class BlogArticlePage(MixinSeoFields, Page, MixinPageMethods):
81+
class BlogArticlePage(MixinSeoFields, Page, MixinPageMethods, GoogleAdsMixin):
8182
template = "blog_post.haml"
8283
date = models.DateField("Post date")
8384
intro = models.CharField(max_length=250)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(function(_window, _document, script, layer, id){
2+
_window[layer] = _window[layer] || [];
3+
_window[layer].push({'gtm.start': new Date().getTime(), event: 'gtm.js'});
4+
var f = _document.getElementsByTagName(script)[0],
5+
j = _document.createElement(script), dl = layer != 'dataLayer' ? '&l=' + layer : '';
6+
j.async = true;
7+
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + id + dl;
8+
f.parentNode.insertBefore(j, f);
9+
})
10+
(window, document, 'script', 'dataLayer', 'GTM-TCWZK7N');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
window.dataLayer = window.dataLayer || [];
2+
function gtag(){
3+
dataLayer.push(arguments);
4+
}
5+
gtag('js', new Date());
6+
gtag('config', window.GOOGLE_ADS_CONVERSION_ID);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function gtagReportConversion(url) {
2+
var callback = function () {
3+
if (typeof (url) != 'undefined') {
4+
window.location = url;
5+
}
6+
};
7+
gtag(
8+
'event',
9+
'conversion',
10+
{
11+
'send_to': window.GOOGLE_ADS_CONVERSION_TARGET_ADDRESS,
12+
'event_callback': callback
13+
}
14+
);
15+
return false;
16+
}

company_website/templates/base.haml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
{% load google_analytics_tags %}
77
{% load pipeline %}
88
{% stylesheet 'common' %}
9-
.google-analytics
10-
%img.google-analytics-image{:src => "{% google_analytics %}", :alt => "Google Analytics"}
119

1210
{% block extra_head %}
1311
{% endblock %}
1412

13+
.google-analytics
14+
%img.google-analytics-image{:src => "{% google_analytics %}", :alt => "Google Analytics"}
1515
.overlay
16+
1617
{% include "common_partials/menu.haml" %}
1718

1819
{% block content %}
1920
{% endblock %}
2021

2122
{% include "common_partials/footer.haml" %}
22-
2323
{% include "common_partials/scripts.haml" %}
2424

2525
{% block extra_script %}

company_website/templates/common_partials/head.haml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{% load static %}
2+
{% load pipeline %}
23

3-
/ Google Tag Manager
44
:javascript
5-
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
6-
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
7-
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
8-
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
9-
})(window,document,'script','dataLayer','GTM-TCWZK7N');
10-
/ End Google Tag Manager
5+
window.GOOGLE_ADS_CONVERSION_ID = "{{ GOOGLE_ADS_CONVERSION_ID }}"
6+
7+
%script{:async => "", :src => "https://www.googletagmanager.com/gtag/js?id={{ google_ads_conversion_id }}"}
8+
9+
{% javascript 'common_head' %}
1110

1211
%link{:rel => "stylesheet",
1312
:type => "text/css",

company_website/templates/estimate_project.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
{% block extra_head %}
1212
{% stylesheet 'estimate_project' %}
13+
:javascript
14+
window.GOOGLE_ADS_CONVERSION_TARGET_ADDRESS = '{{ GOOGLE_ADS_CONVERSION_TARGET_ADDRESS }}'
15+
{% javascript 'estimate_project_head' %}
1316
{% endblock %}
1417

1518
{% block content %}

company_website/templates/estimate_project/estimate_project_form.haml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
{{form.privacy_policy_accepted}}
6363
I have read and accepted our
6464
%a{:href => "{% url 'privacy_and_policy' %}"} Privacy Policy
65-
%input.custom-button{:type => "submit", :value => "Estimate Project", :class => "estimate-button", :id => "btnSubmit"}
65+
{% url "thank_you" as thank_you_url %}
66+
%input.custom-button{:type => "submit", :value => "Estimate Project", :class => "estimate-button", :id => "btnSubmit", :onclick => 'return gtagReportConversion("{{ URL_PREFIX }}://{{ request.get_host }}{{ thank_you_url }}")'}
6667

6768
.row
6869
.hide-add-questions

company_website/view_helpers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
from typing import Any
22
from typing import Dict
33

4+
from django.conf import settings
45
from django.views.generic import TemplateView
6+
from django.views.generic.base import ContextMixin
57

68
from company_website.models import PageSeo
79

810

9-
class CustomTemplateView(TemplateView):
11+
class GoogleAdsMixin(ContextMixin):
12+
extra_context = {
13+
"GOOGLE_ADS_CONVERSION_ID": settings.GOOGLE_ADS_CONVERSION_ID,
14+
"GOOGLE_ADS_CONVERSION_TARGET_ADDRESS": settings.GOOGLE_ADS_CONVERSION_TARGET_ADDRESS,
15+
}
16+
17+
18+
class CustomTemplateView(TemplateView, GoogleAdsMixin):
1019
page_name = ""
1120

1221
def get_context_data(self, **kwargs: Any) -> Dict:

company_website/views.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
from company_website.models import Employees
1212
from company_website.models import Testimonial
1313
from company_website.view_helpers import CustomTemplateView
14+
from company_website.view_helpers import GoogleAdsMixin
1415
from company_website.view_helpers import add_meta_tags_to_page_context
1516

1617

17-
class MainPageView(ListView):
18+
class MainPageView(ListView, GoogleAdsMixin):
1819

1920
template_name = "main_page.haml"
2021
model = Testimonial
@@ -27,7 +28,7 @@ def get_context_data(self, **kwargs: Any) -> dict:
2728
return context_data
2829

2930

30-
class TeamIntroductionPageView(ListView):
31+
class TeamIntroductionPageView(ListView, GoogleAdsMixin):
3132

3233
template_name = "team_introduction_page.haml"
3334
model = Employees
@@ -59,12 +60,17 @@ class PrivacyAndPolicyView(CustomTemplateView):
5960
page_name = PageNames.PRIVACY_AND_POLICY.name
6061

6162

62-
class EstimateProjectView(FormView):
63+
class EstimateProjectView(FormView, GoogleAdsMixin):
6364

6465
template_name = "estimate_project.haml"
6566
form_class = ProjectToEstimateForm
6667
page_name = PageNames.ESTIMATE_PROJECT.name
6768

69+
def get_context_data(self, *, _object_list: Any = None, **kwargs: Any) -> dict:
70+
context_data = super().get_context_data(**kwargs)
71+
context_data["URL_PREFIX"] = settings.URL_PREFIX
72+
return context_data
73+
6874
def form_valid(self, form: ProjectToEstimateForm) -> bool:
6975
messages.success(self.request, "Profile details updated.")
7076
form.save()

0 commit comments

Comments
 (0)