How to trigger custom script on Google Analytics event? - google-analytics

I'm using Google Analytics for goal tracking and I need to run a custom script when Goal is complete. Does GA/GTM have any way to solve this? Thanks.

The goal conversion is recorded serverside, so to be nitpicky you cannot track goal conversions on the client. However you can track the pageviews or events that are defined as goals in GA (this is not purely semantical, events and pageviews will happen once per hit, goal conversions happen once per session).
To track if the condition for a goal is met you can use a hit callback, a function that is executed once the tracking call has finished.
<script>
ga('send', 'event', {
'eventCategory': 'Category',
'eventAction': 'Action',
'eventLabel': 'This is set as a goal!',
'hitCallback': function() {
alert("I'm a user function hear me roar!");
}
});
</script>
The alert will be triggered once the event tracking is complete (obviously you would replace the alert with a call to your custom function).

Related

Which is the best method to set up event tracking for new account signup steps in GA4?

Currently I'm using the following analytics.js method to track different funnels/goals and these get triggered on each specific page.
ga('send', 'pageview', '/ConversionEvents/Step1_MembershipPlan');
ga('send', 'pageview', '/ConversionEvents/Step2_AccountInformation');
ga('send', 'pageview', '/ConversionEvents/Step3_PaymentInformation');
ga('send', 'pageview', '/ConversionEvents/Step4_AccountConfirmation');
ga('send', 'pageview', '/ConversionEvents/Step5_ThankYou');
However, now that I switched to gtag.js I'm seeing what the best method would be to set these up. I came up with the following 3 options.
Option 1:
gtag('event', 'sign_up', {'step_name': 'step1_membership_plan'});
Option 2:
gtag('event', 'pageview', {'step_name': 'step1_membership_plan'});
Option 3:
gtag('event', 'step1_membership_plan');
I'm leaning towards using option 3 because it would be easier to view these events in GA4 since it will be listed as a different event name and it would be easier to create funnels.
From reading online, they say it's better to use the recommended/automatic events like sign_up and pageview for this but I'm not sure if then I can set up events to track the different parameters (e.g. step_name) that I set up for each step.
Which option would be the best to go with?
Thank you!
Personally, I would not clutter my GA4 with too many distinct event names.
But that's a matter of taste: If I understand Google's limits page correctly, web properties do not currently have a limit on distinctly named events, whereas app properties are limited to 500.
Ref.: https://support.google.com/analytics/answer/9267744?hl=en)
Instead, I would send the following events:
sign_up, when a user has completed the registration, aka visits the thank you page.
sign_up_step_complete, when a user has completed a step in the registration process. Sending the custom dimension step_name for each step.
(That is assuming that each step is just a virtual page view and does not actually send a page_view event to Google. If it does send a page_view, you can create the sign_up_step_complete event server-side based on the URL or not create an event at all and just use the page_view.)
This way, you're using the dafault sign_up event which might show up in a default GA report while also tracking intermediate steps in a separate custom event.
Finally, I would visualize the data in a free-form exploration funnel analysis.

Sending GA E-Commerce Events without Pageview

I'm working on an implementation of Advanced E-Commerce in a "messy" GA/GTM environment with multiple tags and multiple GTM installs.
In order to manage the complexity and prevent multiple page-views from firing for every page, I'm trying to use events to get the data to send to GA, rather than relying on a page-view for that.
For example,
for(var i = 0; i < {{variable}}.length; i++) {
var product = {{wizely.variable}}[i];
ga({{TRACKERID}}+'.ec:addImpression', {
'name': product.name,
'id': product.ID
'list': 'LIST',
'position': i
})
}
ga({{TRACKERID}}+".send","event","Ecommerce","Impressions","LIST",{nonInteraction: true});
This is working great, but I can't seem to find a list of Ecommerce events that GA will accept, and I'm trying to use this method for things other than impressions.
Example, will this work?
ga({{TRACKERID}}+".send","event","Ecommerce","Detail","Detail-Page",{nonInteraction: true});
Thanks!
The ga() method you're using is just regular event tracking, you can name them however. The reason you'd use those is to "send" a hit to GA with the ecommerce data.
There are ecommerce actions that you should consider and THEN send the GA event. See here: https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-activities

Trigger analytics event in the past with a specified date

I'm capturing the date of a product installation when user extends a trial, and pass it to a thank you page as a parameter. Then I capture it and want to retroactively trigger event in the past.
From docs the code looks like:
ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
And nothing is said about ability to set any dates.
Is there a way to do it?
You can not push any events retroactively in Google Analytics.
All data is connected to the time that they were sent on.

Send google analytics event in loop

I'm a newbie in analytics.
I want to track a record-created event every time someone creates a time-record in a web application for time-tracking. But the user can also copy a time record to multiple dates which should yield one record-created event for each new record.
Would a simple loop work or does google think my events are coming to them too fast? Should I add a setTimeout?
_.each(dates, function() {
ga('send', 'event', 'record-created');
// maybe wrap the line above in a setTimeout ?
});
As per documentation:
Each analytics.js tracker object starts with 20 hits that are
replenished at a rate of 2 hit per second. Applies to All hits except
for ecommerce (item or transaction).
Plus you have "only" 500 interaction hits per session, so you should not send too many events or GA will drop data.

Detect if user is already logged into LinkedIn and log the results

I'd like to track how many people hit my website that might be logged into LinkedIn. Depending on the results, I might add an interactive LinkedIn section using their API.
I already have the Javascript logic to detect if they're logged in. My question is what is the best approach to log this result? Can Google Analytics do it with a custom variable or event detection?
I'd love to report on something like, "10,000 visits to my website, of which X number of visitors were logged into LinkedIn".
Any help would be appreciated!
I believe Google Analytics Custom Tracking Variables would do the trick. Something along the lines of:
_gaq.push(['_setCustomVar',
1, // variable slot
'LinkedInUser', // variable
'true', // value
1 // variable scope, 1=visitor
]);
_gaq.push(['_trackPageview']);
For custom variables (not that is this is a user-triggered event after the initial GA page load logic, a call to _trackPageview is needed to push the data back to GA), or:
_gaq.push(['_trackEvent',
'PageView',
'LinkedIn'
]);
For custom events. Just pop something similar to that into your JS routine that checks for authed LinkedIn users.

Resources