1

Will Google Analytics be recording page views / events from within a Live Preview? And if so, is there a way to prevent this from happening?

My understanding is that with Google Analytics you can set the domain for which it should track (e.g. to prevent it trying to track any local build events etc), but I am presuming that the domain used for Live Preview views will be the same as as if a user was viewing the front end of the site?

If it would track Live Preview views, I understand that an option for some people would be to wrap their GA code block in a conditional:

{% if not craft.request.isLivePreview %}{# ga code here #}{% endif %}

but because we have custom event tracking built into the JS, which therefore uses the global ga object, it would not be as simple in our case (though is still an option with a few checks for whether ga is defined).

3

If you're using SEOmatic - no. It's built in that the GA doesn't get spat out for the preview. If you're using a regular template - yes; you may want to wrap your GA code in a conditional so that it's not spat out when requested via the live preview.

{% if not craft.config.devMode and not craft.request.isLivePreview %}{# GA code here #}{% else %}<!-- Google Analytics script is NOT included in LivePreview or if devMode is on -->{% endif %}

[edit] Whoops, saw the other half of your question. What is it about your set up that's stopping you doing this? Sounds more like an Analytics issue than a Craft one to me.

| improve this answer | |
  • Thanks Matt! As I mentioned in this question, this is an option, it would just involve adding some extra conditionals into the JS to check if ga is defined before firing any custom events / page tracking. – Mike Vaux Oct 6 '16 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.