When you are building a hybrid application or Single-page applications (SPAs), you will notice that pageview tracking won’t work out of the box like a normal website.
- Data layer push isn’t working
- GTM triggers won’t work
- Debugging is a hassle
- Or Ionics Tag Manager plugin isn’t working in the latest version of Ionic
Code that won’t work
Before reading this article you may have attempted triggering page views with tag manager or by listening to your router in the AppComponent and tried triggering virtual page views with datalayer push.
Guess what, nothing is happening and your there is not activity on your Google Analytics dashboard. You have no easy method or way to debug and figure out what is going wrong.
Thinking up with a solution
The solution I applied was, by analyzing existing pageview requests from a normal website.
This is what i noticed
- The domain-name is www.google-analytics.com
- The query string parameters; the Google analytics code, title and path
- The method name = GET
- Wait, this is really just a generic Ajax request ?
My conclusion was that sending pageview event requests, without datalayer was very simple to implement…
Description of the query parameters
These are the query parameters that are of most importance to enable tracking.
- tid: Google Analytics code
- t: the type of request (pageview, event, …)
- cid: random number that serves as the client id
- dl: the url
- dt: Document title in the HTML tag (Home | Company name)
- el: page title or event label
Solution to page view tracking (code)
The quickest solution I came up with, was by performing a simple Ajax call to Google Analytics . Remove the Tag Manager script and increase page loading.
- Use the fetch api
- Replace certain query parameter; GA code, title, path and client id
- Use URLSearchParams to append parameters
- Create a random number as a client id
As result I was able to figure out to enable page view tracking and afterwards trigger custom events.
The only downfall is dropping tag manager as a service and it’s easiness, to be replaced with something so custom.